login dialog no longer ask for empty password and store last used
authorVictor Kirhenshtein <victor@netxms.org>
Sat, 11 Jan 2014 01:35:48 +0000 (03:35 +0200)
committerVictor Kirhenshtein <victor@netxms.org>
Sat, 11 Jan 2014 01:35:48 +0000 (03:35 +0200)
certificate

src/java/certificate-manager/src/main/java/org/netxms/certificate/manager/CertificateManager.java
src/java/netxms-eclipse/Core/.classpath
src/java/netxms-eclipse/Core/src/org/netxms/ui/eclipse/console/dialogs/LoginDialog.java
src/java/netxms-eclipse/Core/src/org/netxms/ui/eclipse/console/dialogs/PasswordRequestDialog.java

index ba46b48..e1d95d6 100644 (file)
@@ -129,42 +129,58 @@ public class CertificateManager
       return verified;
    }
 
+   /**
+    * @param cert
+    * @return
+    * @throws KeyStoreException
+    * @throws CertificateNotInKeyStoreException
+    * @throws CertificateHasNoPrivateKeyException
+    * @throws NoSuchAlgorithmException
+    * @throws UnrecoverableEntryException
+    */
    protected PrivateKey getPrivateKey(Certificate cert)
       throws KeyStoreException, CertificateNotInKeyStoreException, CertificateHasNoPrivateKeyException,
       NoSuchAlgorithmException, UnrecoverableEntryException
    {
       String alias = keyStore.getCertificateAlias(cert);
 
-      if (alias == null) throw new CertificateNotInKeyStoreException();
+      if (alias == null) 
+         throw new CertificateNotInKeyStoreException();
 
       KeyStore.PrivateKeyEntry pkEntry;
 
       try
       {
-         pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
+         pkEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, new KeyStore.PasswordProtection("".toCharArray()));
       }
       catch(UnrecoverableEntryException uee)
       {
          String password = getEntryPassword();
-         KeyStore.ProtectionParameter parameter = new KeyStore.PasswordProtection(password.toCharArray());
-
-         pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, parameter);
+         pkEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, new KeyStore.PasswordProtection(password.toCharArray()));
       }
 
       PrivateKey pk = pkEntry.getPrivateKey();
 
-      if (pk == null) throw new CertificateHasNoPrivateKeyException();
+      if (pk == null) 
+         throw new CertificateHasNoPrivateKeyException();
 
       return pk;
    }
 
+   /**
+    * @return
+    */
    protected String getEntryPassword()
    {
-      if (entryListener == null) return "";
+      if (entryListener == null) 
+         return "";
 
       return entryListener.keyStoreEntryPasswordRequested();
    }
 
+   /**
+    * @throws KeyStoreLoaderException
+    */
    public void load() throws KeyStoreLoaderException
    {
       keyStore = loader.loadKeyStore();
index 9353168..2aeb2b2 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-       <classpathentry exported="true" kind="lib" path="jar/certificate-manager-1.2.11.jar"/>
+       <classpathentry exported="true" kind="lib" path="jar/certificate-manager-1.2.11.jar" sourcepath="/certificate-manager"/>
        <classpathentry exported="true" kind="lib" path="jar/netxms-base-1.2.11.jar" sourcepath="/netxms-base"/>
        <classpathentry exported="true" kind="lib" path="jar/netxms-client-1.2.11.jar" sourcepath="/netxms-client"/>
        <classpathentry exported="true" kind="lib" path="jar/netxms-client-api-1.2.11.jar"/>
index 557a215..543e5a9 100644 (file)
@@ -21,6 +21,7 @@ package org.netxms.ui.eclipse.console.dialogs;
 import java.security.cert.Certificate;
 import java.security.cert.X509Certificate;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.HashSet;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogSettings;
@@ -240,6 +241,7 @@ public class LoginDialog extends Dialog
       {
        authMethod = NXCSession.AUTH_TYPE_PASSWORD;
       }
+      comboAuth.select(authMethod);
       selectAuthenticationField(false);
             
       // Set initial focus
@@ -313,6 +315,8 @@ public class LoginDialog extends Dialog
       settings.put("Connect.ServerHistory", items.toArray(new String[items.size()])); //$NON-NLS-1$
       settings.put("Connect.Login", textLogin.getText()); //$NON-NLS-1$
       settings.put("Connect.AuthMethod", authMethod); //$NON-NLS-1$
+      if (certificate != null)
+         settings.put("Connect.Certificate", ((X509Certificate)certificate).getSubjectDN().toString());
 
       password = textPassword.getText();
       super.okPressed();
@@ -337,6 +341,17 @@ public class LoginDialog extends Dialog
          certificate = certMgr.getCerts()[index];
       }
    }
+   
+   /**
+    * @param c
+    * @return
+    */
+   private static String getCertificateDisplayName(Certificate c)
+   {
+      String subjString = ((X509Certificate)c).getSubjectDN().toString();
+      Subject subj = SubjectParser.parseSubject(subjString);
+      return String.format("%s (%s, %s, %s)", subj.getCommonName(), subj.getOrganization(), subj.getState(), subj.getCountry()); //$NON-NLS-1$
+   }
 
    /**
     * Fill certificate list
@@ -363,23 +378,32 @@ public class LoginDialog extends Dialog
       }
 
       Certificate[] certs = certMgr.getCerts();
+      Arrays.sort(certs, new Comparator<Certificate>() {
+         @Override
+         public int compare(Certificate o1, Certificate o2)
+         {
+            return getCertificateDisplayName(o1).compareToIgnoreCase(getCertificateDisplayName(o2));
+         }
+      });
+      
       String[] subjectStrings = new String[certs.length];
+      
+      IDialogSettings settings = Activator.getDefault().getDialogSettings();
+      String lastSelected = settings.get("Connect.Certificate");
+      int selectionIndex = 0;
 
       for(int i = 0; i < certs.length; i++)
       {
-         X509Certificate x509 = (X509Certificate)certs[i];
-         String subjString = x509.getSubjectDN().toString();
-
-         Subject subj = SubjectParser.parseSubject(subjString);
-
-         subjectStrings[i] = String.format("%s (%s, %s, %s)", subj.getCommonName(), subj.getOrganization(), subj.getState(), //$NON-NLS-1$
-               subj.getCountry());
+         String subject = ((X509Certificate)certs[i]).getSubjectDN().toString();
+         if (subject.equals(lastSelected))
+            selectionIndex = i;
+         subjectStrings[i] = getCertificateDisplayName(certs[i]);
       }
 
       if (subjectStrings.length != 0)
       {
          comboCert.setItems(subjectStrings);
-         comboCert.select(0);
+         comboCert.select(selectionIndex);
          selectCertificate();
          return true;
       }
index 4a74685..33cb5ce 100644 (file)
@@ -20,7 +20,6 @@ package org.netxms.ui.eclipse.console.dialogs;
 
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
@@ -57,7 +56,6 @@ public class PasswordRequestDialog extends Dialog
        protected void configureShell(Shell shell) 
        {
           super.configureShell(shell);
-          
           shell.setText(title);
        }
        
@@ -67,36 +65,29 @@ public class PasswordRequestDialog extends Dialog
        @Override
        protected Control createDialogArea(Composite parent)
        {
-               // TODO: Make this easier on the eyes
                final Composite container = (Composite) super.createDialogArea(parent);
                
                GridLayout layout = new GridLayout();
       layout.verticalSpacing = WidgetHelper.DIALOG_SPACING;
       layout.marginWidth = WidgetHelper.DIALOG_WIDTH_MARGIN;
       layout.marginHeight = WidgetHelper.DIALOG_HEIGHT_MARGIN;
-               
       container.setLayout(layout);
       
       lblMessage = new Label(container, SWT.NONE | SWT.WRAP);
       lblMessage.setText(message);
-      lblMessage.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+      lblMessage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
       
                textPassword = new LabeledText(container, SWT.NONE, SWT.BORDER | SWT.PASSWORD);
                textPassword.setLabel(Messages.get().LoginDialog_Passwd);
-               textPassword.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
+               GridData gd = new GridData();
+               gd.grabExcessHorizontalSpace = true;
+               gd.horizontalAlignment = SWT.FILL;
+               gd.widthHint = 400;
+               textPassword.setLayoutData(gd);
                
                return container;
        }       
        
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
-        */
-       @Override
-   protected Point getInitialSize()
-   {
-      return new Point(300, 210);
-   }
-
    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */