77import com .azuriom .azauth .model .ErrorResponse ;
88import com .azuriom .azauth .model .User ;
99import com .google .gson .*;
10- import org .jetbrains .annotations .Blocking ;
11- import org .jetbrains .annotations .Contract ;
12- import org .jetbrains .annotations .NotNull ;
13- import org .jetbrains .annotations .Nullable ;
10+ import org .jspecify .annotations .NullMarked ;
11+ import org .jspecify .annotations .Nullable ;
1412
1513import java .awt .*;
1614import java .io .BufferedReader ;
2826
2927/**
3028 * The authentication client for Azuriom.
29+ * All methods perform blocking operations and should not be called on a non-blocking thread.
3130 */
31+ @ NullMarked
3232public class AuthClient {
3333
3434 private static final Logger LOGGER = Logger .getLogger (AuthClient .class .getName ());
@@ -40,14 +40,14 @@ public class AuthClient {
4040 .registerTypeAdapter (UUID .class , new UuidAdapter ())
4141 .create ();
4242
43- private final @ NotNull String url ;
43+ private final String url ;
4444
4545 /**
46- * Construct a new AzAuthenticator instance.
46+ * Construct a new AzAuthenticator instance, with the given website URL .
4747 *
4848 * @param url the website url
4949 */
50- public AuthClient (@ NotNull String url ) {
50+ public AuthClient (String url ) {
5151 Objects .requireNonNull (url , "url" );
5252
5353 this .url = url .endsWith ("/" ) ? url .substring (0 , url .length () - 1 ) : url ;
@@ -62,7 +62,7 @@ public AuthClient(@NotNull String url) {
6262 *
6363 * @return the website url
6464 */
65- public @ NotNull String getUrl () {
65+ public String getUrl () {
6666 return this .url ;
6767 }
6868
@@ -74,9 +74,7 @@ public AuthClient(@NotNull String url) {
7474 * @return the user profile
7575 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
7676 */
77- @ Blocking
78- public @ NotNull AuthResult <@ NotNull User > login (@ NotNull String email ,
79- @ NotNull String password ) throws AuthException {
77+ public AuthResult <User > login (String email , String password ) throws AuthException {
8078 return this .login (email , password , User .class );
8179 }
8280
@@ -89,10 +87,9 @@ public AuthClient(@NotNull String url) {
8987 * @return the user profile
9088 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
9189 */
92- @ Blocking
93- public @ NotNull AuthResult <@ NotNull User > login (@ NotNull String email ,
94- @ NotNull String password ,
95- @ Nullable String code2fa ) throws AuthException {
90+ public AuthResult <User > login (String email ,
91+ String password ,
92+ @ Nullable String code2fa ) throws AuthException {
9693 return this .login (email , password , code2fa , User .class );
9794 }
9895
@@ -106,10 +103,9 @@ public AuthClient(@NotNull String url) {
106103 * @return the user profile
107104 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
108105 */
109- @ Blocking
110- public <T > @ NotNull AuthResult <@ NotNull T > login (@ NotNull String email ,
111- @ NotNull String password ,
112- @ NotNull Class <T > responseType ) throws AuthException {
106+ public <T > AuthResult <T > login (String email ,
107+ String password ,
108+ Class <T > responseType ) throws AuthException {
113109 return login (email , password , (String ) null , responseType );
114110 }
115111
@@ -123,10 +119,9 @@ public AuthClient(@NotNull String url) {
123119 * @return the user profile
124120 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
125121 */
126- @ Blocking
127- public @ NotNull User login (@ NotNull String email ,
128- @ NotNull String password ,
129- @ NotNull Supplier <String > codeSupplier ) throws AuthException {
122+ public User login (String email ,
123+ String password ,
124+ Supplier <String > codeSupplier ) throws AuthException {
130125 return login (email , password , codeSupplier , User .class );
131126 }
132127
@@ -142,11 +137,10 @@ public AuthClient(@NotNull String url) {
142137 * @return the user profile
143138 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
144139 */
145- @ Blocking
146- public <T > @ NotNull T login (@ NotNull String email ,
147- @ NotNull String password ,
148- @ NotNull Supplier <String > codeSupplier ,
149- @ NotNull Class <T > responseType ) throws AuthException {
140+ public <T > T login (String email ,
141+ String password ,
142+ Supplier <@ Nullable String > codeSupplier ,
143+ Class <T > responseType ) throws AuthException {
150144 AuthResult <T > result = login (email , password , responseType );
151145
152146 if (result .isSuccess ()) {
@@ -183,11 +177,10 @@ public AuthClient(@NotNull String url) {
183177 * @return the user profile
184178 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
185179 */
186- @ Blocking
187- public <T > @ NotNull AuthResult <@ NotNull T > login (@ NotNull String email ,
188- @ NotNull String password ,
189- @ Nullable String code2fa ,
190- @ NotNull Class <T > responseType ) throws AuthException {
180+ public <T > AuthResult <T > login (String email ,
181+ String password ,
182+ @ Nullable String code2fa ,
183+ Class <T > responseType ) throws AuthException {
191184 JsonObject body = new JsonObject ();
192185 body .addProperty ("email" , email );
193186 body .addProperty ("password" , password );
@@ -203,8 +196,7 @@ public AuthClient(@NotNull String url) {
203196 * @return the user profile
204197 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
205198 */
206- @ Blocking
207- public @ NotNull User verify (@ NotNull String accessToken ) throws AuthException {
199+ public User verify (String accessToken ) throws AuthException {
208200 return this .verify (accessToken , User .class );
209201 }
210202
@@ -217,9 +209,7 @@ public AuthClient(@NotNull String url) {
217209 * @return the user profile
218210 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
219211 */
220- @ Blocking
221- public <T > @ NotNull T verify (@ NotNull String accessToken , @ NotNull Class <T > responseType )
222- throws AuthException {
212+ public <T > T verify (String accessToken , Class <T > responseType ) throws AuthException {
223213 JsonObject body = new JsonObject ();
224214 body .addProperty ("access_token" , accessToken );
225215
@@ -234,24 +224,20 @@ public AuthClient(@NotNull String url) {
234224
235225 /**
236226 * Invalidate the given access token.
237- * To get a new valid access token you need to use {@link #login(String, String)} again.
227+ * To get a new valid access token, you need to use {@link #login(String, String)} again.
238228 *
239229 * @param accessToken the user access token
240230 * @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
241231 */
242- @ Blocking
243- public void logout (@ NotNull String accessToken ) throws AuthException {
232+ public void logout (String accessToken ) throws AuthException {
244233 JsonObject body = new JsonObject ();
245234 body .addProperty ("access_token" , accessToken );
246235
247- this .post ("logout" , body , null );
236+ this .post ("logout" , body , JsonNull . class );
248237 }
249238
250- @ Blocking
251- @ Contract ("_, _, null -> null; _, _, !null -> !null" )
252- private <T > AuthResult <T > post (@ NotNull String endpoint ,
253- @ NotNull JsonObject body ,
254- @ Nullable Class <T > responseType ) throws AuthException {
239+ private <T > AuthResult <T > post (String endpoint , JsonObject body , Class <T > responseType )
240+ throws AuthException {
255241 try {
256242 URI api = URI .create (this .url + "/api/auth/" + endpoint );
257243 HttpURLConnection connection = (HttpURLConnection ) api .toURL ().openConnection ();
@@ -271,18 +257,18 @@ private <T> AuthResult<T> post(@NotNull String endpoint,
271257 return this .handleClientError (connection );
272258 }
273259
274- if (responseType == null ) {
275- return null ;
276- }
277-
278260 return handleResponse (connection , responseType );
279261 } catch (IOException e ) {
280262 throw new AuthException (e );
281263 }
282264 }
283265
284- private <T > AuthResult <T > handleResponse (HttpURLConnection connection , Class <T > type )
266+ private <T > AuthResult <T > handleResponse (HttpURLConnection connection , Class <@ Nullable T > type )
285267 throws AuthException , IOException {
268+ if (type == JsonNull .class ) {
269+ return new AuthResult .Success <>(type .cast (JsonNull .INSTANCE ));
270+ }
271+
286272 try (BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()))) {
287273 T response = GSON .fromJson (reader , type );
288274
0 commit comments