Search in sources :

Example 1 with AuthType

use of com.microsoft.azure.toolkit.lib.auth.model.AuthType in project azure-tools-for-java by Microsoft.

the class SignInDialog method syncControlControls.

private void syncControlControls() {
    setErrorMessage(null);
    AuthType type = data.getType();
    lblDeviceInfo.setEnabled(type == AuthType.DEVICE_CODE);
    lblAzureCli.setEnabled(type == AuthType.AZURE_CLI);
    lblSP.setEnabled(type == AuthType.SERVICE_PRINCIPAL);
    this.lblOAuth.setEnabled(type == AuthType.OAUTH2);
}
Also used : AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType)

Example 2 with AuthType

use of com.microsoft.azure.toolkit.lib.auth.model.AuthType in project azure-tools-for-java by Microsoft.

the class AzureSignInAction method getSignOutWarningMessage.

private static String getSignOutWarningMessage(@NotNull AuthMethodManager authMethodManager) {
    final AuthMethodDetails authMethodDetails = authMethodManager.getAuthMethodDetails();
    if (authMethodDetails == null || authMethodDetails.getAuthType() == null) {
        return "Do you really want to sign out?";
    }
    final AuthType authType = authMethodDetails.getAuthType();
    final String warningMessage;
    switch(authType) {
        case SERVICE_PRINCIPAL:
            warningMessage = String.format("Signed in using service principal \"%s\"", authMethodDetails.getClientId());
            break;
        case OAUTH2:
        case DEVICE_CODE:
            warningMessage = String.format("Signed in as %s(%s)", authMethodDetails.getAccountEmail(), authType.toString());
            break;
        case AZURE_CLI:
            warningMessage = "Signed in with Azure CLI";
            break;
        default:
            warningMessage = "Signed in by unknown authentication method.";
            break;
    }
    return String.format("%s\nDo you really want to sign out? %s", warningMessage, authType == AuthType.AZURE_CLI ? "(This will not sign you out from Azure CLI)" : "");
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Example 3 with AuthType

use of com.microsoft.azure.toolkit.lib.auth.model.AuthType in project azure-tools-for-java by Microsoft.

the class SignOutCommandHandler method getSignOutWarningMessage.

public static String getSignOutWarningMessage(@Nonnull AuthMethodManager authMethodManager) {
    final AuthMethodDetails authMethodDetails = authMethodManager.getAuthMethodDetails();
    if (authMethodDetails == null || authMethodDetails.getAuthType() == null) {
        return "Do you really want to sign out?";
    }
    final AuthType authType = authMethodDetails.getAuthType();
    final String warningMessage;
    switch(authType) {
        case SERVICE_PRINCIPAL:
            warningMessage = String.format("Signed in using service principal \"%s\"", authMethodDetails.getClientId());
            break;
        case OAUTH2:
        case DEVICE_CODE:
            warningMessage = String.format("Signed in as %s(%s)", authMethodDetails.getAccountEmail(), authType.toString());
            break;
        case AZURE_CLI:
            warningMessage = "Signed in with Azure CLI";
            break;
        default:
            warningMessage = "Signed in by unknown authentication method.";
            break;
    }
    return String.format("%s\nDo you really want to sign out? %s", warningMessage, authType == AuthType.AZURE_CLI ? "(This will not sign you out from Azure CLI)" : "");
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType)

Example 4 with AuthType

use of com.microsoft.azure.toolkit.lib.auth.model.AuthType 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 5 with AuthType

use of com.microsoft.azure.toolkit.lib.auth.model.AuthType 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

AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)5 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)4 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)2 Account (com.microsoft.azure.toolkit.lib.auth.Account)1 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)1 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)1 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)1 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)1 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)1 ServicePrincipalLoginDialog (com.microsoft.intellij.ui.ServicePrincipalLoginDialog)1 SignInWindow (com.microsoft.intellij.ui.SignInWindow)1