use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class SignInCommandHandler method signInIfNotSignedInInternal.
private static Mono<Boolean> signInIfNotSignedInInternal(Shell projectShell) {
final AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
final IDeviceLoginUI deviceLoginUI = new DeviceLoginWindow(projectShell);
return Mono.create(sink -> AzureTaskManager.getInstance().runLater(() -> {
final AuthConfiguration auth;
try {
auth = showSignInWindowAndGetAuthConfiguration(projectShell);
} catch (InterruptedException e) {
sink.error(e);
return;
}
Single<AuthMethodDetails> single;
if (auth.getType() != AuthType.DEVICE_CODE) {
single = loginNonDeviceCodeSingle(auth);
} else {
single = loginDeviceCodeSingle().map(account -> {
AzureTaskManager.getInstance().runLater(() -> deviceLoginUI.promptDeviceCode(account.getDeviceCode()));
CompletableFuture<AuthMethodDetails> future = account.continueLogin().map(ac -> fromAccountEntity(ac.getEntity())).doFinally(signal -> deviceLoginUI.closePrompt()).toFuture();
deviceLoginUI.setFuture(future);
try {
return future.get();
} catch (Throwable ex) {
if (!(ex instanceof CancellationException)) {
ex.printStackTrace();
ErrorWindow.go(projectShell, ex.getMessage(), SIGN_IN_ERROR);
}
return new AuthMethodDetails();
}
});
}
single.subscribeOn(rx.schedulers.Schedulers.io()).subscribe(authMethodDetails -> {
if (authMethodManager.isSignedIn()) {
authMethodManager.setAuthMethodDetails(authMethodDetails);
}
sink.success(authMethodManager.isSignedIn());
}, sink::error);
}));
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class AzureSignInAction method showSignInWindowAndGetAuthConfiguration.
private static AuthConfiguration showSignInWindowAndGetAuthConfiguration(Project project) throws InterruptedException {
final SignInWindow dialog = new SignInWindow(new AuthMethodDetails(), project);
if (!dialog.showAndGet()) {
throw new InterruptedException("user cancel");
}
AuthConfiguration auth = new AuthConfiguration();
AuthType type = dialog.getData();
auth.setType(type);
if (type == AuthType.SERVICE_PRINCIPAL) {
final ServicePrincipalLoginDialog spDialog = new ServicePrincipalLoginDialog(project);
if (!spDialog.showAndGet()) {
throw new InterruptedException("user cancel");
}
auth = spDialog.getData();
}
return auth;
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class AzureSignInAction method loginNonDeviceCodeSingle.
private static Single<AuthMethodDetails> loginNonDeviceCodeSingle(AuthConfiguration auth) {
final AzureString title = AzureOperationBundle.title("account.sign_in");
final AzureTask<AuthMethodDetails> task = new AzureTask<>(null, title, true, () -> {
final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
indicator.setIndeterminate(true);
return doLogin(indicator, auth);
});
return AzureTaskManager.getInstance().runInBackgroundAsObservable(task).toSingle();
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class IdentityAzureManager method restoreSignIn.
public Mono<AuthMethodDetails> restoreSignIn(AuthMethodDetails authMethodDetails) {
if (authMethodDetails == null || authMethodDetails.getAuthMethod() == null || authMethodDetails.getAuthType() == null) {
return Mono.just(new AuthMethodDetails());
}
if (StringUtils.isNotBlank(authMethodDetails.getAzureEnv())) {
Azure.az(AzureCloud.class).setByName(authMethodDetails.getAzureEnv());
}
AuthType authType = authMethodDetails.getAuthType();
try {
if (authType == AuthType.SERVICE_PRINCIPAL) {
AuthConfiguration auth = new AuthConfiguration();
auth.setType(AuthType.SERVICE_PRINCIPAL);
auth.setClient(authMethodDetails.getClientId());
auth.setTenant(authMethodDetails.getTenantId());
auth.setEnvironment(Azure.az(AzureCloud.class).get());
if (StringUtils.isNotBlank(authMethodDetails.getCertificate())) {
auth.setCertificate(authMethodDetails.getCertificate());
} else {
secureStore.migratePassword("account|" + auth.getClient(), null, SERVICE_PRINCIPAL_STORE_SERVICE, auth.getClient(), null);
String key = secureStore == null ? null : secureStore.loadPassword(SERVICE_PRINCIPAL_STORE_SERVICE, authMethodDetails.getClientId(), null);
if (StringUtils.isBlank(key)) {
throw new AzureToolkitRuntimeException(String.format("Cannot find SP security key for '%s' in intellij key pools.", authMethodDetails.getClientId()));
}
auth.setKey(key);
}
return signInServicePrincipal(auth).map(ac -> authMethodDetails);
} else {
if (StringUtils.isNotBlank(authMethodDetails.getClientId())) {
AccountEntity entity = new AccountEntity();
entity.setType(authType);
entity.setEnvironment(Azure.az(AzureCloud.class).get());
entity.setEmail(authMethodDetails.getAccountEmail());
entity.setClientId(authMethodDetails.getClientId());
entity.setTenantIds(StringUtils.isNotBlank(authMethodDetails.getTenantId()) ? Collections.singletonList(authMethodDetails.getTenantId()) : null);
Account account = Azure.az(AzureAccount.class).account(entity);
return Mono.just(fromAccountEntity(account.getEntity()));
} else {
throw new AzureToolkitRuntimeException("Cannot restore credentials due to version change.");
}
}
} catch (Throwable e) {
if (StringUtils.isNotBlank(authMethodDetails.getClientId()) && authMethodDetails.getAuthType() == AuthType.SERVICE_PRINCIPAL && secureStore != null) {
secureStore.forgetPassword(SERVICE_PRINCIPAL_STORE_SERVICE, authMethodDetails.getClientId(), null);
}
return Mono.error(new AzureToolkitRuntimeException(String.format("Cannot restore credentials due to error: %s", e.getMessage())));
}
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class IdentityAzureManager method fromAccountEntity.
private static AuthMethodDetails fromAccountEntity(AccountEntity entity) {
AuthMethodDetails authMethodDetails = new AuthMethodDetails();
authMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
authMethodDetails.setAuthType(entity.getType());
authMethodDetails.setClientId(entity.getClientId());
authMethodDetails.setTenantId(CollectionUtils.isEmpty(entity.getTenantIds()) ? "" : entity.getTenantIds().get(0));
authMethodDetails.setAzureEnv(AzureEnvironmentUtils.getCloudNameForAzureCli(entity.getEnvironment()));
authMethodDetails.setAccountEmail(entity.getEmail());
return authMethodDetails;
}
Aggregations