Search in sources :

Example 1 with AuthMethodManager

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

the class ServiceExplorerView method makeActions.

private void makeActions() {
    refreshAction = new Action("Refresh", Activator.getImageDescriptor("icons/RefreshLight_16.png")) {

        @Override
        public void run() {
            azureModule.load(true);
        }
    };
    refreshAction.setToolTipText("Refresh");
    signInOutAction = new Action("Sign In/Sign Out", com.microsoft.azuretools.core.Activator.getImageDescriptor("icons/SignOutLight_16.png")) {

        @Override
        public void run() {
            try {
                AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
                boolean isSignedIn = authMethodManager.isSignedIn();
                if (isSignedIn) {
                    SignOutCommandHandler.doSignOut(PluginUtil.getParentShell());
                } else {
                    SignInCommandHandler.doSignIn(PluginUtil.getParentShell());
                }
            } catch (Exception ex) {
                Activator.getDefault().log(ex.getMessage(), ex);
            }
        }
    };
    selectSubscriptionAction = new Action("Select Subscriptions", com.microsoft.azuretools.core.Activator.getImageDescriptor("icons/ConnectAccountsLight_16.png")) {

        @Override
        public void run() {
            try {
                if (AuthMethodManager.getInstance().isSignedIn()) {
                    SelectSubsriptionsCommandHandler.onSelectSubscriptions(PluginUtil.getParentShell());
                    azureModule.load(false);
                }
            } catch (Exception ex) {
            }
        }
    };
    selectSubscriptionAction.setToolTipText("Select Subscriptions");
    doubleClickAction = new Action() {

        @Override
        public void run() {
            ISelection selection = viewer.getSelection();
            Object obj = ((IStructuredSelection) selection).getFirstElement();
            if (!viewer.getExpandedState(obj)) {
                viewer.expandToLevel(obj, 1);
            } else {
                viewer.collapseToLevel(obj, 1);
            }
        }
    };
}
Also used : NodeAction(com.microsoft.tooling.msservices.serviceexplorer.NodeAction) Action(org.eclipse.jface.action.Action) ISelection(org.eclipse.jface.viewers.ISelection) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 2 with AuthMethodManager

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

the class AuthPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (PROPERTY_IS_SIGNED_IN.equals(property)) {
        try {
            AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
            boolean isSignIn = authMethodManager.isSignedIn();
            return isSignIn;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 3 with AuthMethodManager

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

the class SignOutCommandHandler method doSignOut.

public static void doSignOut(Shell shell) {
    EventUtil.executeWithLog(TelemetryConstants.ACCOUNT, TelemetryConstants.SIGNOUT, (operation) -> {
        AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
        MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
        messageBox.setMessage(getSignOutWarningMessage(authMethodManager));
        messageBox.setText("Azure Sign Out");
        int response = messageBox.open();
        if (response == SWT.YES) {
            if (authMethodManager.isSignedIn()) {
                authMethodManager.signOut();
            }
        }
    }, (ex) -> ex.printStackTrace());
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 4 with AuthMethodManager

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

the class AzureSignInAction method onAzureSignIn.

public static void onAzureSignIn(Project project) {
    JFrame frame = WindowManager.getInstance().getFrame(project);
    AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
    boolean isSignIn = authMethodManager.isSignedIn();
    if (isSignIn) {
        boolean res = DefaultLoader.getUIHelper().showYesNoDialog(frame.getRootPane(), getSignOutWarningMessage(authMethodManager), "Azure Sign Out", AzureIconLoader.loadIcon(AzureIconSymbol.Common.AZURE));
        if (res) {
            EventUtil.executeWithLog(ACCOUNT, SIGNOUT, (operation) -> {
                authMethodManager.signOut();
            });
        }
    } else {
        signInIfNotSignedIn(project).subscribe(isLoggedIn -> {
            if (isLoggedIn) {
                AzureAccount az = Azure.az(AzureAccount.class);
                AzureTaskManager.getInstance().runOnPooledThread(() -> authMethodManager.getAzureManager().getSelectedSubscriptions().stream().limit(5).forEach(s -> {
                    // pre-load regions;
                    az.listRegions(s.getId());
                }));
            }
        });
    }
}
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) JFrame(javax.swing.JFrame) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 5 with AuthMethodManager

use of com.microsoft.azuretools.authmanage.AuthMethodManager 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)

Aggregations

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