Skip to content

Commit 9a4890c

Browse files
authored
Merge pull request #141 from auth0/allow-to-update-token
Allow to update the Management API token
2 parents 7a56911 + b7edcb7 commit 9a4890c

4 files changed

Lines changed: 77 additions & 10 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,13 @@ TokenHolder holder = authRequest.execute();
264264
ManagementAPI mgmt = new ManagementAPI("{YOUR_DOMAIN}", holder.getAccessToken());
265265
```
266266

267-
(Note that the simplified should have error handling, and ideally cache the obtained token until it expires instead of requesting one access token for each Management API v2 invocation).
267+
(Note that the snippet above should have error handling, and ideally cache the obtained token until it expires instead of requesting one access token for each Management API v2 invocation).
268+
269+
An expired token for an existing `ManagementAPI` instance can be replaced by calling the `setApiToken` method with the new token.
268270

269271
Click [here](https://auth0.com/docs/api/management/v2/tokens) for more information on how to obtain API Tokens.
270272

273+
271274
The Management API is divided into different entities. Each of them have the list, create, update, delete and update methods plus a few more if corresponds. The calls are authenticated using the API Token given in the `ManagementAPI` instance creation and must contain the `scope` required by each entity. See the javadoc for details on which `scope` is expected for each call.
272275

273276
* **Blacklists:** See [Docs](https://auth0.com/docs/api/management/v2#!/Blacklists/get_tokens). Access the methods by calling `mgmt.blacklists()`.

src/main/java/com/auth0/client/mgmt/ManagementAPI.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
public class ManagementAPI {
1616

1717
private final HttpUrl baseUrl;
18-
private final String apiToken;
18+
private String apiToken;
1919
private final OkHttpClient client;
2020
private final TelemetryInterceptor telemetry;
2121
private final HttpLoggingInterceptor logging;
2222

2323
/**
2424
* Create an instance with the given tenant's domain and API token.
25+
* See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens to learn how to obtain a token.
2526
*
2627
* @param domain the tenant's domain.
27-
* @param apiToken the token to authenticate the calls with. See the "Getting an API token" section to learn how to obtain a token.
28+
* @param apiToken the token to authenticate the calls with.
2829
*/
2930
public ManagementAPI(String domain, String apiToken) {
3031
Asserts.assertNotNull(domain, "domain");
@@ -45,6 +46,18 @@ public ManagementAPI(String domain, String apiToken) {
4546
.build();
4647
}
4748

49+
/**
50+
* Update the API token to use on new calls. This is useful when the token is about to expire or already has.
51+
* Please note you'll need to obtain the corresponding entity again for this to apply. e.g. call {@link #clients()} again.
52+
* See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens to learn how to obtain a token.
53+
*
54+
* @param apiToken the token to authenticate the calls with.
55+
*/
56+
public void setApiToken(String apiToken) {
57+
Asserts.assertNotNull(apiToken, "api token");
58+
this.apiToken = apiToken;
59+
}
60+
4861
/**
4962
* Avoid sending Telemetry data in every request to the Auth0 servers.
5063
*/

src/main/java/com/auth0/client/mgmt/ResourceServerEntity.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
/**
1818
* Class that provides an implementation of the Resource Server methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Resource_Servers
1919
*/
20-
public class ResourceServerEntity {
21-
private OkHttpClient client;
22-
private HttpUrl baseUrl;
23-
private String apiToken;
20+
public class ResourceServerEntity extends BaseManagementEntity {
2421

2522
ResourceServerEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
26-
this.client = client;
27-
this.baseUrl = baseUrl;
28-
this.apiToken = apiToken;
23+
super(client, baseUrl, apiToken);
2924
}
3025

3126
/**

src/test/java/com/auth0/client/mgmt/ManagementAPITest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,62 @@ public void shouldThrowWhenApiTokenIsNull() throws Exception {
7575
new ManagementAPI(DOMAIN, null);
7676
}
7777

78+
@Test
79+
public void shouldThrowOnUpdateWhenApiTokenIsNull() throws Exception {
80+
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
81+
82+
exception.expect(IllegalArgumentException.class);
83+
exception.expectMessage("'api token' cannot be null!");
84+
api.setApiToken(null);
85+
}
86+
87+
@Test
88+
public void shouldUpdateApiToken() throws Exception {
89+
//Initialize with a token
90+
ManagementAPI api = new ManagementAPI(DOMAIN, "first token");
91+
92+
assertThat(api.blacklists().apiToken, is("first token"));
93+
assertThat(api.clientGrants().apiToken, is("first token"));
94+
assertThat(api.clients().apiToken, is("first token"));
95+
assertThat(api.connections().apiToken, is("first token"));
96+
assertThat(api.deviceCredentials().apiToken, is("first token"));
97+
assertThat(api.emailProvider().apiToken, is("first token"));
98+
assertThat(api.emailTemplates().apiToken, is("first token"));
99+
assertThat(api.grants().apiToken, is("first token"));
100+
assertThat(api.guardian().apiToken, is("first token"));
101+
assertThat(api.jobs().apiToken, is("first token"));
102+
assertThat(api.logEvents().apiToken, is("first token"));
103+
assertThat(api.resourceServers().apiToken, is("first token"));
104+
assertThat(api.rules().apiToken, is("first token"));
105+
assertThat(api.stats().apiToken, is("first token"));
106+
assertThat(api.tenants().apiToken, is("first token"));
107+
assertThat(api.tickets().apiToken, is("first token"));
108+
assertThat(api.userBlocks().apiToken, is("first token"));
109+
assertThat(api.users().apiToken, is("first token"));
110+
111+
//Update the token
112+
api.setApiToken("new token");
113+
114+
assertThat(api.blacklists().apiToken, is("new token"));
115+
assertThat(api.clientGrants().apiToken, is("new token"));
116+
assertThat(api.clients().apiToken, is("new token"));
117+
assertThat(api.connections().apiToken, is("new token"));
118+
assertThat(api.deviceCredentials().apiToken, is("new token"));
119+
assertThat(api.emailProvider().apiToken, is("new token"));
120+
assertThat(api.emailTemplates().apiToken, is("new token"));
121+
assertThat(api.grants().apiToken, is("new token"));
122+
assertThat(api.guardian().apiToken, is("new token"));
123+
assertThat(api.jobs().apiToken, is("new token"));
124+
assertThat(api.logEvents().apiToken, is("new token"));
125+
assertThat(api.resourceServers().apiToken, is("new token"));
126+
assertThat(api.rules().apiToken, is("new token"));
127+
assertThat(api.stats().apiToken, is("new token"));
128+
assertThat(api.tenants().apiToken, is("new token"));
129+
assertThat(api.tickets().apiToken, is("new token"));
130+
assertThat(api.userBlocks().apiToken, is("new token"));
131+
assertThat(api.users().apiToken, is("new token"));
132+
}
133+
78134
@Test
79135
public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
80136
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);

0 commit comments

Comments
 (0)