Skip to content

Commit c209c66

Browse files
committed
Merge pull request #20 from auth0/oauth2-token-request
add method to call /oauth/token to the AuthenticationAPIClient
2 parents 0e7cd8e + 9f13287 commit c209c66

4 files changed

Lines changed: 75 additions & 13 deletions

File tree

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@
2929
import com.auth0.authentication.result.DatabaseUser;
3030
import com.auth0.authentication.result.Delegation;
3131
import com.auth0.authentication.result.UserProfile;
32-
import com.auth0.request.internal.RequestFactory;
3332
import com.auth0.request.AuthenticationRequest;
3433
import com.auth0.request.ParameterizableRequest;
3534
import com.auth0.request.Request;
35+
import com.auth0.request.internal.RequestFactory;
3636
import com.auth0.util.Telemetry;
3737
import com.fasterxml.jackson.databind.ObjectMapper;
3838
import com.squareup.okhttp.HttpUrl;
3939
import com.squareup.okhttp.OkHttpClient;
4040

4141
import java.util.Map;
4242

43+
import static com.auth0.authentication.ParameterBuilder.GRANT_TYPE_AUTHORIZATION_CODE;
4344
import static com.auth0.authentication.ParameterBuilder.GRANT_TYPE_PASSWORD;
4445

4546
/**
@@ -67,8 +68,12 @@ public class AuthenticationAPIClient {
6768
private static final String PASSWORDLESS_PATH = "passwordless";
6869
private static final String START_PATH = "start";
6970
private static final String OAUTH_PATH = "oauth";
71+
private static final String TOKEN_PATH = "token";
7072
private static final String RESOURCE_OWNER_PATH = "ro";
7173
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";
7277

7378
private final Auth0 auth0;
7479
private final OkHttpClient client;
@@ -160,7 +165,7 @@ public AuthenticationRequest login(String usernameOrEmail, String password) {
160165
* .start(new BaseCallback<Credentials>() {
161166
* {@literal}Override
162167
* public void onSuccess(Credentials payload) { }
163-
168+
*
164169
* {@literal}Override
165170
* public void onFailure(Auth0Exception error) { }
166171
* });
@@ -194,7 +199,7 @@ public AuthenticationRequest loginWithOAuthAccessToken(String token, String conn
194199
* .start(new BaseCallback<Credentials>() {
195200
* {@literal}Override
196201
* public void onSuccess(Credentials payload) { }
197-
202+
*
198203
* {@literal}@Override
199204
* public void onFailure(Auth0Exception error) { }
200205
* });
@@ -223,7 +228,7 @@ public AuthenticationRequest loginWithPhoneNumber(String phoneNumber, String ver
223228
* .start(new BaseCallback<Credentials>() {
224229
* {@literal}Override
225230
* public void onSuccess(Credentials payload) { }
226-
231+
*
227232
* {@literal}@Override
228233
* public void onFailure(Auth0Exception error) { }
229234
* });
@@ -252,7 +257,7 @@ public AuthenticationRequest loginWithEmail(String email, String verificationCod
252257
* .start(new BaseCallback<UserProfile>() {
253258
* {@literal}Override
254259
* public void onSuccess(UserProfile payload) { }
255-
260+
*
256261
* {@literal}@Override
257262
* public void onFailure(Auth0Exception error) { }
258263
* });
@@ -677,4 +682,30 @@ private ParameterizableRequest<UserProfile> profileRequest() {
677682

678683
return factory.POST(url, client, mapper, UserProfile.class);
679684
}
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+
}
680711
}

auth0/src/main/java/com/auth0/authentication/ParameterBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* Builder for Auth0 Authentication API parameters
35-
*
35+
* <p/>
3636
* You can build your parameters like this
3737
* <pre><code>
3838
* Map<String, Object> parameters = ParameterBuilder.newBuilder()
@@ -49,6 +49,7 @@ public class ParameterBuilder {
4949

5050
public static final String GRANT_TYPE_PASSWORD = "password";
5151
public static final String GRANT_TYPE_JWT = "urn:ietf:params:oauth:grant-type:jwt-bearer";
52+
public static final String GRANT_TYPE_AUTHORIZATION_CODE = "authorization_code";
5253

5354
public static final String SCOPE_OPENID = "openid";
5455
public static final String SCOPE_OFFLINE_ACCESS = "openid offline_access";

auth0/src/test/java/com/auth0/authentication/AuthenticationAPIClientTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626

2727

2828
import com.auth0.Auth0;
29-
import com.auth0.Auth0Exception;
3029
import com.auth0.authentication.result.Authentication;
3130
import com.auth0.authentication.result.Credentials;
3231
import com.auth0.authentication.result.DatabaseUser;
3332
import com.auth0.authentication.result.Delegation;
3433
import com.auth0.authentication.result.UserProfile;
35-
import com.auth0.request.ParameterizableRequest;
3634
import com.auth0.util.AuthenticationAPI;
3735
import com.auth0.util.MockBaseCallback;
3836
import com.fasterxml.jackson.core.type.TypeReference;
@@ -50,7 +48,6 @@
5048
import static com.auth0.util.AuthenticationAPI.ID_TOKEN;
5149
import static com.auth0.util.AuthenticationAPI.REFRESH_TOKEN;
5250
import static com.auth0.util.CallbackMatcher.hasNoError;
53-
import static com.auth0.util.CallbackMatcher.hasNoPayloadOfType;
5451
import static com.auth0.util.CallbackMatcher.hasPayload;
5552
import static com.auth0.util.CallbackMatcher.hasPayloadOfType;
5653
import static org.hamcrest.Matchers.equalTo;
@@ -769,7 +766,7 @@ public void shouldGetNewIdTokenWithRefreshTokenSync() throws Exception {
769766

770767
assertThat(delegation, is(notNullValue()));
771768
}
772-
769+
773770
@Test
774771
public void shouldUnlinkAccount() throws Exception {
775772
mockAPI.willReturnSuccessfulUnlinkAccount();
@@ -1173,7 +1170,29 @@ public void shouldFetchProfileAfterLoginRequest() throws Exception {
11731170
assertThat(secondRequest.getPath(), equalTo("/tokeninfo"));
11741171

11751172
assertThat(callback, hasPayloadOfType(Authentication.class));
1173+
}
1174+
1175+
@Test
1176+
public void shouldGetOAuthTokens() throws Exception {
1177+
mockAPI
1178+
.willReturnTokens()
1179+
.willReturnTokenInfo();
11761180

1181+
final MockBaseCallback<Credentials> callback = new MockBaseCallback<>();
1182+
client.token("code", "codeVerifier", "http://redirect.uri")
1183+
.start(callback);
1184+
1185+
final RecordedRequest request = mockAPI.takeRequest();
1186+
assertThat(request.getPath(), equalTo("/oauth/token"));
1187+
1188+
Map<String, String> body = bodyFromRequest(request);
1189+
assertThat(body, hasEntry("grant_type", ParameterBuilder.GRANT_TYPE_AUTHORIZATION_CODE));
1190+
assertThat(body, hasEntry("client_id", CLIENT_ID));
1191+
assertThat(body, hasEntry("code", "code"));
1192+
assertThat(body, hasEntry("code_verifier", "codeVerifier"));
1193+
assertThat(body, hasEntry("redirect_uri", "http://redirect.uri"));
1194+
1195+
assertThat(callback, hasPayloadOfType(Credentials.class));
11771196
}
11781197

11791198
private Map<String, String> bodyFromRequest(RecordedRequest request) throws java.io.IOException {

auth0/src/test/java/com/auth0/util/AuthenticationAPI.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030

3131
import java.io.IOException;
3232

33-
public class AuthenticationAPI {
33+
public class AuthenticationAPI {
3434

3535
public static final String REFRESH_TOKEN = "REFRESH_TOKEN";
3636
public static final String ID_TOKEN = "ID_TOKEN";
3737
public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
3838
public static final String BEARER = "BEARER";
3939
public static final String GENERIC_TOKEN = "GENERIC_TOKEN";
4040
public static final String NEW_ID_TOKEN = "NEW_ID_TOKEN";
41-
public static final int EXPIRES_IN = 1234567890;
4241
public static final String TOKEN_TYPE = "TOKEN_TYPE";
42+
public static final int EXPIRES_IN = 1234567890;
4343

4444
private MockWebServer server;
4545

@@ -155,8 +155,19 @@ public AuthenticationAPI willReturnTokenInfo() {
155155
return this;
156156
}
157157

158+
public AuthenticationAPI willReturnTokens() {
159+
String json = "{\"" +
160+
"access_token\": \"" + ACCESS_TOKEN + "\"," +
161+
"\"refresh_token\": \"" + REFRESH_TOKEN + "\"," +
162+
"\"id_token\":\"" + ID_TOKEN + "\"," +
163+
"\"token_type\":\"Bearer\"" +
164+
"}";
165+
server.enqueue(responseWithJSON(json, 200));
166+
return this;
167+
}
168+
158169
public AuthenticationAPI willReturnApplicationResponseWithBody(String body, int statusCode) {
159-
MockResponse response = new MockResponse()
170+
MockResponse response = new MockResponse()
160171
.setResponseCode(statusCode)
161172
.addHeader("Content-Type", "application/x-javascript")
162173
.setBody(body);

0 commit comments

Comments
 (0)