Add ActiveMQ security support
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / activemq / SecuredActiveMqConnectionFactory.java
index 3860d67705f532a0365a64c7530ba5687cb1594e..c873d063d553f8dc4cf0871bc1dd0b2ff22f726c 100644 (file)
@@ -1,9 +1,7 @@
 package org.argeo.security.activemq;
 
 import java.io.InputStream;
-import java.net.URL;
 import java.security.KeyStore;
-import java.security.Principal;
 import java.security.SecureRandom;
 
 import javax.jms.Connection;
@@ -11,9 +9,9 @@ import javax.jms.ConnectionFactory;
 import javax.jms.JMSException;
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.TrustManagerFactory;
-import javax.security.auth.Subject;
-import javax.security.auth.login.LoginContext;
-import javax.security.auth.login.LoginException;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import javax.swing.plaf.metal.MetalLookAndFeel;
 
 import org.apache.activemq.ActiveMQSslConnectionFactory;
 import org.apache.commons.logging.Log;
@@ -32,7 +30,7 @@ public class SecuredActiveMqConnectionFactory implements ConnectionFactory,
        public final static String AUTHMODE_UI = "ui";
        public final static String AUTHMODE_OS = "os";
        public final static String AUTHMODE_DEFAULT = AUTHMODE_OS;
-       private final static String LOGIN_CONFIG_PROPERTY = "java.security.auth.login.config";
+//     private final static String LOGIN_CONFIG_PROPERTY = "java.security.auth.login.config";
 
        private final static Log log = LogFactory
                        .getLog(SecuredActiveMqConnectionFactory.class);
@@ -78,62 +76,75 @@ public class SecuredActiveMqConnectionFactory implements ConnectionFactory,
                        authenticationMode = AUTHMODE_DEFAULT;
 
                if (AUTHMODE_OS.equals(authenticationMode)) {
-                       // Cache previous value of login conf location
-                       String oldLoginConfLocation = System
-                                       .getProperty(LOGIN_CONFIG_PROPERTY);
-                       // Find OS family
-                       String osName = System.getProperty("os.name");
-                       final String auth;
-                       if (osName.startsWith("Windows"))
-                               auth = "Windows";
-                       else if (osName.startsWith("SunOS") || osName.startsWith("Solaris"))
-                               auth = "Solaris";
-                       else
-                               auth = "Unix";
-
-                       Subject subject;
-                       try {
-
-                               URL url = getClass().getResource(
-                                               "/org/argeo/security/activemq/osLogin.conf");
-
-                               System.setProperty(LOGIN_CONFIG_PROPERTY, url.toString());
-                               LoginContext lc = new LoginContext(auth);
-                               lc.login();
-                               subject = lc.getSubject();
-                       } catch (LoginException le) {
-                               throw new ArgeoException("OS authentication failed", le);
-                       } finally {
-                               if (oldLoginConfLocation != null)
-                                       System.setProperty(LOGIN_CONFIG_PROPERTY,
-                                                       oldLoginConfLocation);
-                       }
-
-                       // Extract user name
-                       String osUsername = null;
-                       for (Principal principal : subject.getPrincipals()) {
-                               String className = principal.getClass().getName();
-                               if ("Unix".equals(auth)
-                                               && "com.sun.security.auth.UnixPrincipal"
-                                                               .equals(className))
-                                       osUsername = principal.getName();
-                               else if ("Windows".equals(auth)
-                                               && "com.sun.security.auth.NTUserPrincipal"
-                                                               .equals(className))
-                                       osUsername = principal.getName();
-                               else if ("Solaris".equals(auth)
-                                               && "com.sun.security.auth.SolarisPrincipal"
-                                                               .equals(className))
-                                       osUsername = principal.getName();
-                       }
-
-                       if (osUsername == null)
-                               throw new ArgeoException("Could not find OS user name");
-
-                       uccfa.setUsername(osUsername);
+//                     if (false) {
+//                             // Cache previous value of login conf location
+//                             String oldLoginConfLocation = System
+//                                             .getProperty(LOGIN_CONFIG_PROPERTY);
+//                             // Find OS family
+//                             String osName = System.getProperty("os.name");
+//                             final String auth;
+//                             if (osName.startsWith("Windows"))
+//                                     auth = "Windows";
+//                             else if (osName.startsWith("SunOS")
+//                                             || osName.startsWith("Solaris"))
+//                                     auth = "Solaris";
+//                             else
+//                                     auth = "Unix";
+//
+//                             Subject subject;
+//                             // see http://old.nabble.com/osgi-and-jaas-td23485885.html
+//                             ClassLoader ccl = Thread.currentThread()
+//                                             .getContextClassLoader();
+//                             try {
+//                                     Thread.currentThread().setContextClassLoader(
+//                                                     getClass().getClassLoader());
+//                                     URL url = getClass().getResource(
+//                                                     "/org/argeo/security/activemq/osLogin.conf");
+//
+//                                     System.setProperty(LOGIN_CONFIG_PROPERTY, url.toString());
+//                                     LoginContext lc = new LoginContext(auth);
+//                                     lc.login();
+//                                     subject = lc.getSubject();
+//                             } catch (LoginException le) {
+//                                     throw new ArgeoException("OS authentication failed", le);
+//                             } finally {
+//                                     if (oldLoginConfLocation != null)
+//                                             System.setProperty(LOGIN_CONFIG_PROPERTY,
+//                                                             oldLoginConfLocation);
+//                                     Thread.currentThread().setContextClassLoader(ccl);
+//                             }
+//                             // Extract user name
+//                             String osUsername = null;
+//                             for (Principal principal : subject.getPrincipals()) {
+//                                     String className = principal.getClass().getName();
+//                                     if ("Unix".equals(auth)
+//                                                     && "com.sun.security.auth.UnixPrincipal"
+//                                                                     .equals(className))
+//                                             osUsername = principal.getName();
+//                                     else if ("Windows".equals(auth)
+//                                                     && "com.sun.security.auth.NTUserPrincipal"
+//                                                                     .equals(className))
+//                                             osUsername = principal.getName();
+//                                     else if ("Solaris".equals(auth)
+//                                                     && "com.sun.security.auth.SolarisPrincipal"
+//                                                                     .equals(className))
+//                                             osUsername = principal.getName();
+//                             }
+//
+//                             if (osUsername == null)
+//                                     throw new ArgeoException("Could not find OS user name");
+//                     }
+
+                       uccfa.setUsername(System.getProperty("user.name"));
                        uccfa.setPassword(null);
 
                } else if (AUTHMODE_UI.equals(authenticationMode)) {
+                       try {
+                               UIManager.setLookAndFeel(new MetalLookAndFeel());
+                       } catch (UnsupportedLookAndFeelException e) {
+                               throw new ArgeoException("Cannot load look and feel", e);
+                       }
+                       UIManager.put("ClassLoader", getClass().getClassLoader());
                        UserPasswordDialog dialog = new UserPasswordDialog() {
                                private static final long serialVersionUID = -891646559691412088L;