Search in sources :

Example 11 with AuthMethodDetails

use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.

the class Program method main.

@SuppressWarnings("unused")
public static void main(String[] args) {
    LogManager.getLogManager().reset();
    try {
        try {
            if (CommonSettings.getUiFactory() == null)
                CommonSettings.setUiFactory(new UIFactory());
            String wd = "AuthManageWorkingDir";
            Path dirPath = Paths.get(System.getProperty("user.home"), wd);
            if (!Files.exists(dirPath)) {
                Files.createDirectory(dirPath);
            }
            CommonSettings.settingsBaseDir = dirPath.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("--- Service Principals section ---");
        // create with sp
        //final File credFile = new File(System.getProperty("user.home") + "/ServicePrincipals/srv-pri.azureauth-778");
        String path = "C:\\Users\\shch\\_ServicePrincipalsTest\\sp5\\sp-9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef_20161202161437.azureauth";
        final File credFile = new File("C:\\Users\\shch\\_ServicePrincipalsTest\\sp5\\sp-9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef_20161202161437.azureauth");
        AuthMethodDetails amd = new AuthMethodDetails();
        amd.setAuthMethod(AuthMethod.SP);
        amd.setCredFilePath(path);
        AuthMethodManager amm = AuthMethodManager.getInstance();
        amm.setAuthMethodDetails(amd);
        AzureManager am = amm.getAzureManager();
        SubscriptionManager subscriptionManager = am.getSubscriptionManager();
        List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
        //AzureManager spManager = new ServicePrincipalAzureManager(credFile);
        //printResourceGroups(spManager);
        //printSubscriptions(spManager);
        // create with token
        //AzureManager atManager = new AccessTokenAzureManager(Constants.tenant);
        //AzureManager atManager = new AccessTokenAzureManager();
        //SubscriptionManager.getInstance().showSubscriptionDialog();
        //            AdAuthManager adAuthManager = AdAuthManager.getInstance();
        System.out.println("=== Access token section ---");
        //            AuthenticationResult res = AdAuthManager.getInstance().signIn();
        //            UserInfo ui = res.getUserInfo();
        // Setup working dir
        String wd = "AuthManageWorkingDir";
        Path dirPath = Paths.get(System.getProperty("user.home"), wd);
        if (!Files.exists(dirPath)) {
            Files.createDirectory(dirPath);
        }
        CommonSettings.settingsBaseDir = dirPath.toString();
        // Setup uiFactory
        if (CommonSettings.getUiFactory() == null)
            CommonSettings.setUiFactory(new UIFactory());
    //testReporter(dirPath.toString());
    //graphApiAction();
    //createSp();
    //            AzureManager am = AuthMethodManager.getInstance().getAzureManager(AuthMethod.AD);
    //            if (am == null) {
    //                return;
    //            }
    //
    //            System.out.println("-- Subscription list ---");
    //            printSubscriptions(am);
    //           sidList = AdAuthManager.getInstance().getAccountSidList();
    //            for (String sid : AdAuthManager.getInstance().getAccountSidList()) {
    //                System.out.println("  - " + sid);
    //            }
    //printResourceGroups(am);
    //printTenants(atManager);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IUIFactory(com.microsoft.azuretools.authmanage.interact.IUIFactory) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) IOException(java.io.IOException) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) File(java.io.File) IOException(java.io.IOException) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 12 with AuthMethodDetails

use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails 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 13 with AuthMethodDetails

use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.

the class AuthMethodManager method loadSettings.

@AzureOperation(name = "account|auth_setting.load", type = AzureOperation.Type.TASK)
private static AuthMethodDetails loadSettings() {
    System.out.println("loading authMethodDetails...");
    try {
        String json = AzureStoreManager.getInstance().getIdeStore().getProperty(ACCOUNT, AUTH_METHOD_DETAIL, "");
        if (StringUtils.isBlank(json)) {
            FileStorage fs = new FileStorage(FILE_NAME_AUTH_METHOD_DETAILS, CommonSettings.getSettingsBaseDir());
            byte[] data = fs.read();
            json = new String(data);
            AzureStoreManager.getInstance().getIdeStore().setProperty(ACCOUNT, AUTH_METHOD_DETAIL, json);
            fs.removeFile();
        }
        if (StringUtils.isBlank(json)) {
            System.out.println("No auth method details are saved.");
            return new AuthMethodDetails();
        }
        return JsonHelper.deserialize(AuthMethodDetails.class, json);
    } catch (IOException ignored) {
        System.out.println("Failed to loading authMethodDetails settings. Use defaults.");
        return new AuthMethodDetails();
    }
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IOException(java.io.IOException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 14 with AuthMethodDetails

use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.

the class AuthMethodManager method initAuthMethodManagerFromSettings.

private void initAuthMethodManagerFromSettings() {
    EventUtil.executeWithLog(ACCOUNT, RESIGNIN, operation -> {
        try {
            AuthMethodDetails targetAuthMethodDetails = loadSettings();
            if (targetAuthMethodDetails == null || targetAuthMethodDetails.getAuthMethod() == null) {
                targetAuthMethodDetails = new AuthMethodDetails();
                targetAuthMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
            } else {
                // convert old auth method to new ones
                switch(targetAuthMethodDetails.getAuthMethod()) {
                    case AZ:
                        {
                            targetAuthMethodDetails.setAuthType(AuthType.AZURE_CLI);
                            break;
                        }
                    case DC:
                        {
                            targetAuthMethodDetails.setAuthType(AuthType.DEVICE_CODE);
                            break;
                        }
                    case AD:
                        // we don't support it now
                        LOGGER.warn("The AD auth method is not supported now, ignore the credential.");
                        break;
                    case SP:
                        targetAuthMethodDetails.setAuthType(AuthType.SERVICE_PRINCIPAL);
                        break;
                    default:
                        break;
                }
                targetAuthMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
            }
            authMethodDetails = this.identityAzureManager.restoreSignIn(targetAuthMethodDetails).block();
            List<SubscriptionDetail> persistSubscriptions = identityAzureManager.getSubscriptionManager().loadSubscriptions();
            if (CollectionUtils.isNotEmpty(persistSubscriptions)) {
                List<String> savedSubscriptionList = persistSubscriptions.stream().filter(SubscriptionDetail::isSelected).map(SubscriptionDetail::getSubscriptionId).distinct().collect(Collectors.toList());
                identityAzureManager.selectSubscriptionByIds(savedSubscriptionList);
            }
            initFuture.complete(true);
            // pre-load regions
            AzureAccount az = com.microsoft.azure.toolkit.lib.Azure.az(AzureAccount.class);
            Optional.of(identityAzureManager.getSelectedSubscriptionIds()).ifPresent(sids -> sids.stream().limit(5).forEach(az::listRegions));
            final String authMethod = authMethodDetails.getAuthMethod() == null ? "Empty" : authMethodDetails.getAuthMethod().name();
            final Map<String, String> telemetryProperties = new HashMap<String, String>() {

                {
                    put(SIGNIN_METHOD, authMethod);
                    put(AZURE_ENVIRONMENT, CommonSettings.getEnvironment().getName());
                }
            };
            EventUtil.logEvent(EventType.info, operation, telemetryProperties);
            notifySignInEventListener();
        } catch (RuntimeException exception) {
            initFuture.complete(true);
            EventUtil.logError(operation, ErrorType.systemError, exception, null, null);
            this.authMethodDetails = new AuthMethodDetails();
            this.authMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
            notifySignOutEventListener();
        }
        return this;
    });
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) HashMap(java.util.HashMap) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount)

Example 15 with AuthMethodDetails

use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.

the class SignInCommandHandler 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, () -> doLogin(AzureTaskContext.current().getTask().getMonitor(), auth));
    return AzureTaskManager.getInstance().runInBackgroundAsObservable(task).toSingle();
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Aggregations

AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)20 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)6 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)5 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)5 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)4 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)4 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)4 AuthMethodManager (com.microsoft.azuretools.authmanage.AuthMethodManager)4 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)3 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)3 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 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 AzureMessager (com.microsoft.azure.toolkit.lib.common.messager.AzureMessager)2 AzureOperationBundle (com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle)2 AzureTaskManager (com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager)2 IDeviceLoginUI (com.microsoft.azuretools.adauth.IDeviceLoginUI)2