Skip to content

Commit e31ddbb

Browse files
committed
Merge pull request #3 from auth0/sync-mode
Add sync (blocking) mode to all requests
2 parents fbb8d69 + 7021a91 commit e31ddbb

17 files changed

Lines changed: 909 additions & 115 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Auth0Exception.java
3+
*
4+
* Copyright (c) 2016 Auth0 (http://auth0.com)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.auth0;
26+
27+
/**
28+
* Base Exception for any error found during a request to Auth0's API
29+
*/
30+
public class Auth0Exception extends RuntimeException {
31+
32+
public Auth0Exception(String message, Throwable cause) {
33+
super(message, cause);
34+
}
35+
36+
public Auth0Exception(String message) {
37+
super(message);
38+
}
39+
}

core/src/main/java/com/auth0/authentication/AuthenticationAPIClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,12 @@ public ParameterizableRequest<Void> passwordlessWithSMS(String phoneNumber, Pass
406406
.addParameters(parameters);
407407
}
408408

409-
protected ParameterizableRequest<Map<String, Object>> delegation() {
409+
/**
410+
* Performs a custom <a href="https://auth0.com/docs/auth-api#!#post--delegation">delegation</a> request that will
411+
* yield a delegation token.
412+
* @return a request to configure and start
413+
*/
414+
public ParameterizableRequest<Map<String, Object>> delegation() {
410415
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
411416
.addPathSegment("delegation")
412417
.build();
@@ -432,7 +437,11 @@ protected <T> ParameterizableRequest<T> delegation(Class<T> clazz) {
432437
.addParameters(parameters);
433438
}
434439

435-
protected ParameterizableRequest<Void> passwordless() {
440+
/**
441+
* Start a custom passwordless flow
442+
* @return a request to configure and start
443+
*/
444+
public ParameterizableRequest<Void> passwordless() {
436445
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
437446
.addPathSegment("passwordless")
438447
.addPathSegment("start")
@@ -446,7 +455,7 @@ protected ParameterizableRequest<Void> passwordless() {
446455
return RequestFactory.POST(url, client, mapper)
447456
.addParameters(parameters);
448457
}
449-
458+
450459
protected ParameterizableRequest<Token> loginWithResourceOwner() {
451460
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
452461
.addPathSegment("oauth")

core/src/main/java/com/auth0/authentication/AuthenticationRequest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.auth0.authentication;
2626

27+
import com.auth0.Auth0Exception;
2728
import com.auth0.authentication.api.ParameterBuilder;
2829
import com.auth0.authentication.api.ParameterizableRequest;
2930
import com.auth0.authentication.api.Request;
@@ -98,16 +99,34 @@ public void onSuccess(UserProfile profile) {
9899
}
99100

100101
@Override
101-
public void onFailure(Throwable error) {
102+
public void onFailure(Auth0Exception error) {
102103
callback.onFailure(error);
103104
}
104105
});
105106
}
106107

107108
@Override
108-
public void onFailure(Throwable error) {
109+
public void onFailure(Auth0Exception error) {
109110
callback.onFailure(error);
110111
}
111112
});
112113
}
114+
115+
/**
116+
* Logs in the user with Auth0 and fetches it's profile.
117+
* @return authentication object containing the user's tokens and profile
118+
* @throws Auth0Exception when either authentication or profile fetch fails
119+
*/
120+
@Override
121+
public Authentication execute() throws Auth0Exception {
122+
Token token = credentialsRequest.execute();
123+
Map<String, Object> parameters = new ParameterBuilder()
124+
.clearAll()
125+
.set("id_token", token.getIdToken())
126+
.asDictionary();
127+
UserProfile profile = tokenInfoRequest
128+
.addParameters(parameters)
129+
.execute();
130+
return new Authentication(profile, token);
131+
}
113132
}

core/src/main/java/com/auth0/authentication/ChangePasswordRequest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.auth0.authentication;
2626

27+
import com.auth0.Auth0Exception;
2728
import com.auth0.authentication.api.ParameterizableRequest;
2829
import com.auth0.authentication.api.callback.BaseCallback;
2930

@@ -82,11 +83,21 @@ public ChangePasswordRequest addHeader(String name, String value) {
8283
}
8384

8485
/**
85-
* Performs the HTTP request against Auth0 API
86+
* Starts the change password request
8687
* @param callback called either on success or failure
8788
*/
8889
@Override
8990
public void start(final BaseCallback<Void> callback) {
9091
request.start(callback);
9192
}
93+
94+
/**
95+
* Executes the change password request
96+
* @return nothing on success
97+
* @throws Auth0Exception when the change password request fails
98+
*/
99+
@Override
100+
public Void execute() throws Auth0Exception {
101+
return request.execute();
102+
}
92103
}

core/src/main/java/com/auth0/authentication/DelegationRequest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.auth0.authentication;
2626

27+
import com.auth0.Auth0Exception;
2728
import com.auth0.authentication.api.ParameterBuilder;
2829
import com.auth0.authentication.api.ParameterizableRequest;
2930
import com.auth0.authentication.api.Request;
@@ -33,7 +34,11 @@
3334
import java.util.Map;
3435

3536
/**
36-
* Represent a delegation request for Auth0 tokens that will yield a new 'id_token'
37+
* Represents a delegation request for Auth0 tokens that will yield a new delegation token.
38+
* The delegation response depends on the 'api_type' parameter.
39+
* @param <T> type of object that will hold the delegation response. When requesting Auth0’s 'id_token' you can
40+
* use {@link Delegation}, otherwise you’ll need to provide an object that can be created from the JSON
41+
* payload or just use {@code Map<String, Object>}
3742
*/
3843
public class DelegationRequest<T> implements Request<T> {
3944

@@ -65,7 +70,7 @@ public DelegationRequest<T> addParameters(Map<String, Object> parameters) {
6570
}
6671

6772
/**
68-
* Set the api_type parameter to be sent in the request
73+
* Set the 'api_type' parameter to be sent in the request
6974
* @param apiType the delegation api type
7075
* @return itself
7176
*/
@@ -74,7 +79,7 @@ public DelegationRequest<T> setApiType(String apiType) {
7479
}
7580

7681
/**
77-
* Set the scope used to make the delegation
82+
* Set the 'scope' used to make the delegation
7883
* @param scope value
7984
* @return itself
8085
*/
@@ -83,7 +88,7 @@ public DelegationRequest<T> setScope(String scope) {
8388
}
8489

8590
/**
86-
* Set the target parameter to be sent in the request
91+
* Set the 'target' parameter to be sent in the request
8792
* @param target the delegation target
8893
* @return itself
8994
*/
@@ -92,11 +97,21 @@ public DelegationRequest<T> setTarget(String target) {
9297
}
9398

9499
/**
95-
* Performs the HTTP request against Auth0 API
100+
* Starts the delegation request against Auth0 API
96101
* @param callback called either on success or failure
97102
*/
98103
@Override
99104
public void start(final BaseCallback<T> callback) {
100105
request.start(callback);
101106
}
107+
108+
/**
109+
* Executes the delegation request against Auth0 API
110+
* @return the delegation response on success
111+
* @throws Auth0Exception when the delegation request fails
112+
*/
113+
@Override
114+
public T execute() throws Auth0Exception {
115+
return request.execute();
116+
}
102117
}

core/src/main/java/com/auth0/authentication/SignUpRequest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.auth0.authentication;
2626

27+
import com.auth0.Auth0Exception;
2728
import com.auth0.authentication.api.ParameterizableRequest;
2829
import com.auth0.authentication.api.Request;
2930
import com.auth0.authentication.api.callback.BaseCallback;
@@ -98,9 +99,20 @@ public void onSuccess(final DatabaseUser user) {
9899
}
99100

100101
@Override
101-
public void onFailure(Throwable error) {
102+
public void onFailure(Auth0Exception error) {
102103
callback.onFailure(error);
103104
}
104105
});
105106
}
107+
108+
/**
109+
* Execute the create user request and then logs the user in.
110+
* @return authentication object on success
111+
* @throws Auth0Exception on failure
112+
*/
113+
@Override
114+
public Authentication execute() throws Auth0Exception {
115+
signUpRequest.execute();
116+
return authenticationRequest.execute();
117+
}
106118
}

core/src/main/java/com/auth0/authentication/api/APIClientException.java renamed to core/src/main/java/com/auth0/authentication/api/APIException.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
package com.auth0.authentication.api;
2626

27+
import com.auth0.Auth0Exception;
28+
2729
import java.util.HashMap;
2830
import java.util.Map;
2931

3032
/**
3133
* Internal exception raised when a request to the API fails
3234
*/
33-
public class APIClientException extends RuntimeException {
35+
public class APIException extends Auth0Exception {
3436

3537
private int statusCode;
3638

@@ -41,7 +43,7 @@ public class APIClientException extends RuntimeException {
4143
* @param detailMessage error message
4244
* @param throwable the cause of the exception
4345
*/
44-
public APIClientException(String detailMessage, Throwable throwable) {
46+
public APIException(String detailMessage, Throwable throwable) {
4547
super(detailMessage, throwable);
4648
this.statusCode = -1;
4749
this.responseError = new HashMap<>();
@@ -53,7 +55,7 @@ public APIClientException(String detailMessage, Throwable throwable) {
5355
* @param statusCode status code returned by the server
5456
* @param responseError payload of the error returned by the server
5557
*/
56-
public APIClientException(String detailMessage, int statusCode, Map<String, Object> responseError) {
58+
public APIException(String detailMessage, int statusCode, Map<String, Object> responseError) {
5759
super(detailMessage);
5860
this.statusCode = statusCode;
5961
this.responseError = responseError != null ? responseError : new HashMap<String, Object>();
@@ -66,7 +68,7 @@ public APIClientException(String detailMessage, int statusCode, Map<String, Obje
6668
* @param statusCode status code returned by the server
6769
* @param responseError payload of the error returned by the server
6870
*/
69-
public APIClientException(String detailMessage, Throwable throwable, int statusCode, Map<String, Object> responseError) {
71+
public APIException(String detailMessage, Throwable throwable, int statusCode, Map<String, Object> responseError) {
7072
super(detailMessage, throwable);
7173
this.statusCode = statusCode;
7274
this.responseError = responseError != null ? responseError : new HashMap<String, Object>();

core/src/main/java/com/auth0/authentication/api/Request.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.auth0.authentication.api;
2626

27+
import com.auth0.Auth0Exception;
2728
import com.auth0.authentication.api.callback.BaseCallback;
2829

2930
/**
@@ -32,5 +33,16 @@
3233
*/
3334
public interface Request<T> {
3435

36+
/**
37+
* Performs an async HTTP request against Auth0 API
38+
* @param callback called either on success or failure
39+
*/
3540
void start(BaseCallback<T> callback);
41+
42+
/**
43+
* Executes the HTTP request against Auth0 API (blocking the current thread)
44+
* @return the response on success
45+
* @throws Auth0Exception on failure
46+
*/
47+
T execute() throws Auth0Exception;
3648
}

core/src/main/java/com/auth0/authentication/api/RequestBodyBuildException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
package com.auth0.authentication.api;
2626

27+
import com.auth0.Auth0Exception;
28+
2729
/**
2830
* Exception that wraps errors when creating a body for a request
2931
*/
30-
public class RequestBodyBuildException extends RuntimeException {
32+
public class RequestBodyBuildException extends Auth0Exception {
3133

3234
public RequestBodyBuildException(String message, Throwable cause) {
3335
super(message, cause);

core/src/main/java/com/auth0/authentication/api/callback/Callback.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424

2525
package com.auth0.authentication.api.callback;
2626

27+
import com.auth0.Auth0Exception;
28+
2729
/**
2830
* Interface for all callbacks used with Auth0 API clients
2931
*/
3032
public interface Callback {
3133

3234
/**
3335
* Method called on Auth0 API request failure
34-
* @param error Error with the reason of the failure
36+
* @param error Auth0Exception with the reason of the failure
3537
*/
36-
void onFailure(Throwable error);
38+
void onFailure(Auth0Exception error);
3739

3840
}

0 commit comments

Comments
 (0)