use of com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration in project azure-tools-for-java by Microsoft.
the class SignInCommandHandler method showSignInWindowAndGetAuthConfiguration.
private static AuthConfiguration showSignInWindowAndGetAuthConfiguration(Shell parentShell) throws InterruptedException {
final SignInDialog dialog = new SignInDialog(parentShell);
dialog.create();
if (dialog.open() != Window.OK) {
throw new InterruptedException("user cancel");
}
AuthConfiguration auth = dialog.getData();
if (auth.getType() == AuthType.SERVICE_PRINCIPAL) {
ServicePrincipalLoginDialog servicePrincipalLoginDialog = new ServicePrincipalLoginDialog(parentShell);
if (servicePrincipalLoginDialog.open() == Window.CANCEL) {
throw new InterruptedException("user cancel");
}
auth = servicePrincipalLoginDialog.getModel();
}
return auth;
}
use of com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration in project azure-tools-for-java by Microsoft.
the class ServicePrincipalLoginDialog method doValidateAll.
@Override
@NotNull
protected List<ValidationInfo> doValidateAll() {
List<ValidationInfo> res = new ArrayList<>();
AuthConfiguration data = getData();
if (StringUtils.isBlank(data.getTenant())) {
res.add(new ValidationInfo("tenant is required.", tenantIdTextField));
}
if (!isGuid(data.getTenant())) {
res.add(new ValidationInfo("tenant should be a valid guid.", tenantIdTextField));
}
if (StringUtils.isBlank(data.getClient())) {
res.add(new ValidationInfo("clientId(appId) is required.", clientIdTextField));
}
if (!isGuid(data.getClient())) {
res.add(new ValidationInfo("clientId(appId) should be a valid guid.", clientIdTextField));
}
if (this.passwordRadioButton.isSelected()) {
if (StringUtils.isBlank(data.getKey())) {
res.add(new ValidationInfo("Password is required.", keyPasswordField));
}
} else {
if (StringUtils.isBlank(data.getCertificate())) {
res.add(new ValidationInfo("Please select a cert file.", certFileTextField));
} else if (!new File(data.getCertificate()).exists()) {
res.add(new ValidationInfo(String.format("Cannot find cert file(%s).", certFileTextField.getText()), certFileTextField));
}
}
return res;
}
use of com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration in project azure-tools-for-java by Microsoft.
the class ServicePrincipalLoginDialog method uiTextComponents2Json.
private void uiTextComponents2Json() {
if (!intermediateState.compareAndSet(false, true)) {
return;
}
try {
Map<String, String> map = new LinkedHashMap<>();
AuthConfiguration data = getData();
if (this.certificateRadioButton.isSelected()) {
map.put("fileWithCertAndPrivateKey", data.getCertificate());
} else {
String password = StringUtils.isNotBlank(data.getKey()) ? "<hidden>" : "<empty>";
map.put("password", password);
}
map.put("appId", data.getClient());
map.put("tenant", data.getTenant());
String text = JsonUtils.getGson().toJson(map);
if (!StringUtils.equals(jsonDataEditor.getText(), text)) {
this.jsonDataEditor.setText(text);
this.jsonDataEditor.setCaretPosition(0);
}
} finally {
intermediateState.set(false);
}
}
use of com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration 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.azure.toolkit.lib.auth.model.AuthConfiguration 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())));
}
}
Aggregations