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);
}
}
Aggregations