Skip to content

Commit d87e39c

Browse files
committed
Issue #669: Allow to create ManagementAPI with a custom TokenProvider
1 parent 8959fc1 commit d87e39c

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public static ManagementAPI.Builder newBuilder(String domain, String apiToken) {
6767
return new ManagementAPI.Builder(domain, apiToken);
6868
}
6969

70+
/**
71+
* Instantiate a new {@link Builder} to configure and build a new ManagementAPI client.
72+
*
73+
* @param domain the tenant's domain. Must be a non-null valid HTTPS domain.
74+
* @param tokenProvider an implementation of {@link TokenProvider}
75+
* @return a Builder for further configuration.
76+
*/
77+
public static ManagementAPI.Builder newBuilderForTokenProvider(String domain, TokenProvider tokenProvider) {
78+
return new ManagementAPI.Builder(domain, tokenProvider);
79+
}
80+
7081
private ManagementAPI(String domain, TokenProvider tokenProvider, Auth0HttpClient httpClient) {
7182
Asserts.assertNotNull(domain, "domain");
7283
Asserts.assertNotNull(tokenProvider, "token provider");
@@ -402,16 +413,30 @@ public SelfServiceProfilesEntity selfServiceProfiles() {
402413
public static class Builder {
403414
private final String domain;
404415
private final String apiToken;
416+
private final TokenProvider tokenProvider;
405417
private Auth0HttpClient httpClient = DefaultHttpClient.newBuilder().build();
406418

407419
/**
408420
* Create a new Builder
409421
* @param domain the domain of the tenant.
410422
* @param apiToken the API token used to make requests to the Auth0 Management API.
411423
*/
412-
public Builder(String domain, String apiToken) {
424+
private Builder(String domain, String apiToken) {
413425
this.domain = domain;
414426
this.apiToken = apiToken;
427+
this.tokenProvider = null;
428+
}
429+
430+
/**
431+
* Create a new Builder, which is based on an implementation of {@link TokenProvider}.
432+
* This allows for more flexibility, e.g. for transparent token renewal.
433+
* @param domain the domain of the tenant.
434+
* @param tokenProvider an implementation of {@link TokenProvider}
435+
*/
436+
private Builder(String domain, TokenProvider tokenProvider) {
437+
this.domain = domain;
438+
this.apiToken = null;
439+
this.tokenProvider = tokenProvider;
415440
}
416441

417442
/**
@@ -430,7 +455,14 @@ public Builder withHttpClient(Auth0HttpClient httpClient) {
430455
* @return the configured {@code ManagementAPI} instance.
431456
*/
432457
public ManagementAPI build() {
433-
return new ManagementAPI(domain, SimpleTokenProvider.create(apiToken), httpClient);
458+
checkState();
459+
return new ManagementAPI(domain, tokenProvider == null ? SimpleTokenProvider.create(apiToken) : tokenProvider, httpClient);
460+
}
461+
462+
private void checkState() {
463+
if((apiToken == null && tokenProvider == null) || (apiToken != null && tokenProvider != null)) {
464+
throw new IllegalArgumentException("Exactly one of 'apiToken' or 'tokenProvider' must be non-null for Builder.");
465+
}
434466
}
435467
}
436468
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void shouldThrowWhenDomainIsNull() {
9999
public void shouldThrowWhenApiTokenIsNull() {
100100
verifyThrows(IllegalArgumentException.class,
101101
() -> ManagementAPI.newBuilder(DOMAIN, null).build(),
102-
"'api token' cannot be null!");
102+
"Exactly one of 'apiToken' or 'tokenProvider' must be non-null for Builder.");
103103
}
104104

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

114+
@Test
115+
public void shouldThrowWhenTokenProviderIsNull() {
116+
verifyThrows(IllegalArgumentException.class,
117+
() -> ManagementAPI.newBuilderForTokenProvider(DOMAIN, null).build(),
118+
"Exactly one of 'apiToken' or 'tokenProvider' must be non-null for Builder.");
119+
}
120+
114121
@Test
115122
public void shouldUpdateApiToken() throws Auth0Exception {
116123
//Initialize with a token

0 commit comments

Comments
 (0)