From b4398b8fe4e643b0c8c547a81d32fb900327ada2 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 12 Jul 2018 16:05:26 -0300 Subject: [PATCH 1/3] allow to update the MGMT Api token --- README.md | 3 ++ .../com/auth0/client/mgmt/ManagementAPI.java | 17 +++++- .../client/mgmt/ResourceServerEntity.java | 9 +--- .../auth0/client/mgmt/ManagementAPITest.java | 54 +++++++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fd1b2ba9..8f6c49af 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,9 @@ ManagementAPI mgmt = new ManagementAPI("{YOUR_DOMAIN}", holder.getAccessToken()) Click [here](https://auth0.com/docs/api/management/v2/tokens) for more information on how to obtain API Tokens. +In the event of token expiration a new one can be set to an existing `ManagementAPI` instance by calling the `setApiToken` method with the new token. + + 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. * **Blacklists:** See [Docs](https://auth0.com/docs/api/management/v2#!/Blacklists/get_tokens). Access the methods by calling `mgmt.blacklists()`. diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index 62aa7df6..06271851 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -15,16 +15,17 @@ public class ManagementAPI { private final HttpUrl baseUrl; - private final String apiToken; + private String apiToken; private final OkHttpClient client; private final TelemetryInterceptor telemetry; private final HttpLoggingInterceptor logging; /** * Create an instance with the given tenant's domain and API token. + * 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. * * @param domain the tenant's domain. - * @param apiToken the token to authenticate the calls with. See the "Getting an API token" section to learn how to obtain a token. + * @param apiToken the token to authenticate the calls with. */ public ManagementAPI(String domain, String apiToken) { Asserts.assertNotNull(domain, "domain"); @@ -45,6 +46,18 @@ public ManagementAPI(String domain, String apiToken) { .build(); } + /** + * Update the API token to use on new calls. This is useful when the token is about to expire or it already has. + * Please note you'll need to obtain the correspondent entity again for this to apply. e.g. call {@link #clients()} again. + * 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. + * + * @param apiToken the token to authenticate the calls with. + */ + public void setApiToken(String apiToken) { + Asserts.assertNotNull(apiToken, "api token"); + this.apiToken = apiToken; + } + /** * Avoid sending Telemetry data in every request to the Auth0 servers. */ diff --git a/src/main/java/com/auth0/client/mgmt/ResourceServerEntity.java b/src/main/java/com/auth0/client/mgmt/ResourceServerEntity.java index 76ded3f0..c1c748e8 100644 --- a/src/main/java/com/auth0/client/mgmt/ResourceServerEntity.java +++ b/src/main/java/com/auth0/client/mgmt/ResourceServerEntity.java @@ -17,15 +17,10 @@ /** * 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 */ -public class ResourceServerEntity { - private OkHttpClient client; - private HttpUrl baseUrl; - private String apiToken; +public class ResourceServerEntity extends BaseManagementEntity { ResourceServerEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) { - this.client = client; - this.baseUrl = baseUrl; - this.apiToken = apiToken; + super(client, baseUrl, apiToken); } /** diff --git a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java index 018e1120..4bf835eb 100644 --- a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java +++ b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java @@ -75,6 +75,60 @@ public void shouldThrowWhenApiTokenIsNull() throws Exception { new ManagementAPI(DOMAIN, null); } + @Test + public void shouldThrowOnUpdateWhenApiTokenIsNull() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("'api token' cannot be null!"); + new ManagementAPI(DOMAIN, null); + } + + @Test + public void shouldUpdateApiToken() throws Exception { + //Initialize with a token + ManagementAPI api = new ManagementAPI(DOMAIN, "first token"); + + assertThat(api.blacklists().apiToken, is("first token")); + assertThat(api.clientGrants().apiToken, is("first token")); + assertThat(api.clients().apiToken, is("first token")); + assertThat(api.connections().apiToken, is("first token")); + assertThat(api.deviceCredentials().apiToken, is("first token")); + assertThat(api.emailProvider().apiToken, is("first token")); + assertThat(api.emailTemplates().apiToken, is("first token")); + assertThat(api.grants().apiToken, is("first token")); + assertThat(api.guardian().apiToken, is("first token")); + assertThat(api.jobs().apiToken, is("first token")); + assertThat(api.logEvents().apiToken, is("first token")); + assertThat(api.resourceServers().apiToken, is("first token")); + assertThat(api.rules().apiToken, is("first token")); + assertThat(api.stats().apiToken, is("first token")); + assertThat(api.tenants().apiToken, is("first token")); + assertThat(api.tickets().apiToken, is("first token")); + assertThat(api.userBlocks().apiToken, is("first token")); + assertThat(api.users().apiToken, is("first token")); + + //Update the token + api.setApiToken("new token"); + + assertThat(api.blacklists().apiToken, is("new token")); + assertThat(api.clientGrants().apiToken, is("new token")); + assertThat(api.clients().apiToken, is("new token")); + assertThat(api.connections().apiToken, is("new token")); + assertThat(api.deviceCredentials().apiToken, is("new token")); + assertThat(api.emailProvider().apiToken, is("new token")); + assertThat(api.emailTemplates().apiToken, is("new token")); + assertThat(api.grants().apiToken, is("new token")); + assertThat(api.guardian().apiToken, is("new token")); + assertThat(api.jobs().apiToken, is("new token")); + assertThat(api.logEvents().apiToken, is("new token")); + assertThat(api.resourceServers().apiToken, is("new token")); + assertThat(api.rules().apiToken, is("new token")); + assertThat(api.stats().apiToken, is("new token")); + assertThat(api.tenants().apiToken, is("new token")); + assertThat(api.tickets().apiToken, is("new token")); + assertThat(api.userBlocks().apiToken, is("new token")); + assertThat(api.users().apiToken, is("new token")); + } + @Test public void shouldAddAndEnableTelemetryInterceptor() throws Exception { ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN); From 6bc0ae11e4dea35590d42b28507558d5cfaee64b Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 12 Jul 2018 18:11:40 -0300 Subject: [PATCH 2/3] update grammar --- README.md | 6 +++--- src/main/java/com/auth0/client/mgmt/ManagementAPI.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8f6c49af..84824886 100644 --- a/README.md +++ b/README.md @@ -264,11 +264,11 @@ TokenHolder holder = authRequest.execute(); ManagementAPI mgmt = new ManagementAPI("{YOUR_DOMAIN}", holder.getAccessToken()); ``` -(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). +(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). -Click [here](https://auth0.com/docs/api/management/v2/tokens) for more information on how to obtain API Tokens. +An expired token for an existing `ManagementAPI` instance can be replaced by calling the `setApiToken` method with the new token. -In the event of token expiration a new one can be set to an existing `ManagementAPI` instance by calling the `setApiToken` method with the new token. +Click [here](https://auth0.com/docs/api/management/v2/tokens) for more information on how to obtain API Tokens. 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. diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index 06271851..85c8d3ad 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -47,8 +47,8 @@ public ManagementAPI(String domain, String apiToken) { } /** - * Update the API token to use on new calls. This is useful when the token is about to expire or it already has. - * Please note you'll need to obtain the correspondent entity again for this to apply. e.g. call {@link #clients()} again. + * Update the API token to use on new calls. This is useful when the token is about to expire or already has. + * Please note you'll need to obtain the corresponding entity again for this to apply. e.g. call {@link #clients()} again. * 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. * * @param apiToken the token to authenticate the calls with. From b7edcb712340f39ce8edfc147ad330a04c91560f Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 13 Jul 2018 16:10:46 -0300 Subject: [PATCH 3/3] fix test logic --- src/test/java/com/auth0/client/mgmt/ManagementAPITest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java index 4bf835eb..0a311755 100644 --- a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java +++ b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java @@ -77,9 +77,11 @@ public void shouldThrowWhenApiTokenIsNull() throws Exception { @Test public void shouldThrowOnUpdateWhenApiTokenIsNull() throws Exception { + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN); + exception.expect(IllegalArgumentException.class); exception.expectMessage("'api token' cannot be null!"); - new ManagementAPI(DOMAIN, null); + api.setApiToken(null); } @Test