001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.security;
018    
019    import java.util.HashMap;
020    import java.util.HashSet;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.StringTokenizer;
026    
027    import org.apache.activemq.broker.Broker;
028    import org.apache.activemq.broker.BrokerPlugin;
029    import org.apache.activemq.jaas.GroupPrincipal;
030    
031    /**
032     * A simple authentication plugin
033     * 
034     * @org.apache.xbean.XBean element="simpleAuthenticationPlugin"
035     *                         description="Provides a simple authentication plugin
036     *                         configured with a map of user-passwords and a map of
037     *                         user-groups or a list of authentication users"
038     * 
039     * @version $Revision: 564679 $
040     */
041    public class SimpleAuthenticationPlugin implements BrokerPlugin {
042        private Map<String, String> userPasswords;
043        private Map<String, Set<GroupPrincipal>> userGroups;
044    
045        public SimpleAuthenticationPlugin() {
046        }
047    
048        public SimpleAuthenticationPlugin(List users) {
049            setUsers(users);
050        }
051    
052        public Broker installPlugin(Broker broker) {
053            return new SimpleAuthenticationBroker(broker, userPasswords, userGroups);
054        }
055    
056        public Map<String, Set<GroupPrincipal>> getUserGroups() {
057            return userGroups;
058        }
059    
060        /**
061         * Sets individual users for authentication
062         * 
063         * @org.apache.xbean.ElementType class="org.apache.activemq.security.AuthenticationUser"
064         */
065        public void setUsers(List users) {
066            userPasswords = new HashMap<String, String>();
067            userGroups = new HashMap<String, Set<GroupPrincipal>>();
068            for (Iterator it = users.iterator(); it.hasNext();) {
069                AuthenticationUser user = (AuthenticationUser)it.next();
070                userPasswords.put(user.getUsername(), user.getPassword());
071                Set<GroupPrincipal> groups = new HashSet<GroupPrincipal>();
072                StringTokenizer iter = new StringTokenizer(user.getGroups(), ",");
073                while (iter.hasMoreTokens()) {
074                    String name = iter.nextToken().trim();
075                    groups.add(new GroupPrincipal(name));
076                }
077                userGroups.put(user.getUsername(), groups);
078            }
079        }
080    
081        /**
082         * Sets the groups a user is in. The key is the user name and the value is a
083         * Set of groups
084         */
085        public void setUserGroups(Map<String, Set<GroupPrincipal>> userGroups) {
086            this.userGroups = userGroups;
087        }
088    
089        public Map<String, String> getUserPasswords() {
090            return userPasswords;
091        }
092    
093        /**
094         * Sets the map indexed by user name with the value the password
095         */
096        public void setUserPasswords(Map<String, String> userPasswords) {
097            this.userPasswords = userPasswords;
098        }
099    
100    }