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);
}
Aggregations