Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -52,7 +52,7 @@ public class AuthorizationCodeCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
@Immutable
public class ClientCertificateCredential implements TokenCredential {
private final String clientCertificate;
private final String clientCertificatePassword;
private final IdentityClient identityClient;

/**
Expand All @@ -40,22 +38,19 @@ public class ClientCertificateCredential implements TokenCredential {
ClientCertificateCredential(String tenantId, String clientId, String certificatePath, String certificatePassword,
IdentityClientOptions identityClientOptions) {
Objects.requireNonNull(certificatePath, "'certificatePath' cannot be null.");
this.clientCertificate = certificatePath;
this.clientCertificatePassword = certificatePassword;
identityClient =
new IdentityClientBuilder()
.tenantId(tenantId)
.clientId(clientId)
.identityClientOptions(identityClientOptions)
.build();
identityClient = new IdentityClientBuilder()
.tenantId(tenantId)
.clientId(clientId)
.certificatePath(certificatePath)
.certificatePassword(certificatePassword)
.identityClientOptions(identityClientOptions)
.build();
}

@Override
public Mono<AccessToken> getToken(TokenRequestContext request) {
if (clientCertificatePassword != null) {
return identityClient.authenticateWithPfxCertificate(clientCertificate, clientCertificatePassword, request);
} else {
return identityClient.authenticateWithPemCertificate(clientCertificate, request);
}
return identityClient.authenticateWithConfidentialClientCache(request)
.onErrorResume(t -> Mono.empty())
.switchIfEmpty(Mono.defer(() -> identityClient.authenticateWithConfidentialClient(request)));
Copy link
Member

Choose a reason for hiding this comment

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

Is Mono.defer() necessary? You can simply do switchIfEmpty(identityClient.authenticateWithConfidentialClient(request)) since this gets triggered only if the cache returns empty result and doesn't have to be deferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alzimmermsft asked the same question. if we do switchIfEmpty(identityClient.authenticateWithConfidentialClient(request)) the method call identityClient.authenticateWithConfidentialClient(request) will be evaluated first but we do not want that.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ClientCertificateCredentialBuilder extends AadCredentialBuilderBase
* Sets the client certificate for authenticating to AAD.
*
* @param certificatePath the PEM file containing the certificate
* @return the ClientCertificateCredentialBuilder itself
* @return An updated instance of this builder.
*/
public ClientCertificateCredentialBuilder pemCertificate(String certificatePath) {
this.clientCertificate = certificatePath;
Expand All @@ -32,14 +32,26 @@ public ClientCertificateCredentialBuilder pemCertificate(String certificatePath)
*
* @param certificatePath the password protected PFX file containing the certificate
* @param clientCertificatePassword the password protecting the PFX file
* @return the ClientCertificateCredentialBuilder itself
* @return An updated instance of this builder.
*/
public ClientCertificateCredentialBuilder pfxCertificate(String certificatePath, String clientCertificatePassword) {
this.clientCertificate = certificatePath;
this.clientCertificatePassword = clientCertificatePassword;
return this;
}

/**
* Sets whether to enable using the shared token cache. This is disabled by default.
*
* @param enabled indicates whether to enable using the shared token cache.
*
* @return An updated instance of this builder.
*/
public ClientCertificateCredentialBuilder enablePersistentCache(boolean enabled) {
this.identityClientOptions.enablePersistentCache(enabled);
return this;
}

/**
* Creates a new {@link ClientCertificateCredential} with the current configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
@Immutable
public class ClientSecretCredential implements TokenCredential {
/* The client secret value. */
private final String clientSecret;
private final IdentityClient identityClient;

/**
Expand All @@ -44,13 +42,15 @@ public class ClientSecretCredential implements TokenCredential {
identityClient = new IdentityClientBuilder()
.tenantId(tenantId)
.clientId(clientId)
.clientSecret(clientSecret)
.identityClientOptions(identityClientOptions)
.build();
this.clientSecret = clientSecret;
}

@Override
public Mono<AccessToken> getToken(TokenRequestContext request) {
return identityClient.authenticateWithClientSecret(clientSecret, request);
return identityClient.authenticateWithConfidentialClientCache(request)
.onErrorResume(t -> Mono.empty())
.switchIfEmpty(Mono.defer(() -> identityClient.authenticateWithConfidentialClient(request)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ public class ClientSecretCredentialBuilder extends AadCredentialBuilderBase<Clie
/**
* Sets the client secret for the authentication.
* @param clientSecret the secret value of the AAD application.
* @return the ClientSecretCredentialBuilder itself
* @return An updated instance of this builder.
*/
public ClientSecretCredentialBuilder clientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
}

/**
* Sets whether to enable using the shared token cache. This is disabled by default.
*
* @param enabled indicates whether to enable using the shared token cache.
*
* @return An updated instance of this builder.
*/
public ClientSecretCredentialBuilder enablePersistentCache(boolean enabled) {
this.identityClientOptions.enablePersistentCache(enabled);
return this;
}

/**
* Creates a new {@link ClientCertificateCredential} with the current configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DeviceCodeCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class IntelliJCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class InteractiveBrowserCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class SharedTokenCacheCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class UsernamePasswordCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class VisualStudioCodeCredential implements TokenCredential {
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithMsalAccount(request, cachedToken.get().getAccount())
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get().getAccount())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
Expand Down
Loading