Skip to content

Commit 3a0a54d

Browse files
arcseldonhzalaz
authored andcommitted
TokenInfo and UserProfile updates
1 parent ccb52ef commit 3a0a54d

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class AuthenticationAPIClient {
7373
private static final String TOKEN_INFO_PATH = "tokeninfo";
7474
private static final String OAUTH_CODE_KEY = "code";
7575
private static final String OAUTH_CODE_VERIFIER_KEY = "code_verifier";
76+
private static final String OAUTH_CLIENT_SECRET_KEY = "client_secret";
7677
private static final String REDIRECT_URI_KEY = "redirect_uri";
7778

7879
private final Auth0 auth0;
@@ -703,6 +704,19 @@ private ParameterizableRequest<UserProfile> profileRequest() {
703704
return factory.POST(url, client, gson, UserProfile.class);
704705
}
705706

707+
/**
708+
* For backwards compatibility only
709+
*
710+
* @param authorizationCode
711+
* @param codeVerifier
712+
* @param redirectUri
713+
* @return
714+
*/
715+
@Deprecated
716+
public AuthenticationRequest token(String authorizationCode, String codeVerifier, String redirectUri) {
717+
return tokenUsingCodeVerifier(authorizationCode, codeVerifier, redirectUri);
718+
}
719+
706720
/**
707721
* Fetch the token information from Auth0, using the authorization_code grant type
708722
*
@@ -711,7 +725,7 @@ private ParameterizableRequest<UserProfile> profileRequest() {
711725
* @param redirectUri the uri to redirect after a successful request.
712726
* @return a request to configure and start
713727
*/
714-
public AuthenticationRequest token(String authorizationCode, String codeVerifier, String redirectUri) {
728+
public AuthenticationRequest tokenUsingCodeVerifier(String authorizationCode, String codeVerifier, String redirectUri) {
715729
Map<String, Object> parameters = ParameterBuilder.newBuilder()
716730
.setClientId(getClientId())
717731
.setGrantType(GRANT_TYPE_AUTHORIZATION_CODE)
@@ -728,4 +742,30 @@ public AuthenticationRequest token(String authorizationCode, String codeVerifier
728742
return factory.authenticationPOST(url, client, gson)
729743
.addAuthenticationParameters(parameters);
730744
}
745+
746+
/**
747+
* Fetch the token information from Auth0, using the authorization_code grant type
748+
*
749+
* @param authorizationCode the authorization code received from the /authorize call.
750+
* @param clientSecret the client secret used when requesting a code to /authorize.
751+
* @param redirectUri the uri to redirect after a successful request.
752+
* @return a request to configure and start
753+
*/
754+
public AuthenticationRequest tokenUsingClientSecret(final String authorizationCode, final String clientSecret, final String redirectUri) {
755+
final Map<String, Object> parameters = ParameterBuilder.newBuilder()
756+
.setClientId(getClientId())
757+
.setGrantType(GRANT_TYPE_AUTHORIZATION_CODE)
758+
.set(OAUTH_CODE_KEY, authorizationCode)
759+
.set(OAUTH_CLIENT_SECRET_KEY, clientSecret)
760+
.set(REDIRECT_URI_KEY, redirectUri)
761+
.asDictionary();
762+
763+
final HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
764+
.addPathSegment(OAUTH_PATH)
765+
.addPathSegment(TOKEN_PATH)
766+
.build();
767+
768+
return factory.authenticationPOST(url, client, gson)
769+
.addAuthenticationParameters(parameters);
770+
}
731771
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.auth0.authentication.result.DatabaseUser;
3232
import com.auth0.authentication.result.Delegation;
3333
import com.auth0.authentication.result.UserProfile;
34+
import com.auth0.request.ParameterizableRequest;
3435
import com.auth0.util.AuthenticationAPI;
3536
import com.auth0.util.MockBaseCallback;
3637
import com.google.gson.Gson;
@@ -1214,13 +1215,13 @@ public void shouldFetchProfileAfterLoginRequest() throws Exception {
12141215
}
12151216

12161217
@Test
1217-
public void shouldGetOAuthTokens() throws Exception {
1218+
public void shouldGetOAuthTokensUsingCodeVerifier() throws Exception {
12181219
mockAPI
12191220
.willReturnTokens()
12201221
.willReturnTokenInfo();
12211222

12221223
final MockBaseCallback<Credentials> callback = new MockBaseCallback<>();
1223-
client.token("code", "codeVerifier", "http://redirect.uri")
1224+
client.tokenUsingCodeVerifier("code", "codeVerifier", "http://redirect.uri")
12241225
.start(callback);
12251226

12261227
final RecordedRequest request = mockAPI.takeRequest();
@@ -1236,6 +1237,29 @@ public void shouldGetOAuthTokens() throws Exception {
12361237
assertThat(callback, hasPayloadOfType(Credentials.class));
12371238
}
12381239

1240+
@Test
1241+
public void shouldGetOAuthTokensUsingClientSecret() throws Exception {
1242+
mockAPI
1243+
.willReturnTokens()
1244+
.willReturnTokenInfo();
1245+
1246+
final MockBaseCallback<Credentials> callback = new MockBaseCallback<>();
1247+
client.tokenUsingClientSecret("code", "clientSecret", "http://redirect.uri")
1248+
.start(callback);
1249+
1250+
final RecordedRequest request = mockAPI.takeRequest();
1251+
assertThat(request.getPath(), equalTo("/oauth/token"));
1252+
1253+
Map<String, String> body = bodyFromRequest(request);
1254+
assertThat(body, hasEntry("grant_type", ParameterBuilder.GRANT_TYPE_AUTHORIZATION_CODE));
1255+
assertThat(body, hasEntry("client_id", CLIENT_ID));
1256+
assertThat(body, hasEntry("code", "code"));
1257+
assertThat(body, hasEntry("client_secret", "clientSecret"));
1258+
assertThat(body, hasEntry("redirect_uri", "http://redirect.uri"));
1259+
1260+
assertThat(callback, hasPayloadOfType(Credentials.class));
1261+
}
1262+
12391263
private Map<String, String> bodyFromRequest(RecordedRequest request) throws java.io.IOException {
12401264
final Type mapType = new TypeToken<Map<String, String>>() {
12411265
}.getType();

0 commit comments

Comments
 (0)