Search in sources :

Example 6 with AuthMethodManager

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

the class ApplicationInsightsPreferencePage method signInOutBtnListener.

private void signInOutBtnListener() {
    try {
        AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
        boolean isSignedIn = authMethodManager.isSignedIn();
        if (isSignedIn) {
            SignOutCommandHandler.doSignOut(PluginUtil.getParentShell());
        } else {
            SignInCommandHandler.doSignIn(PluginUtil.getParentShell());
        }
        signInOutBtn.setImage(PluginUtil.getImage(authMethodManager.isSignedIn() ? ICON_SIGNOUT : ICON_SIGNIN));
        tableViewer.refresh();
    } catch (Exception ex) {
        Activator.getDefault().log(ex.getMessage(), ex);
    }
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 7 with AuthMethodManager

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

the class SignInCommandHandler method doSignIn.

public static boolean doSignIn(Shell shell) {
    try {
        AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
        boolean isSignIn = authMethodManager.isSignedIn();
        if (isSignIn)
            return true;
        SignInDialog d = SignInDialog.go(shell, authMethodManager.getAuthMethodDetails());
        if (null != d) {
            AuthMethodDetails authMethodDetailsUpdated = d.getAuthMethodDetails();
            authMethodManager.setAuthMethodDetails(authMethodDetailsUpdated);
            SelectSubsriptionsCommandHandler.onSelectSubscriptions(shell);
            authMethodManager.notifySignInEventListener();
        }
        return authMethodManager.isSignedIn();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) SignInDialog(com.microsoft.azuretools.core.ui.SignInDialog) ExecutionException(org.eclipse.core.commands.ExecutionException) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 8 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);
    try {
        AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
        boolean isSignIn = authMethodManager.isSignedIn();
        if (isSignIn) {
            String artifact = (authMethodManager.getAuthMethod() == AuthMethod.AD) ? "Signed in as " + authMethodManager.getAuthMethodDetails().getAccountEmail() : "Signed in using file \"" + authMethodManager.getAuthMethodDetails().getCredFilePath() + "\"";
            int res = JOptionPane.showConfirmDialog(frame, artifact + "\n" + "Do you really want to sign out?", "Azure Sign Out", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("icons/azure.png"));
            if (res == JOptionPane.OK_OPTION) {
                AdAuthManager adAuthManager = AdAuthManager.getInstance();
                if (adAuthManager.isSignedIn())
                    adAuthManager.signOut();
                authMethodManager.signOut();
            }
        } else {
            //                SignInWindow w = SignInWindow.go(authMethodManager.getAuthMethodDetails(), project);
            //                if (w != null) {
            //                    AuthMethodDetails authMethodDetailsUpdated = w.getAuthMethodDetails();
            //                    authMethodManager.setAuthMethodDetails(authMethodDetailsUpdated);
            //                    SelectSubscriptionsAction.onShowSubscriptions(project);
            //                }
            doSignIn(authMethodManager, project);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        //LOGGER.error("onAzureSignIn", ex);
        ErrorWindow.show(project, ex.getMessage(), "AzureSignIn Action Error");
    }
}
Also used : AdAuthManager(com.microsoft.azuretools.authmanage.AdAuthManager) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 9 with AuthMethodManager

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

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

the class SignInCommandHandler method onAzureSignIn.

private void onAzureSignIn(Shell shell) {
    final AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
    boolean isSignIn = authMethodManager.isSignedIn();
    if (isSignIn) {
        boolean res = showYesNoDialog(shell, "Azure Sign Out", SignOutCommandHandler.getSignOutWarningMessage(authMethodManager));
        if (res) {
            EventUtil.executeWithLog(ACCOUNT, SIGNOUT, (operation) -> {
                authMethodManager.signOut();
            });
        }
    } else {
        signInIfNotSignedIn(shell).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) 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) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) 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