Skip to content

Commit 806cf35

Browse files
authored
Merge pull request #50 from auth0/add-revoke-token
Add revoke token endpoint
2 parents 65b95b8 + 42f2428 commit 806cf35

4 files changed

Lines changed: 94 additions & 4 deletions

File tree

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Creates a new request to exchange the `code` previously obtained by calling the
138138

139139
Example:
140140
```java
141-
AuthRequest request = exchangeCode("asdfgh", "https://me.auth0.com/callback")
141+
AuthRequest request = auth.exchangeCode("asdfgh", "https://me.auth0.com/callback")
142142
.setAudience("https://api.me.auth0.com/users")
143143
.setScope("openid contacts");
144144
try {
@@ -158,7 +158,7 @@ Creates a new request to log in the user with `username` and `password`. The con
158158

159159
Example:
160160
```java
161-
AuthRequest request = login("me@domain.com", "password123")
161+
AuthRequest request = auth.login("me@domain.com", "password123")
162162
.setAudience("https://api.me.auth0.com/users")
163163
.setScope("openid contacts");
164164
try {
@@ -178,7 +178,7 @@ Creates a new request to log in the user with `username` and `password` using th
178178

179179
Example:
180180
```java
181-
AuthRequest request = login("me@domain.com", "password123", "Username-Password-Authentication")
181+
AuthRequest request = auth.login("me@domain.com", "password123", "Username-Password-Authentication")
182182
.setAudience("https://api.me.auth0.com/users")
183183
.setScope("openid contacts");
184184
try {
@@ -198,7 +198,7 @@ Creates a new request to get a Token for the given Audience.
198198

199199
Example:
200200
```java
201-
AuthRequest request = requestToken("https://api.me.auth0.com/users")
201+
AuthRequest request = auth.requestToken("https://api.me.auth0.com/users")
202202
.setScope("openid contacts");
203203
try {
204204
TokenHolder holder = request.execute();
@@ -209,6 +209,24 @@ try {
209209
}
210210
```
211211

212+
### Revoke Refresh Token
213+
214+
Creates a new request to revoke an existing Refresh Token.
215+
216+
`Request<Void> revokeToken(String refreshToken)`
217+
218+
Example:
219+
```java
220+
Request<Void> request = auth.revokeToken("nisd1h9dks1doWJOsaf");
221+
try {
222+
request.execute();
223+
} catch (APIException exception) {
224+
// api error
225+
} catch (Auth0Exception exception) {
226+
// request error
227+
}
228+
```
229+
212230

213231
## Management API
214232

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ public class AuthAPI {
2424
private static final String KEY_AUDIENCE = "audience";
2525
private static final String KEY_EMAIL = "email";
2626
private static final String KEY_CONNECTION = "connection";
27+
private static final String KEY_TOKEN = "token";
2728

2829
private static final String PATH_OAUTH = "oauth";
2930
private static final String PATH_TOKEN = "token";
3031
private static final String PATH_DBCONNECTIONS = "dbconnections";
32+
private static final String PATH_REVOKE = "revoke";
3133

3234
private final OkHttpClient client;
3335
private final String clientId;
@@ -407,6 +409,39 @@ public AuthRequest requestToken(String audience) {
407409
return request;
408410
}
409411

412+
/**
413+
* Creates a new request to revoke an existing Refresh Token.
414+
* <pre>
415+
* {@code
416+
* AuthAPI auth = new AuthAPI("me.auth0.com", "B3c6RYhk1v9SbIJcRIOwu62gIUGsnze", "2679NfkaBn62e6w5E8zNEzjr-yWfkaBne");
417+
* try {
418+
* auth.revokeToken("ej2E8zNEzjrcSD2edjaE")
419+
* .execute();
420+
* } catch (Auth0Exception e) {
421+
* //Something happened
422+
* }
423+
* }
424+
* </pre>
425+
*
426+
* @param refreshToken the refresh token to revoke.
427+
* @return a Request to execute.
428+
*/
429+
public Request<Void> revokeToken(String refreshToken) {
430+
Asserts.assertNotNull(refreshToken, "refresh token");
431+
432+
String url = HttpUrl.parse(baseUrl)
433+
.newBuilder()
434+
.addPathSegment(PATH_OAUTH)
435+
.addPathSegment(PATH_REVOKE)
436+
.build()
437+
.toString();
438+
VoidRequest request = new VoidRequest(client, url, "POST");
439+
request.addParameter(KEY_CLIENT_ID, clientId);
440+
request.addParameter(KEY_CLIENT_SECRET, clientSecret);
441+
request.addParameter(KEY_TOKEN, refreshToken);
442+
return request;
443+
}
444+
410445
/**
411446
* Creates a new request to exchange the code obtained in the /authorize call using the 'Authorization Code' grant.
412447
* <pre>

src/test/java/com/auth0/client/MockServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public void textResponse(String path, int statusCode) throws IOException {
105105
server.enqueue(response);
106106
}
107107

108+
public void emptyResponse(int statusCode) throws IOException {
109+
MockResponse response = new MockResponse()
110+
.setResponseCode(statusCode);
111+
server.enqueue(response);
112+
}
113+
108114
public static Map<String, Object> bodyFromRequest(RecordedRequest request) throws IOException {
109115
ObjectMapper mapper = new ObjectMapper();
110116
MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,4 +711,35 @@ public void shouldCreateLogInWithClientCredentialsGrantRequest() throws Exceptio
711711
assertThat(response.getExpiresIn(), is(notNullValue()));
712712
}
713713

714+
715+
//Revoke a Token
716+
717+
718+
@Test
719+
public void shouldThrowOnRevokeTokenWithNullToken() throws Exception {
720+
exception.expect(IllegalArgumentException.class);
721+
exception.expectMessage("'refresh token' cannot be null!");
722+
api.revokeToken(null);
723+
}
724+
725+
@Test
726+
public void shouldCreateRevokeTokenRequest() throws Exception {
727+
Request<Void> request = api.revokeToken("2679NfkaBn62e6w5E8zNEzjr");
728+
assertThat(request, is(notNullValue()));
729+
730+
server.emptyResponse(200);
731+
Void response = request.execute();
732+
RecordedRequest recordedRequest = server.takeRequest();
733+
734+
assertThat(recordedRequest, hasMethodAndPath("POST", "/oauth/revoke"));
735+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
736+
737+
Map<String, Object> body = bodyFromRequest(recordedRequest);
738+
assertThat(body, hasEntry("client_id", (Object) CLIENT_ID));
739+
assertThat(body, hasEntry("client_secret", (Object) CLIENT_SECRET));
740+
assertThat(body, hasEntry("token", (Object) "2679NfkaBn62e6w5E8zNEzjr"));
741+
742+
assertThat(response, is(nullValue()));
743+
}
744+
714745
}

0 commit comments

Comments
 (0)