Skip to content

Commit 8bf99b3

Browse files
committed
Added ManagementAPI token provider customization
1 parent a3b6219 commit 8bf99b3

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

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

Lines changed: 23 additions & 3 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 the API Token provider to use when making requests.
75+
* @return a Builder for further configuration.
76+
*/
77+
public static ManagementAPI.Builder newBuilder(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");
@@ -409,7 +420,7 @@ public NetworkAclsEntity networkAcls() {
409420
*/
410421
public static class Builder {
411422
private final String domain;
412-
private final String apiToken;
423+
private final TokenProvider tokenProvider;
413424
private Auth0HttpClient httpClient = DefaultHttpClient.newBuilder().build();
414425

415426
/**
@@ -418,8 +429,17 @@ public static class Builder {
418429
* @param apiToken the API token used to make requests to the Auth0 Management API.
419430
*/
420431
public Builder(String domain, String apiToken) {
432+
this(domain, SimpleTokenProvider.create(apiToken));
433+
}
434+
435+
/**
436+
* Create a new Builder
437+
* @param domain the domain of the tenant.
438+
* @param tokenProvider the API Token provider to use when making requests.
439+
*/
440+
public Builder(String domain, TokenProvider tokenProvider) {
421441
this.domain = domain;
422-
this.apiToken = apiToken;
442+
this.tokenProvider = tokenProvider;
423443
}
424444

425445
/**
@@ -438,7 +458,7 @@ public Builder withHttpClient(Auth0HttpClient httpClient) {
438458
* @return the configured {@code ManagementAPI} instance.
439459
*/
440460
public ManagementAPI build() {
441-
return new ManagementAPI(domain, SimpleTokenProvider.create(apiToken), httpClient);
461+
return new ManagementAPI(domain, tokenProvider, httpClient);
442462
}
443463
}
444464
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ public CompletableFuture<Auth0HttpResponse> sendRequestAsync(Auth0HttpRequest re
6767
}
6868
};
6969

70-
ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN)
71-
.withHttpClient(httpClient).build();
70+
ManagementAPI api = ManagementAPI.newBuilder(
71+
DOMAIN,
72+
SimpleTokenProvider.create(API_TOKEN)
73+
)
74+
.withHttpClient(httpClient)
75+
.build();
7276
assertThat(api, is(notNullValue()));
7377
assertThat(api.getHttpClient(), is(httpClient));
7478
}
@@ -98,7 +102,7 @@ public void shouldThrowWhenDomainIsNull() {
98102
@Test
99103
public void shouldThrowWhenApiTokenIsNull() {
100104
verifyThrows(IllegalArgumentException.class,
101-
() -> ManagementAPI.newBuilder(DOMAIN, null).build(),
105+
() -> ManagementAPI.newBuilder(DOMAIN, (String) null).build(),
102106
"'api token' cannot be null!");
103107
}
104108

0 commit comments

Comments
 (0)