use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-gradle-plugins by microsoft.
the class GradleAuthHelper method login.
public static String login(GradleAuthConfig auth, String subscriptionId) {
try {
Account account = login(toAuthConfiguration(ObjectUtils.firstNonNull(auth, new GradleAuthConfig())));
final List<Subscription> subscriptions = account.getSubscriptions();
final String targetSubscriptionId = getTargetSubscriptionId(subscriptionId, subscriptions, account.getSelectedSubscriptions());
checkSubscription(subscriptions, targetSubscriptionId);
Azure.az(AzureAccount.class).account().selectSubscription(Collections.singletonList(targetSubscriptionId));
printCurrentSubscription(account);
return targetSubscriptionId;
} catch (InvalidConfigurationException e) {
throw new AzureToolkitAuthenticationException("Failed to authenticate with Azure due to error: " + e.getMessage());
}
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-gradle-plugins by microsoft.
the class GradleAuthHelper method accountLogin.
private static Account accountLogin(AuthConfiguration auth) {
if (auth.getEnvironment() != null) {
Azure.az(AzureCloud.class).set(auth.getEnvironment());
}
if (auth.getType() == null || auth.getType() == AuthType.AUTO) {
if (StringUtils.isAllBlank(auth.getCertificate(), auth.getCertificatePassword(), auth.getKey())) {
final Account account = findFirstAvailableAccount().block();
if (account == null) {
throw new AzureToolkitAuthenticationException("There are no accounts available.");
}
promptForOAuthOrDeviceCodeLogin(account.getAuthType());
return handleDeviceCodeAccount(Azure.az(AzureAccount.class).loginAsync(account, false).block());
} else {
return doServicePrincipalLogin(auth);
}
} else {
promptForOAuthOrDeviceCodeLogin(auth.getType());
return handleDeviceCodeAccount(Azure.az(AzureAccount.class).loginAsync(auth, false).block());
}
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException 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);
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-maven-plugins by microsoft.
the class RefreshTokenTokenCredentialManager method getRefreshTokenFromMsalToken.
private static String getRefreshTokenFromMsalToken(MsalToken accessToken) {
IAuthenticationResult result = accessToken.getAuthenticationResult();
if (result == null) {
return null;
}
String refreshTokenFromResult;
try {
refreshTokenFromResult = (String) FieldUtils.readField(result, "refreshToken", true);
} catch (IllegalAccessException e) {
throw new AzureToolkitAuthenticationException("cannot read refreshToken from IAuthenticationResult.");
}
return refreshTokenFromResult;
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-maven-plugins by microsoft.
the class AzureCliAccount method preLoginCheck.
protected Mono<Boolean> preLoginCheck() {
return Mono.fromCallable(() -> {
AzureCliUtils.ensureMinimumCliVersion();
AzureCliUtils.executeAzureCli("az account get-access-token --output json");
List<AzureCliSubscription> subscriptions = AzureCliUtils.listSubscriptions();
if (subscriptions.isEmpty()) {
throw new AzureToolkitAuthenticationException("Cannot find any subscriptions in current account.");
}
AzureCliSubscription defaultSubscription = subscriptions.stream().filter(AzureCliSubscription::isSelected).findFirst().orElse(subscriptions.get(0));
AzureEnvironment configEnv = Azure.az(AzureCloud.class).get();
if (configEnv != null && defaultSubscription.getEnvironment() != configEnv) {
throw new AzureToolkitAuthenticationException(String.format("The azure cloud from azure cli '%s' doesn't match with your auth configuration, " + "you can change it by executing 'az cloud set --name=%s' command to change the cloud in azure cli.", AzureEnvironmentUtils.getCloudNameForAzureCli(defaultSubscription.getEnvironment()), AzureEnvironmentUtils.getCloudNameForAzureCli(configEnv)));
}
this.entity.setEnvironment(defaultSubscription.getEnvironment());
this.entity.setEmail(defaultSubscription.getEmail());
subscriptions = subscriptions.stream().filter(s -> StringUtils.equals(this.entity.getEmail(), s.getEmail())).collect(Collectors.toList());
// use the tenant who has one or more subscriptions
this.entity.setTenantIds(subscriptions.stream().map(Subscription::getTenantId).distinct().collect(Collectors.toList()));
this.entity.setSubscriptions(subscriptions.stream().filter(distinctByKey(t -> StringUtils.lowerCase(t.getId()))).map(AzureCliAccount::toSubscription).collect(Collectors.toList()));
// set initial selection of subscriptions
this.entity.setSelectedSubscriptionIds(subscriptions.stream().filter(Subscription::isSelected).map(Subscription::getId).distinct().collect(Collectors.toList()));
return true;
});
}
Aggregations