Skip to content

Commit

Permalink
Merge pull request #43 from auth0/refresh-token-via-token
Browse files Browse the repository at this point in the history
Get new Credentials using a Refresh Token
  • Loading branch information
hzalaz authored Nov 25, 2016
2 parents c32930d + 8b11a4a commit 957c3ca
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,41 @@ public DatabaseConnectionRequest<Void, AuthenticationException> resetPassword(@N
return new DatabaseConnectionRequest<>(request);
}

/**
* Requests new Credentials using a valid Refresh Token.
* Example usage:
* <pre><code>
* client.renewAuth("{refresh_token}")
* .addParameter("scope", "openid profile email")
* .start(new BaseCallback<Credentials>() {
* {@literal}Override
* public void onSuccess(Credentials payload) { }
*
* {@literal}@Override
* public void onFailure(AuthenticationException error) { }
* });
* </code></pre>
*
* @param refreshToken used to fetch the new Credentials.
* @return a request to start
*/
@SuppressWarnings("WeakerAccess")
public ParameterizableRequest<Credentials, AuthenticationException> renewAuth(@NonNull String refreshToken) {
final Map<String, Object> parameters = ParameterBuilder.newBuilder()
.setClientId(getClientId())
.setRefreshToken(refreshToken)
.setGrantType(ParameterBuilder.GRANT_TYPE_REFRESH_TOKEN)
.asDictionary();

HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(TOKEN_PATH)
.build();

return factory.POST(url, client, gson, Credentials.class, authErrorBuilder)
.addParameters(parameters);
}

/**
* Performs a <a href="https://auth0.com/docs/auth-api#!#post--delegation">delegation</a> request that will yield a new Auth0 'id_token'
* Example usage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*/
public class ParameterBuilder {

public static final String GRANT_TYPE_REFRESH_TOKEN = "refresh_token";
public static final String GRANT_TYPE_PASSWORD = "password";
public static final String GRANT_TYPE_JWT = "urn:ietf:params:oauth:grant-type:jwt-bearer";
public static final String GRANT_TYPE_AUTHORIZATION_CODE = "authorization_code";
Expand Down Expand Up @@ -131,6 +132,16 @@ public ParameterBuilder setAccessToken(String accessToken) {
return set(ACCESS_TOKEN_KEY, accessToken);
}

/**
* Sets the 'refresh_token' parameter
*
* @param refreshToken a access token
* @return itself
*/
public ParameterBuilder setRefreshToken(String refreshToken) {
return set(REFRESH_TOKEN_KEY, refreshToken);
}

/**
* Sets the 'send' parameter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,45 @@ public void shouldFetchProfileAfterLoginRequest() throws Exception {
assertThat(callback, hasPayloadOfType(Authentication.class));
}

@Test
public void shouldRenewAuth() throws Exception {
mockAPI.willReturnSuccessfulLogin();

final MockAuthenticationCallback<Credentials> callback = new MockAuthenticationCallback<>();
client.renewAuth("refreshToken")
.start(callback);

final RecordedRequest request = mockAPI.takeRequest();
assertThat(request.getHeader("Accept-Language"), is(getDefaultLocale()));
assertThat(request.getPath(), equalTo("/oauth/token"));

Map<String, String> body = bodyFromRequest(request);
assertThat(body, hasEntry("client_id", CLIENT_ID));
assertThat(body, hasEntry("refresh_token", "refreshToken"));
assertThat(body, hasEntry("grant_type", "refresh_token"));

assertThat(callback, hasPayloadOfType(Credentials.class));
}

@Test
public void shouldRenewAuthSync() throws Exception {
mockAPI.willReturnSuccessfulLogin();

Credentials credentials = client.renewAuth("refreshToken")
.execute();

final RecordedRequest request = mockAPI.takeRequest();
assertThat(request.getHeader("Accept-Language"), is(getDefaultLocale()));
assertThat(request.getPath(), equalTo("/oauth/token"));

Map<String, String> body = bodyFromRequest(request);
assertThat(body, hasEntry("client_id", CLIENT_ID));
assertThat(body, hasEntry("refresh_token", "refreshToken"));
assertThat(body, hasEntry("grant_type", "refresh_token"));

assertThat(credentials, is(notNullValue()));
}

@Test
public void shouldGetOAuthTokensUsingCodeVerifier() throws Exception {
mockAPI.willReturnTokens()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public void shouldSetDevice() throws Exception {
assertThat(parameters, hasEntry("device", DEVICE));
}

@Test
public void shouldSetRefreshToken() throws Exception {
Map<String, Object> parameters = builder.setRefreshToken(DEVICE).asDictionary();
assertThat(parameters, hasEntry("refresh_token", DEVICE));
}

@Test
public void shouldSetScopeWithOfflineAccess() throws Exception {
Map<String, Object> parameters = builder.setScope(ParameterBuilder.SCOPE_OFFLINE_ACCESS).asDictionary();
Expand Down

0 comments on commit 957c3ca

Please sign in to comment.