Search in sources :

Example 1 with IDeviceLoginUI

use of com.microsoft.azuretools.adauth.IDeviceLoginUI in project azure-tools-for-java by Microsoft.

the class AzureSignInAction method signInIfNotSignedInInternal.

private static Mono<Boolean> signInIfNotSignedInInternal(Project project) {
    final AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
    final IDeviceLoginUI deviceLoginUI = new DeviceLoginUI();
    return Mono.create(sink -> AzureTaskManager.getInstance().runLater(() -> {
        final AuthConfiguration auth;
        try {
            auth = showSignInWindowAndGetAuthConfiguration(project);
        } 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.show(project, 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);
    }));
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) Azure(com.microsoft.azure.toolkit.lib.Azure) AzureBundle.message(com.microsoft.intellij.ui.messages.AzureBundle.message) DataKeys(com.intellij.openapi.actionSystem.DataKeys) ACCOUNT(com.microsoft.azuretools.telemetry.TelemetryConstants.ACCOUNT) StringUtils(org.apache.commons.lang3.StringUtils) IDeviceLoginUI(com.microsoft.azuretools.adauth.IDeviceLoginUI) DeviceLoginUI(com.microsoft.intellij.ui.DeviceLoginUI) AzureIconSymbol(com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) Single(rx.Single) UIHelperImpl(com.microsoft.intellij.helpers.UIHelperImpl) Duration(java.time.Duration) Map(java.util.Map) AzureAnAction(com.microsoft.intellij.AzureAnAction) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IdentityAzureManager(com.microsoft.azuretools.sdkmanage.IdentityAzureManager) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity) AZURE_ENVIRONMENT(com.microsoft.azuretools.telemetry.TelemetryConstants.AZURE_ENVIRONMENT) Logger(com.intellij.openapi.diagnostic.Logger) JFrame(javax.swing.JFrame) MsalClientException(com.microsoft.aad.msal4j.MsalClientException) AzureLoginHelper(com.microsoft.intellij.util.AzureLoginHelper) ProgressManager(com.intellij.openapi.progress.ProgressManager) CancellationException(java.util.concurrent.CancellationException) AuthMethod(com.microsoft.azuretools.authmanage.AuthMethod) WindowManager(com.intellij.openapi.wm.WindowManager) ErrorType(com.microsoft.azuretools.telemetrywrapper.ErrorType) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) SIGNIN_METHOD(com.microsoft.azuretools.telemetry.TelemetryConstants.SIGNIN_METHOD) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SIGNIN(com.microsoft.azuretools.telemetry.TelemetryConstants.SIGNIN) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Optional(java.util.Optional) EventUtil(com.microsoft.azuretools.telemetrywrapper.EventUtil) Lombok(lombok.Lombok) NotNull(org.jetbrains.annotations.NotNull) AzureTaskManager(com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager) AzureEnvironmentUtils(com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) AzureIconLoader(com.microsoft.intellij.helpers.AzureIconLoader) Exceptions(rx.exceptions.Exceptions) AzureOperationBundle(com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle) SignInWindow(com.microsoft.intellij.ui.SignInWindow) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) CollectionUtils(org.apache.commons.collections4.CollectionUtils) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) Project(com.intellij.openapi.project.Project) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) Schedulers(reactor.core.scheduler.Schedulers) ServicePrincipalLoginDialog(com.microsoft.intellij.ui.ServicePrincipalLoginDialog) SIGNOUT(com.microsoft.azuretools.telemetry.TelemetryConstants.SIGNOUT) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureMessager(com.microsoft.azure.toolkit.lib.common.messager.AzureMessager) Mono(reactor.core.publisher.Mono) ErrorWindow(com.microsoft.intellij.ui.ErrorWindow) Flux(reactor.core.publisher.Flux) TelemetryConstants(com.microsoft.azuretools.telemetry.TelemetryConstants) SignInOutAction(com.microsoft.intellij.serviceexplorer.azure.SignInOutAction) TelemetryManager(com.microsoft.azuretools.telemetrywrapper.TelemetryManager) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IDeviceLoginUI(com.microsoft.azuretools.adauth.IDeviceLoginUI) Single(rx.Single) CancellationException(java.util.concurrent.CancellationException) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) IDeviceLoginUI(com.microsoft.azuretools.adauth.IDeviceLoginUI) DeviceLoginUI(com.microsoft.intellij.ui.DeviceLoginUI) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 2 with IDeviceLoginUI

use of com.microsoft.azuretools.adauth.IDeviceLoginUI 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);
    }));
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) Azure(com.microsoft.azure.toolkit.lib.Azure) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) StringUtils(org.apache.commons.lang3.StringUtils) IDeviceLoginUI(com.microsoft.azuretools.adauth.IDeviceLoginUI) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) Single(rx.Single) PluginUtil(com.microsoft.azuretools.core.utils.PluginUtil) Duration(java.time.Duration) Map(java.util.Map) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IdentityAzureManager(com.microsoft.azuretools.sdkmanage.IdentityAzureManager) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity) MsalClientException(com.microsoft.aad.msal4j.MsalClientException) CancellationException(java.util.concurrent.CancellationException) AuthMethod(com.microsoft.azuretools.authmanage.AuthMethod) ErrorType(com.microsoft.azuretools.telemetrywrapper.ErrorType) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AzureTaskContext(com.microsoft.azure.toolkit.lib.common.task.AzureTaskContext) Window(org.eclipse.jface.window.Window) List(java.util.List) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) SWT(org.eclipse.swt.SWT) DeviceLoginWindow(com.microsoft.azuretools.core.ui.DeviceLoginWindow) EventUtil(com.microsoft.azuretools.telemetrywrapper.EventUtil) Lombok(lombok.Lombok) AzureTaskManager(com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager) SignInDialog(com.microsoft.azuretools.core.ui.SignInDialog) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) AzureAbstractHandler(com.microsoft.azuretools.core.utils.AzureAbstractHandler) AzureEnvironmentUtils(com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) Exceptions(rx.exceptions.Exceptions) AzureOperationBundle(com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) CollectionUtils(org.apache.commons.collections4.CollectionUtils) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) ServicePrincipalLoginDialog(com.microsoft.azuretools.core.ui.ServicePrincipalLoginDialog) ErrorWindow(com.microsoft.azuretools.core.ui.ErrorWindow) Schedulers(reactor.core.scheduler.Schedulers) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureMessager(com.microsoft.azure.toolkit.lib.common.messager.AzureMessager) Shell(org.eclipse.swt.widgets.Shell) Mono(reactor.core.publisher.Mono) ExecutionException(org.eclipse.core.commands.ExecutionException) Flux(reactor.core.publisher.Flux) TelemetryConstants(com.microsoft.azuretools.telemetry.TelemetryConstants) TelemetryManager(com.microsoft.azuretools.telemetrywrapper.TelemetryManager) MessageBox(org.eclipse.swt.widgets.MessageBox) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IDeviceLoginUI(com.microsoft.azuretools.adauth.IDeviceLoginUI) DeviceLoginWindow(com.microsoft.azuretools.core.ui.DeviceLoginWindow) Single(rx.Single) CancellationException(java.util.concurrent.CancellationException) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Aggregations

MsalClientException (com.microsoft.aad.msal4j.MsalClientException)2 Azure (com.microsoft.azure.toolkit.lib.Azure)2 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)2 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)2 DeviceCodeAccount (com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount)2 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)2 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)2 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)2 AzureEnvironmentUtils (com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils)2 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)2 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)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 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)2