Skip to content

Commit 78edccb

Browse files
tanya732krrrr38
andauthored
Customize management api token provider (#748)
Co-authored-by: krrrr38 <k.kaizu38@gmail.com>
1 parent a3b6219 commit 78edccb

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
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: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void shouldCreateWithDomainAndToken() {
5454
}
5555

5656
@Test
57-
public void shouldCreateWithHttpClient() {
57+
public void shouldCreateWithHttpClientWithApiToken() {
5858
Auth0HttpClient httpClient = new Auth0HttpClient() {
5959
@Override
6060
public Auth0HttpResponse sendRequest(Auth0HttpRequest request) {
@@ -69,6 +69,31 @@ public CompletableFuture<Auth0HttpResponse> sendRequestAsync(Auth0HttpRequest re
6969

7070
ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN)
7171
.withHttpClient(httpClient).build();
72+
73+
assertThat(api, is(notNullValue()));
74+
assertThat(api.getHttpClient(), is(httpClient));
75+
}
76+
77+
@Test
78+
public void shouldCreateWithHttpClientWithTokenProvider() {
79+
Auth0HttpClient httpClient = new Auth0HttpClient() {
80+
@Override
81+
public Auth0HttpResponse sendRequest(Auth0HttpRequest request) {
82+
return null;
83+
}
84+
85+
@Override
86+
public CompletableFuture<Auth0HttpResponse> sendRequestAsync(Auth0HttpRequest request) {
87+
return null;
88+
}
89+
};
90+
91+
ManagementAPI api = ManagementAPI.newBuilder(
92+
DOMAIN,
93+
SimpleTokenProvider.create(API_TOKEN)
94+
)
95+
.withHttpClient(httpClient)
96+
.build();
7297
assertThat(api, is(notNullValue()));
7398
assertThat(api.getHttpClient(), is(httpClient));
7499
}
@@ -98,10 +123,17 @@ public void shouldThrowWhenDomainIsNull() {
98123
@Test
99124
public void shouldThrowWhenApiTokenIsNull() {
100125
verifyThrows(IllegalArgumentException.class,
101-
() -> ManagementAPI.newBuilder(DOMAIN, null).build(),
126+
() -> ManagementAPI.newBuilder(DOMAIN, (String) null).build(),
102127
"'api token' cannot be null!");
103128
}
104129

130+
@Test
131+
public void shouldThrowWhenTokenProviderIsNull() {
132+
verifyThrows(IllegalArgumentException.class,
133+
() -> ManagementAPI.newBuilder(DOMAIN, (TokenProvider) null).build(),
134+
"'token provider' cannot be null!");
135+
}
136+
105137
@Test
106138
public void shouldThrowOnUpdateWhenApiTokenIsNull() {
107139
ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN).build();

0 commit comments

Comments
 (0)