You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2009/01/05 10:09:06 UTC

svn commit: r731497 - in /servicemix/smx4/kernel/trunk/jaas/jaas-modules: ./ src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/ src/main/resources/META-INF/spring/

Author: gnodet
Date: Mon Jan  5 01:09:06 2009
New Revision: 731497

URL: http://svn.apache.org/viewvc?rev=731497&view=rev
Log:
SMX4KNL-162: do not use the config admin to store clear passwords

Added:
    servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/
    servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/PropertiesLoginModule.java
Modified:
    servicemix/smx4/kernel/trunk/jaas/jaas-modules/pom.xml
    servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/resources/META-INF/spring/servicemix-jaas-module.xml

Modified: servicemix/smx4/kernel/trunk/jaas/jaas-modules/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/jaas/jaas-modules/pom.xml?rev=731497&r1=731496&r2=731497&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/jaas/jaas-modules/pom.xml (original)
+++ servicemix/smx4/kernel/trunk/jaas/jaas-modules/pom.xml Mon Jan  5 01:09:06 2009
@@ -80,6 +80,7 @@
                         </Export-Package>
                         <Import-Package>
                             org.apache.servicemix.kernel.jaas.config,
+                            org.springframework.beans.factory.config,
                             *
                         </Import-Package>
                         <Spring-Context>*;publish-context:=false;create-asynchronously:=false</Spring-Context>

Added: servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/PropertiesLoginModule.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/PropertiesLoginModule.java?rev=731497&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/PropertiesLoginModule.java (added)
+++ servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/java/org/apache/servicemix/kernel/jaas/modules/properties/PropertiesLoginModule.java Mon Jan  5 01:09:06 2009
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.kernel.jaas.modules.properties;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.security.Principal;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.FailedLoginException;
+import javax.security.auth.login.LoginException;
+import javax.security.auth.spi.LoginModule;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.kernel.jaas.modules.UserPrincipal;
+import org.apache.servicemix.kernel.jaas.modules.RolePrincipal;
+
+/**
+ * JAAS Login module for user / password, based on two properties files.
+ *
+ */
+public class PropertiesLoginModule implements LoginModule {
+
+    private static final String USER_FILE = "users";
+    private static final Log LOG = LogFactory.getLog(PropertiesLoginModule.class);
+
+    private Subject subject;
+    private CallbackHandler callbackHandler;
+    private boolean debug;
+    private String usersFile;
+    private String user;
+    private Set principals = new HashSet();
+
+    public void initialize(Subject sub, CallbackHandler handler, Map sharedState, Map options) {
+        this.subject = sub;
+        this.callbackHandler = handler;
+
+        debug = "true".equalsIgnoreCase((String) options.get("debug"));
+        usersFile = (String) options.get(USER_FILE) + "";
+
+        if (debug) {
+            LOG.debug("Initialized debug=" + debug + " usersFile=" + usersFile);
+        }
+    }
+
+    public boolean login() throws LoginException {
+        Properties users = new Properties();
+        File f = new File(usersFile);
+        try {
+            users.load(new java.io.FileInputStream(f));
+        } catch (IOException ioe) {
+            throw new LoginException("Unable to load user properties file " + f);
+        }
+
+        Callback[] callbacks = new Callback[2];
+
+        callbacks[0] = new NameCallback("Username: ");
+        callbacks[1] = new PasswordCallback("Password: ", false);
+        try {
+            callbackHandler.handle(callbacks);
+        } catch (IOException ioe) {
+            throw new LoginException(ioe.getMessage());
+        } catch (UnsupportedCallbackException uce) {
+            throw new LoginException(uce.getMessage() + " not available to obtain information from user");
+        }
+        user = ((NameCallback) callbacks[0]).getName();
+        char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
+        if (tmpPassword == null) {
+            tmpPassword = new char[0];
+        }
+
+        String userInfos = (String) users.get(user);
+        if (userInfos == null) {
+            throw new FailedLoginException("User does not exist");
+        }
+        String[] infos = userInfos.split(",");
+        if (!new String(tmpPassword).equals(infos[0])) {
+            throw new FailedLoginException("Password does not match");
+        }
+
+        principals = new HashSet<Principal>();
+        principals.add(new UserPrincipal(user));
+        for (int i = 1; i < infos.length; i++) {
+            principals.add(new RolePrincipal(infos[i]));
+        }
+
+        users.clear();
+
+        if (debug) {
+            LOG.debug("login " + user);
+        }
+        return true;
+    }
+
+    public boolean commit() throws LoginException {
+        subject.getPrincipals().addAll(principals);
+        clear();
+        if (debug) {
+            LOG.debug("commit");
+        }
+        return true;
+    }
+
+    public boolean abort() throws LoginException {
+        clear();
+        if (debug) {
+            LOG.debug("abort");
+        }
+        return true;
+    }
+
+    public boolean logout() throws LoginException {
+        subject.getPrincipals().removeAll(principals);
+        principals.clear();
+        if (debug) {
+            LOG.debug("logout");
+        }
+        return true;
+    }
+
+    private void clear() {
+        user = null;
+    }
+}

Modified: servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/resources/META-INF/spring/servicemix-jaas-module.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/resources/META-INF/spring/servicemix-jaas-module.xml?rev=731497&r1=731496&r2=731497&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/resources/META-INF/spring/servicemix-jaas-module.xml (original)
+++ servicemix/smx4/kernel/trunk/jaas/jaas-modules/src/main/resources/META-INF/spring/servicemix-jaas-module.xml Mon Jan  5 01:09:06 2009
@@ -29,6 +29,7 @@
   http://servicemix.apache.org/jaas
   http://servicemix.apache.org/schema/servicemix-jaas.xsd">
 
+    <!--
     <bean id="configAdminHolder" class="org.apache.servicemix.kernel.jaas.modules.osgi.ConfigAdminHolder">
         <property name="service" ref="configAdmin" />
     </bean>
@@ -40,5 +41,15 @@
             pid = org.apache.servicemix.users
         </jaas:module>
     </jaas:config>
+    -->
+
+    <!-- Bean to allow the ${servicemix.home} property to be correctly resolved -->
+    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
+
+    <jaas:config id="servicemix">
+        <jaas:module className="org.apache.servicemix.kernel.jaas.modules.properties.PropertiesLoginModule" flags="required">
+            users = ${servicemix.home}/etc/users.properties
+        </jaas:module>
+    </jaas:config>
 
 </beans>
\ No newline at end of file