Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to update the Management API token #141

Merged
merged 3 commits into from
Jul 13, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awkward .... consider:

An expired token for an existing ManagementAPI instance can be replaced 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()`.
17 changes: 15 additions & 2 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
@@ -15,16 +15,17 @@
public class ManagementAPI {

private final HttpUrl baseUrl;
private final String apiToken;
private String apiToken;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if ManagementAPI is supposed to be thread safe, if yes then apiToken should have volatile modifier.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"when the token is about to expire or already has"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had doubts on this one 😛

* Please note you'll need to obtain the correspondent entity again for this to apply. e.g. call {@link #clients()} again.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"corresponding"

* 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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong but it looks like you've got these params backwards?

http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertNotNull(java.lang.Object)

I'm not sure if Asserts.assertNotNull() is the same one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm wrong ... Assert is a unit testing thing, Asserts is in this lib

Copy link
Contributor Author

@lbalmaceda lbalmaceda Jul 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Asserts is a helper class that throws an exception if a value is different than the expected. I think I mentioned this on one of my reviews to your PRs last week. It's basically to avoid boilerplate code.

if (value == null) {
   throw new IllegalArgumentException(String.format("'%s' cannot be null!", name));
}

BTW. At the top of the class you can see the imports and in this case, that I'm importing this lib Asserts class import com.auth0.utils.Asserts. Also in case the class is on the same package, there's no need to explicitly import it.

this.apiToken = apiToken;
}

/**
* Avoid sending Telemetry data in every request to the Auth0 servers.
*/
Original file line number Diff line number Diff line change
@@ -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);
}

/**
54 changes: 54 additions & 0 deletions src/test/java/com/auth0/client/mgmt/ManagementAPITest.java
Original file line number Diff line number Diff line change
@@ -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);