|
29 | 29 | import com.auth0.authentication.result.DatabaseUser; |
30 | 30 | import com.auth0.authentication.result.Delegation; |
31 | 31 | import com.auth0.authentication.result.UserProfile; |
32 | | -import com.auth0.request.internal.RequestFactory; |
33 | 32 | import com.auth0.request.AuthenticationRequest; |
34 | 33 | import com.auth0.request.ParameterizableRequest; |
35 | 34 | import com.auth0.request.Request; |
| 35 | +import com.auth0.request.internal.RequestFactory; |
36 | 36 | import com.auth0.util.Telemetry; |
37 | 37 | import com.fasterxml.jackson.databind.ObjectMapper; |
38 | 38 | import com.squareup.okhttp.HttpUrl; |
39 | 39 | import com.squareup.okhttp.OkHttpClient; |
40 | 40 |
|
41 | 41 | import java.util.Map; |
42 | 42 |
|
| 43 | +import static com.auth0.authentication.ParameterBuilder.GRANT_TYPE_AUTHORIZATION_CODE; |
43 | 44 | import static com.auth0.authentication.ParameterBuilder.GRANT_TYPE_PASSWORD; |
44 | 45 |
|
45 | 46 | /** |
@@ -67,8 +68,12 @@ public class AuthenticationAPIClient { |
67 | 68 | private static final String PASSWORDLESS_PATH = "passwordless"; |
68 | 69 | private static final String START_PATH = "start"; |
69 | 70 | private static final String OAUTH_PATH = "oauth"; |
| 71 | + private static final String TOKEN_PATH = "token"; |
70 | 72 | private static final String RESOURCE_OWNER_PATH = "ro"; |
71 | 73 | private static final String TOKEN_INFO_PATH = "tokeninfo"; |
| 74 | + private static final String OAUTH_CODE_KEY = "code"; |
| 75 | + private static final String OAUTH_CODE_VERIFIER_KEY = "code_verifier"; |
| 76 | + private static final String REDIRECT_URI_KEY = "redirect_uri"; |
72 | 77 |
|
73 | 78 | private final Auth0 auth0; |
74 | 79 | private final OkHttpClient client; |
@@ -160,7 +165,7 @@ public AuthenticationRequest login(String usernameOrEmail, String password) { |
160 | 165 | * .start(new BaseCallback<Credentials>() { |
161 | 166 | * {@literal}Override |
162 | 167 | * public void onSuccess(Credentials payload) { } |
163 | | -
|
| 168 | + * |
164 | 169 | * {@literal}Override |
165 | 170 | * public void onFailure(Auth0Exception error) { } |
166 | 171 | * }); |
@@ -194,7 +199,7 @@ public AuthenticationRequest loginWithOAuthAccessToken(String token, String conn |
194 | 199 | * .start(new BaseCallback<Credentials>() { |
195 | 200 | * {@literal}Override |
196 | 201 | * public void onSuccess(Credentials payload) { } |
197 | | -
|
| 202 | + * |
198 | 203 | * {@literal}@Override |
199 | 204 | * public void onFailure(Auth0Exception error) { } |
200 | 205 | * }); |
@@ -223,7 +228,7 @@ public AuthenticationRequest loginWithPhoneNumber(String phoneNumber, String ver |
223 | 228 | * .start(new BaseCallback<Credentials>() { |
224 | 229 | * {@literal}Override |
225 | 230 | * public void onSuccess(Credentials payload) { } |
226 | | -
|
| 231 | + * |
227 | 232 | * {@literal}@Override |
228 | 233 | * public void onFailure(Auth0Exception error) { } |
229 | 234 | * }); |
@@ -252,7 +257,7 @@ public AuthenticationRequest loginWithEmail(String email, String verificationCod |
252 | 257 | * .start(new BaseCallback<UserProfile>() { |
253 | 258 | * {@literal}Override |
254 | 259 | * public void onSuccess(UserProfile payload) { } |
255 | | -
|
| 260 | + * |
256 | 261 | * {@literal}@Override |
257 | 262 | * public void onFailure(Auth0Exception error) { } |
258 | 263 | * }); |
@@ -677,4 +682,30 @@ private ParameterizableRequest<UserProfile> profileRequest() { |
677 | 682 |
|
678 | 683 | return factory.POST(url, client, mapper, UserProfile.class); |
679 | 684 | } |
| 685 | + |
| 686 | + /** |
| 687 | + * Fetch the token information from Auth0, using the authorization_code grant type |
| 688 | + * |
| 689 | + * @param authorizationCode the authorization code received from the /authorize call. |
| 690 | + * @param codeVerifier the code verifier used when requesting a code to /authorize. |
| 691 | + * @param redirectUri the uri to redirect after a successful request. |
| 692 | + * @return a request to configure and start |
| 693 | + */ |
| 694 | + public AuthenticationRequest token(String authorizationCode, String codeVerifier, String redirectUri) { |
| 695 | + Map<String, Object> parameters = ParameterBuilder.newBuilder() |
| 696 | + .setClientId(getClientId()) |
| 697 | + .setGrantType(GRANT_TYPE_AUTHORIZATION_CODE) |
| 698 | + .set(OAUTH_CODE_KEY, authorizationCode) |
| 699 | + .set(OAUTH_CODE_VERIFIER_KEY, codeVerifier) |
| 700 | + .set(REDIRECT_URI_KEY, redirectUri) |
| 701 | + .asDictionary(); |
| 702 | + |
| 703 | + HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder() |
| 704 | + .addPathSegment(OAUTH_PATH) |
| 705 | + .addPathSegment(TOKEN_PATH) |
| 706 | + .build(); |
| 707 | + |
| 708 | + return factory.authenticationPOST(url, client, mapper) |
| 709 | + .addAuthenticationParameters(parameters); |
| 710 | + } |
680 | 711 | } |
0 commit comments