Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 34 additions & 2 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public static ManagementAPI.Builder newBuilder(String domain, String apiToken) {
return new ManagementAPI.Builder(domain, apiToken);
}

/**
* Instantiate a new {@link Builder} to configure and build a new ManagementAPI client.
*
* @param domain the tenant's domain. Must be a non-null valid HTTPS domain.
* @param tokenProvider an implementation of {@link TokenProvider}
* @return a Builder for further configuration.
*/
public static ManagementAPI.Builder newBuilderForTokenProvider(String domain, TokenProvider tokenProvider) {
return new ManagementAPI.Builder(domain, tokenProvider);
}

private ManagementAPI(String domain, TokenProvider tokenProvider, Auth0HttpClient httpClient) {
Asserts.assertNotNull(domain, "domain");
Asserts.assertNotNull(tokenProvider, "token provider");
Expand Down Expand Up @@ -402,16 +413,30 @@ public SelfServiceProfilesEntity selfServiceProfiles() {
public static class Builder {
private final String domain;
private final String apiToken;
private final TokenProvider tokenProvider;
private Auth0HttpClient httpClient = DefaultHttpClient.newBuilder().build();

/**
* Create a new Builder
* @param domain the domain of the tenant.
* @param apiToken the API token used to make requests to the Auth0 Management API.
*/
public Builder(String domain, String apiToken) {
private Builder(String domain, String apiToken) {
this.domain = domain;
this.apiToken = apiToken;
this.tokenProvider = null;
}

/**
* Create a new Builder, which is based on an implementation of {@link TokenProvider}.
* This allows for more flexibility, e.g. for transparent token renewal.
* @param domain the domain of the tenant.
* @param tokenProvider an implementation of {@link TokenProvider}
*/
private Builder(String domain, TokenProvider tokenProvider) {
this.domain = domain;
this.apiToken = null;
this.tokenProvider = tokenProvider;
}

/**
Expand All @@ -430,7 +455,14 @@ public Builder withHttpClient(Auth0HttpClient httpClient) {
* @return the configured {@code ManagementAPI} instance.
*/
public ManagementAPI build() {
return new ManagementAPI(domain, SimpleTokenProvider.create(apiToken), httpClient);
checkState();
return new ManagementAPI(domain, tokenProvider == null ? SimpleTokenProvider.create(apiToken) : tokenProvider, httpClient);
}

private void checkState() {
if((apiToken == null && tokenProvider == null) || (apiToken != null && tokenProvider != null)) {
throw new IllegalArgumentException("Exactly one of 'apiToken' or 'tokenProvider' must be non-null for Builder.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void shouldThrowWhenDomainIsNull() {
public void shouldThrowWhenApiTokenIsNull() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilder(DOMAIN, null).build(),
"'api token' cannot be null!");
"Exactly one of 'apiToken' or 'tokenProvider' must be non-null for Builder.");
}

@Test
Expand All @@ -111,6 +111,13 @@ public void shouldThrowOnUpdateWhenApiTokenIsNull() {
"'api token' cannot be null!");
}

@Test
public void shouldThrowWhenTokenProviderIsNull() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilderForTokenProvider(DOMAIN, null).build(),
"Exactly one of 'apiToken' or 'tokenProvider' must be non-null for Builder.");
}

@Test
public void shouldUpdateApiToken() throws Auth0Exception {
//Initialize with a token
Expand Down
Loading