
com.azure.identity.ClientSecretCredential Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-identity Show documentation
Show all versions of azure-identity Show documentation
This module contains client library for Microsoft Azure Identity.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.identity;
import com.azure.core.annotation.Immutable;
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.implementation.IdentityClient;
import com.azure.identity.implementation.IdentityClientBuilder;
import com.azure.identity.implementation.IdentityClientOptions;
import com.azure.identity.implementation.IdentitySyncClient;
import com.azure.identity.implementation.util.LoggingUtil;
import reactor.core.publisher.Mono;
import java.util.Objects;
/**
* An AAD credential that acquires a token with a client secret for an AAD application.
*
* Sample: Construct a simple ClientSecretCredential
*
*
* ClientSecretCredential credential1 = new ClientSecretCredentialBuilder()
* .tenantId(tenantId)
* .clientId(clientId)
* .clientSecret(clientSecret)
* .build();
*
*
*
* Sample: Construct a ClientSecretCredential behind a proxy
*
*
* ClientSecretCredential credential2 = new ClientSecretCredentialBuilder()
* .tenantId(tenantId)
* .clientId(clientId)
* .clientSecret(clientSecret)
* .proxyOptions(new ProxyOptions(Type.HTTP, new InetSocketAddress("10.21.32.43", 5465)))
* .build();
*
*
*/
@Immutable
public class ClientSecretCredential implements TokenCredential {
private static final ClientLogger LOGGER = new ClientLogger(ClientSecretCredential.class);
private final IdentityClient identityClient;
private final IdentitySyncClient identitySyncClient;
/**
* Creates a ClientSecretCredential with the given identity client options.
*
* @param tenantId the tenant ID of the application
* @param clientId the client ID of the application
* @param clientSecret the secret value of the AAD application.
* @param identityClientOptions the options for configuring the identity client
*/
ClientSecretCredential(String tenantId, String clientId, String clientSecret,
IdentityClientOptions identityClientOptions) {
Objects.requireNonNull(clientSecret, "'clientSecret' cannot be null.");
Objects.requireNonNull(identityClientOptions, "'identityClientOptions' cannot be null.");
IdentityClientBuilder builder = new IdentityClientBuilder()
.tenantId(tenantId)
.clientId(clientId)
.clientSecret(clientSecret)
.identityClientOptions(identityClientOptions);
identityClient = builder.build();
identitySyncClient = builder.buildSyncClient();
}
@Override
public Mono getToken(TokenRequestContext request) {
return identityClient.authenticateWithConfidentialClientCache(request)
.onErrorResume(t -> Mono.empty())
.switchIfEmpty(Mono.defer(() -> identityClient.authenticateWithConfidentialClient(request)))
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request,
error));
}
@Override
public AccessToken getTokenSync(TokenRequestContext request) {
try {
AccessToken token = identitySyncClient.authenticateWithConfidentialClientCache(request);
LoggingUtil.logTokenSuccess(LOGGER, request);
return token;
} catch (Exception e) { }
try {
AccessToken token = identitySyncClient.authenticateWithConfidentialClient(request);
LoggingUtil.logTokenSuccess(LOGGER, request);
return token;
} catch (Exception e) {
LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, e);
throw e;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy