@@ -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}
0 commit comments