Search in sources :

Example 6 with AuthConfiguration

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;
}
Also used : SignInDialog(com.microsoft.azuretools.core.ui.SignInDialog) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) ServicePrincipalLoginDialog(com.microsoft.azuretools.core.ui.ServicePrincipalLoginDialog)

Example 7 with AuthConfiguration

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;
}
Also used : ValidationInfo(com.intellij.openapi.ui.ValidationInfo) AzureValidationInfo(com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo) ArrayList(java.util.ArrayList) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with AuthConfiguration

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);
    }
}
Also used : AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with AuthConfiguration

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;
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) ServicePrincipalLoginDialog(com.microsoft.intellij.ui.ServicePrincipalLoginDialog) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) SignInWindow(com.microsoft.intellij.ui.SignInWindow)

Example 10 with AuthConfiguration

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())));
    }
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)

Aggregations

AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)10 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)4 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)4 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)3 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)3 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)3 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)3 MsalClientException (com.microsoft.aad.msal4j.MsalClientException)2 Azure (com.microsoft.azure.toolkit.lib.Azure)2 DeviceCodeAccount (com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount)2 AzureEnvironmentUtils (com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils)2 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)2 AzureMessager (com.microsoft.azure.toolkit.lib.common.messager.AzureMessager)2 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)2 AzureOperationBundle (com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle)2 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)2 AzureTaskManager (com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager)2 IDeviceLoginUI (com.microsoft.azuretools.adauth.IDeviceLoginUI)2 AuthMethod (com.microsoft.azuretools.authmanage.AuthMethod)2 AuthMethodManager (com.microsoft.azuretools.authmanage.AuthMethodManager)2