Skip to content

Commit 669c6c5

Browse files
committed
add renew authentication endpoint
1 parent 83ee983 commit 669c6c5

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ try {
226226
}
227227
```
228228

229+
### Renew Authentication
230+
231+
Creates a new request to renew the authentication and get fresh new credentials using a valid Refresh Token.
232+
233+
`AuthRequest renewAuth(String refreshToken)`
234+
235+
Example:
236+
```java
237+
AuthRequest request = auth.renewAuth("nisd1h9dks1doWJOsaf");
238+
try {
239+
TokenHolder holder = request.execute();
240+
} catch (APIException exception) {
241+
// api error
242+
} catch (Auth0Exception exception) {
243+
// request error
244+
}
245+
```
246+
229247

230248
## Management API
231249

src/main/java/com/auth0/client/auth/AuthAPI.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class AuthAPI {
2525
private static final String KEY_EMAIL = "email";
2626
private static final String KEY_CONNECTION = "connection";
2727
private static final String KEY_TOKEN = "token";
28+
private static final String KEY_REFRESH_TOKEN = "refresh_token";
2829

2930
private static final String PATH_OAUTH = "oauth";
3031
private static final String PATH_TOKEN = "token";
@@ -442,6 +443,41 @@ public Request<Void> revokeToken(String refreshToken) {
442443
return request;
443444
}
444445

446+
447+
/**
448+
* Creates a new request to renew the authentication and get fresh new credentials using a valid Refresh Token and the 'refresh_token' grant.
449+
* <pre>
450+
* {@code
451+
* AuthAPI auth = new AuthAPI("me.auth0.com", "B3c6RYhk1v9SbIJcRIOwu62gIUGsnze", "2679NfkaBn62e6w5E8zNEzjr-yWfkaBne");
452+
* try {
453+
* TokenHolder result = auth.renewAuth("ej2E8zNEzjrcSD2edjaE")
454+
* .execute();
455+
* } catch (Auth0Exception e) {
456+
* //Something happened
457+
* }
458+
* }
459+
* </pre>
460+
*
461+
* @param refreshToken the refresh token to use to get fresh new credentials.
462+
* @return a Request to configure and execute.
463+
*/
464+
public AuthRequest renewAuth(String refreshToken) {
465+
Asserts.assertNotNull(refreshToken, "refresh token");
466+
467+
String url = HttpUrl.parse(baseUrl)
468+
.newBuilder()
469+
.addPathSegment(PATH_OAUTH)
470+
.addPathSegment(PATH_TOKEN)
471+
.build()
472+
.toString();
473+
TokenRequest request = new TokenRequest(client, url);
474+
request.addParameter(KEY_CLIENT_ID, clientId);
475+
request.addParameter(KEY_CLIENT_SECRET, clientSecret);
476+
request.addParameter(KEY_GRANT_TYPE, "refresh_token");
477+
request.addParameter(KEY_REFRESH_TOKEN, refreshToken);
478+
return request;
479+
}
480+
445481
/**
446482
* Creates a new request to exchange the code obtained in the /authorize call using the 'Authorization Code' grant.
447483
* <pre>

src/test/java/com/auth0/client/auth/AuthAPITest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,6 @@ public void shouldCreateLogInWithClientCredentialsGrantRequest() throws Exceptio
714714

715715
//Revoke a Token
716716

717-
718717
@Test
719718
public void shouldThrowOnRevokeTokenWithNullToken() throws Exception {
720719
exception.expect(IllegalArgumentException.class);
@@ -742,4 +741,40 @@ public void shouldCreateRevokeTokenRequest() throws Exception {
742741
assertThat(response, is(nullValue()));
743742
}
744743

744+
745+
//Renew Authentication using Refresh Token
746+
747+
@Test
748+
public void shouldThrowOnRenewAuthWithNullRefreshToken() throws Exception {
749+
exception.expect(IllegalArgumentException.class);
750+
exception.expectMessage("'refresh token' cannot be null!");
751+
api.renewAuth(null);
752+
}
753+
754+
@Test
755+
public void shouldCreateRenewAuthRequest() throws Exception {
756+
AuthRequest request = api.renewAuth("ej2E8zNEzjrcSD2edjaE");
757+
assertThat(request, is(notNullValue()));
758+
759+
server.jsonResponse(AUTH_TOKENS, 200);
760+
TokenHolder response = request.execute();
761+
RecordedRequest recordedRequest = server.takeRequest();
762+
763+
assertThat(recordedRequest, hasMethodAndPath("POST", "/oauth/token"));
764+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
765+
766+
Map<String, Object> body = bodyFromRequest(recordedRequest);
767+
assertThat(body, hasEntry("grant_type", (Object) "refresh_token"));
768+
assertThat(body, hasEntry("client_id", (Object) CLIENT_ID));
769+
assertThat(body, hasEntry("client_secret", (Object) CLIENT_SECRET));
770+
assertThat(body, hasEntry("refresh_token", (Object) "ej2E8zNEzjrcSD2edjaE"));
771+
772+
assertThat(response, is(notNullValue()));
773+
assertThat(response.getAccessToken(), not(isEmptyOrNullString()));
774+
assertThat(response.getIdToken(), not(isEmptyOrNullString()));
775+
assertThat(response.getRefreshToken(), not(isEmptyOrNullString()));
776+
assertThat(response.getTokenType(), not(isEmptyOrNullString()));
777+
assertThat(response.getExpiresIn(), is(notNullValue()));
778+
}
779+
745780
}

0 commit comments

Comments
 (0)