Search in sources :

Example 1 with SharedTokenCacheCredential

use of com.azure.identity.SharedTokenCacheCredential in project azure-maven-plugins by microsoft.

the class AzureAccount method restoreLogin.

private Mono<Account> restoreLogin(@Nonnull AccountEntity accountEntity) {
    Preconditions.checkNotNull(accountEntity.getEnvironment(), "Azure environment for account entity is required.");
    Preconditions.checkNotNull(accountEntity.getType(), "Auth type for account entity is required.");
    Account target;
    if (Arrays.asList(AuthType.DEVICE_CODE, AuthType.OAUTH2).contains(accountEntity.getType())) {
        AzureEnvironmentUtils.setupAzureEnvironment(accountEntity.getEnvironment());
        SharedTokenCacheCredentialBuilder builder = new SharedTokenCacheCredentialBuilder();
        SharedTokenCacheCredential credential = builder.tokenCachePersistenceOptions(new TokenCachePersistenceOptions().setName(Account.TOOLKIT_TOKEN_CACHE_NAME)).username(accountEntity.getEmail()).tenantId(accountEntity.getTenantIds() == null ? "organizations" : accountEntity.getTenantIds().get(0)).clientId(accountEntity.getClientId()).build();
        target = new SimpleAccount(accountEntity, credential);
    } else if (Arrays.asList(AuthType.VSCODE, AuthType.AZURE_CLI).contains(accountEntity.getType())) {
        target = buildAccountMap().get(accountEntity.getType()).get();
    } else {
        return Mono.error(new AzureToolkitAuthenticationException(String.format("login for auth type '%s' cannot be restored.", accountEntity.getType())));
    }
    return target.login().map(ac -> {
        if (ac.getEnvironment() != accountEntity.getEnvironment()) {
            throw new AzureToolkitAuthenticationException(String.format("you have changed the azure cloud to '%s' for auth type: '%s' since last time you signed in.", AzureEnvironmentUtils.getCloudNameForAzureCli(ac.getEnvironment()), accountEntity.getType()));
        }
        if (!StringUtils.equalsIgnoreCase(ac.entity.getEmail(), accountEntity.getEmail())) {
            throw new AzureToolkitAuthenticationException(String.format("you have changed the account from '%s' to '%s' since last time you signed in.", accountEntity.getEmail(), ac.entity.getEmail()));
        }
        return ac;
    }).doOnSuccess(this::setAccount);
}
Also used : SharedTokenCacheCredential(com.azure.identity.SharedTokenCacheCredential) Azure(com.microsoft.azure.toolkit.lib.Azure) Setter(lombok.Setter) Arrays(java.util.Arrays) AzureEnvironmentUtils(com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) Subscription(com.azure.resourcemanager.resources.models.Subscription) StringUtils(org.apache.commons.lang3.StringUtils) Supplier(java.util.function.Supplier) Region(com.microsoft.azure.toolkit.lib.common.model.Region) LinkedHashMap(java.util.LinkedHashMap) AzureCliAccount(com.microsoft.azure.toolkit.lib.auth.core.azurecli.AzureCliAccount) AzureEnvironment(com.azure.core.management.AzureEnvironment) AccessLevel(lombok.AccessLevel) Utils(com.microsoft.azure.toolkit.lib.common.utils.Utils) ServicePrincipalAccount(com.microsoft.azure.toolkit.lib.auth.core.serviceprincipal.ServicePrincipalAccount) Map(java.util.Map) SharedTokenCacheCredentialBuilder(com.azure.identity.SharedTokenCacheCredentialBuilder) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity) Location(com.azure.resourcemanager.resources.models.Location) Nonnull(javax.annotation.Nonnull) LoginFailureException(com.microsoft.azure.toolkit.lib.auth.exception.LoginFailureException) Collection(java.util.Collection) Mono(reactor.core.publisher.Mono) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) Collectors(java.util.stream.Collectors) RegionType(com.azure.resourcemanager.resources.models.RegionType) OAuthAccount(com.microsoft.azure.toolkit.lib.auth.core.oauth.OAuthAccount) Objects(java.util.Objects) Flux(reactor.core.publisher.Flux) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException) List(java.util.List) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) TokenCachePersistenceOptions(com.azure.identity.TokenCachePersistenceOptions) TokenCredential(com.azure.core.credential.TokenCredential) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) AzureCliAccount(com.microsoft.azure.toolkit.lib.auth.core.azurecli.AzureCliAccount) ServicePrincipalAccount(com.microsoft.azure.toolkit.lib.auth.core.serviceprincipal.ServicePrincipalAccount) OAuthAccount(com.microsoft.azure.toolkit.lib.auth.core.oauth.OAuthAccount) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) TokenCachePersistenceOptions(com.azure.identity.TokenCachePersistenceOptions) SharedTokenCacheCredentialBuilder(com.azure.identity.SharedTokenCacheCredentialBuilder) SharedTokenCacheCredential(com.azure.identity.SharedTokenCacheCredential) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Aggregations

TokenCredential (com.azure.core.credential.TokenCredential)1 AzureEnvironment (com.azure.core.management.AzureEnvironment)1 SharedTokenCacheCredential (com.azure.identity.SharedTokenCacheCredential)1 SharedTokenCacheCredentialBuilder (com.azure.identity.SharedTokenCacheCredentialBuilder)1 TokenCachePersistenceOptions (com.azure.identity.TokenCachePersistenceOptions)1 Location (com.azure.resourcemanager.resources.models.Location)1 RegionType (com.azure.resourcemanager.resources.models.RegionType)1 Subscription (com.azure.resourcemanager.resources.models.Subscription)1 Preconditions (com.google.common.base.Preconditions)1 Azure (com.microsoft.azure.toolkit.lib.Azure)1 IAzureAccount (com.microsoft.azure.toolkit.lib.account.IAzureAccount)1 AzureCliAccount (com.microsoft.azure.toolkit.lib.auth.core.azurecli.AzureCliAccount)1 DeviceCodeAccount (com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount)1 OAuthAccount (com.microsoft.azure.toolkit.lib.auth.core.oauth.OAuthAccount)1 ServicePrincipalAccount (com.microsoft.azure.toolkit.lib.auth.core.serviceprincipal.ServicePrincipalAccount)1 AzureToolkitAuthenticationException (com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)1 LoginFailureException (com.microsoft.azure.toolkit.lib.auth.exception.LoginFailureException)1 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)1 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)1 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)1