Search in sources :

Example 1 with CertificateManager

use of org.netxms.certificate.manager.CertificateManager in project netxms by netxms.

the class ApplicationWorkbenchWindowAdvisor method doLogin.

/**
 * Show login dialog and perform login
 */
private void doLogin(final Display display) {
    settings = Activator.getDefault().getDialogSettings();
    boolean success = false;
    boolean autoConnect = false;
    boolean ignoreProtocolVersion = false;
    // $NON-NLS-1$
    String password = "";
    CertificateManager certMgr = CertificateManagerProvider.provideCertificateManager();
    certMgr.setKeyStoreRequestListener(this);
    certMgr.setPasswordRequestListener(this);
    for (String s : Platform.getCommandLineArgs()) {
        if (// $NON-NLS-1$
        s.startsWith("-server=")) {
            // $NON-NLS-1$
            settings.put("Connect.Server", s.substring(8));
        } else if (// $NON-NLS-1$
        s.startsWith("-login=")) {
            // $NON-NLS-1$
            settings.put("Connect.Login", s.substring(7));
        } else if (// $NON-NLS-1$
        s.startsWith("-password=")) {
            password = s.substring(10);
            // $NON-NLS-1$
            settings.put("Connect.AuthMethod", AuthenticationType.PASSWORD.getValue());
        } else if (// $NON-NLS-1$
        s.equals("-auto")) {
            autoConnect = true;
        } else if (// $NON-NLS-1$
        s.equals("-ignore-protocol-version")) {
            ignoreProtocolVersion = true;
        }
    }
    boolean encrypt = true;
    LoginDialog loginDialog = new LoginDialog(null, certMgr);
    while (!success) {
        if (!autoConnect) {
            if (loginDialog.open() != Window.OK)
                System.exit(0);
            password = loginDialog.getPassword();
        } else {
            // only do auto connect first time
            autoConnect = false;
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        ConsoleSharedData.setProperty("SlowLink", settings.getBoolean("Connect.SlowLink"));
        LoginJob job = new LoginJob(display, // $NON-NLS-1$
        settings.get("Connect.Server"), // $NON-NLS-1$
        settings.get("Connect.Login"), encrypt, ignoreProtocolVersion);
        AuthenticationType authMethod;
        try {
            // $NON-NLS-1$
            authMethod = AuthenticationType.getByValue(settings.getInt("Connect.AuthMethod"));
        } catch (NumberFormatException e) {
            authMethod = AuthenticationType.PASSWORD;
        }
        switch(authMethod) {
            case PASSWORD:
                job.setPassword(password);
                break;
            case CERTIFICATE:
                job.setCertificate(loginDialog.getCertificate(), getSignature(certMgr, loginDialog.getCertificate()));
                break;
            default:
                break;
        }
        try {
            ModalContext.run(job, true, SplashHandler.getInstance().getBundleProgressMonitor(), Display.getCurrent());
            success = true;
        } catch (InvocationTargetException e) {
            if ((e.getCause() instanceof NXCException) && ((((NXCException) e.getCause()).getErrorCode() == RCC.NO_ENCRYPTION_SUPPORT) || (((NXCException) e.getCause()).getErrorCode() == RCC.NO_CIPHERS)) && encrypt) {
                // $NON-NLS-1$ //$NON-NLS-2$
                boolean alwaysAllow = settings.getBoolean("Connect.AllowUnencrypted." + settings.get("Connect.Server"));
                int action = getAction(settings, alwaysAllow);
                if (action != SecurityWarningDialog.NO) {
                    autoConnect = true;
                    encrypt = false;
                    if (action == SecurityWarningDialog.ALWAYS) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        settings.put("Connect.AllowUnencrypted." + settings.get("Connect.Server"), true);
                    }
                }
            } else {
                e.getCause().printStackTrace();
                MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_ConnectionError, e.getCause().getLocalizedMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            // $NON-NLS-1$
            MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Exception, e.toString());
        }
    }
    CertificateManagerProvider.dispose();
    // Suggest user to change password if it is expired
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    if ((session.getAuthenticationMethod() == AuthenticationType.PASSWORD) && session.isPasswordExpired()) {
        requestPasswordChange(loginDialog.getPassword(), session);
    }
}
Also used : NXCSession(org.netxms.client.NXCSession) CertificateManager(org.netxms.certificate.manager.CertificateManager) LoginDialog(org.netxms.ui.eclipse.console.dialogs.LoginDialog) LoginJob(org.netxms.ui.eclipse.jobs.LoginJob) InvocationTargetException(java.lang.reflect.InvocationTargetException) NXCException(org.netxms.client.NXCException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NXCException(org.netxms.client.NXCException) AuthenticationType(org.netxms.client.constants.AuthenticationType)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PartInitException (org.eclipse.ui.PartInitException)1 CertificateManager (org.netxms.certificate.manager.CertificateManager)1 NXCException (org.netxms.client.NXCException)1 NXCSession (org.netxms.client.NXCSession)1 AuthenticationType (org.netxms.client.constants.AuthenticationType)1 LoginDialog (org.netxms.ui.eclipse.console.dialogs.LoginDialog)1 LoginJob (org.netxms.ui.eclipse.jobs.LoginJob)1