Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity credential Bug Fixes #8765

Merged
merged 8 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package com.azure.identity;

import java.util.concurrent.ExecutorService;

/**
* The base class for credential builders that allow specifying a client ID and tenant ID for an Azure Active Directory.
* @param <T> the type of the credential builder
Expand All @@ -14,7 +16,7 @@ public abstract class AadCredentialBuilderBase<T extends AadCredentialBuilderBas
/**
* Specifies the Azure Active Directory endpoint to acquire tokens.
* @param authorityHost the Azure Active Directory endpoint
* @return itself
* @return An updated instance of this builder with the authority host set as specified.
*/
@SuppressWarnings("unchecked")
public T authorityHost(String authorityHost) {
Expand All @@ -26,7 +28,7 @@ public T authorityHost(String authorityHost) {
* Sets the client ID of the application.
*
* @param clientId the client ID of the application.
* @return itself
* @return An updated instance of this builder with the client id set as specified.
*/
@SuppressWarnings("unchecked")
public T clientId(String clientId) {
Expand All @@ -38,11 +40,24 @@ public T clientId(String clientId) {
* Sets the tenant ID of the application.
*
* @param tenantId the tenant ID of the application.
* @return itself
* @return An updated instance of this builder with the tenant id set as specified.
*/
@SuppressWarnings("unchecked")
public T tenantId(String tenantId) {
this.tenantId = tenantId;
return (T) this;
}

/**
* Specifies the ExecutorService to be used to execute the requests.
* Developer is responsible for maintaining the lifecycle of the ExecutorService.
*
* @param executorService the executor service to use for executing authentication requests.
* @return An updated instance of this builder with the executor service set as specified.
*/
@SuppressWarnings("unchecked")
public T executorService(ExecutorService executorService) {
this.identityClientOptions.setExecutorService(executorService);
return (T) this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,37 @@

package com.azure.identity;

import java.util.concurrent.ExecutorService;

/**
* Fluent credential builder for instantiating a {@link DefaultAzureCredential}.
*
* @see DefaultAzureCredential
*/
public class DefaultAzureCredentialBuilder extends CredentialBuilderBase<DefaultAzureCredentialBuilder> {

/**
* Specifies the Azure Active Directory endpoint to acquire tokens.
* @param authorityHost the Azure Active Directory endpoint
* @return An updated instance of this builder with the authority host set as specified.
*/
public DefaultAzureCredentialBuilder authorityHost(String authorityHost) {
this.identityClientOptions.setAuthorityHost(authorityHost);
return this;
}

/**
* Specifies the ExecutorService to be used to execute the requests.
* Developer is responsible for maintaining the lifecycle of the ExecutorService.
Copy link
Member

@srnagar srnagar Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does the developer shutDown() the ExecutorService? Could you please elaborate the documentation on how and when the user should use this and when to shut it down? Also, would be great to have some samples for this.

*
* @param executorService the executor service to use for executing authentication requests.
* @return An updated instance of this builder with the executor service set as specified.
*/
public DefaultAzureCredentialBuilder executorService(ExecutorService executorService) {
this.identityClientOptions.setExecutorService(executorService);
return this;
}

/**
* Creates new {@link DefaultAzureCredential} with the configured options set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package com.azure.identity;

import java.util.concurrent.ExecutorService;

/**
* Fluent credential builder for instantiating a {@link EnvironmentCredential}.
*
Expand All @@ -12,13 +14,25 @@ public class EnvironmentCredentialBuilder extends CredentialBuilderBase<Environm
/**
* Specifies the Azure Active Directory endpoint to acquire tokens.
* @param authorityHost the Azure Active Directory endpoint
* @return itself
* @return An updated instance of this builder with the authority host set as specified.
*/
public EnvironmentCredentialBuilder authorityHost(String authorityHost) {
this.identityClientOptions.setAuthorityHost(authorityHost);
return this;
}

/**
* Specifies the ExecutorService to be used to execute the requests.
* Developer is responsible for maintaining the lifecycle of the ExecutorService.
*
* @param executorService the executor service to use for executing authentication requests.
* @return An updated instance of this builder with the executor service set as specified.
*/
public EnvironmentCredentialBuilder executorService(ExecutorService executorService) {
this.identityClientOptions.setExecutorService(executorService);
return this;
}

/**
* Creates a new {@link EnvironmentCredential} with the current configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
if (pubClient == null) {
try {
PersistentTokenCacheAccessAspect accessAspect = new PersistentTokenCacheAccessAspect();
pubClient = PublicClientApplication.builder(this.clientId)
PublicClientApplication.Builder applicationBuilder = PublicClientApplication.builder(this.clientId);
if (options.getExecutorService() != null) {
applicationBuilder.executorService(options.getExecutorService());
}

pubClient = applicationBuilder
.authority(authorityUrl)
.setTokenCacheAccessAspect(accessAspect)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SharedTokenCacheCredentialBuilder extends AadCredentialBuilderBase<
* @param username The username for the account.
*
* @return The updated SharedTokenCacheCredentialBuilder object.
* */
*/
public SharedTokenCacheCredentialBuilder username(String username) {
this.username = username;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public class IdentityClient {
publicClientApplicationBuilder.httpClient(httpPipelineAdapter);
}
}

if (options.getExecutorService() != null) {
publicClientApplicationBuilder.executorService(options.getExecutorService());
}
this.publicClientApplication = publicClientApplicationBuilder.build();
}
}
Expand All @@ -144,6 +148,10 @@ public Mono<AccessToken> authenticateWithClientSecret(String clientSecret, Token
applicationBuilder.proxy(proxyOptionsToJavaNetProxy(options.getProxyOptions()));
}

if (options.getExecutorService() != null) {
applicationBuilder.executorService(options.getExecutorService());
}

ConfidentialClientApplication application = applicationBuilder.build();
return Mono.fromFuture(application.acquireToken(
ClientCredentialParameters.builder(new HashSet<>(request.getScopes()))
Expand Down Expand Up @@ -191,6 +199,10 @@ public Mono<AccessToken> authenticateWithPfxCertificate(String pfxCertificatePat
applicationBuilder.proxy(proxyOptionsToJavaNetProxy(options.getProxyOptions()));
}

if (options.getExecutorService() != null) {
applicationBuilder.executorService(options.getExecutorService());
}

return applicationBuilder.build();
}).flatMap(application -> Mono.fromFuture(application.acquireToken(
ClientCredentialParameters.builder(new HashSet<>(request.getScopes())).build())))
Expand Down Expand Up @@ -222,6 +234,10 @@ public Mono<AccessToken> authenticateWithPemCertificate(String pemCertificatePat
applicationBuilder.proxy(proxyOptionsToJavaNetProxy(options.getProxyOptions()));
}

if (options.getExecutorService() != null) {
applicationBuilder.executorService(options.getExecutorService());
}

ConfidentialClientApplication application = applicationBuilder.build();
return Mono.fromFuture(application.acquireToken(
ClientCredentialParameters.builder(new HashSet<>(request.getScopes()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.azure.core.http.ProxyOptions;

import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;

/**
Expand All @@ -22,6 +23,7 @@ public final class IdentityClientOptions {
private Function<Duration, Duration> retryTimeout;
private ProxyOptions proxyOptions;
private HttpPipeline httpPipeline;
private ExecutorService executorService;
private HttpClient httpClient;

/**
Expand Down Expand Up @@ -125,6 +127,25 @@ public IdentityClientOptions setHttpPipeline(HttpPipeline httpPipeline) {
return this;
}

/**
* Specifies the ExecutorService to be used to execute the requests.
* Developer is responsible for maintaining the lifecycle of the ExecutorService.
*
* @param executorService the executor service to use for executing authentication requests.
* @return IdentityClientOptions
*/
public IdentityClientOptions setExecutorService(ExecutorService executorService) {
this.executorService = executorService;
return this;
}

/**
* @return the ExecutorService to execute authentication requests.
*/
public ExecutorService getExecutorService() {
return executorService;
}

/**
* Specifies the HttpClient to send use for requests.
* @param httpClient the http client to use for requests
Expand Down