From 9dcd00823b64553a153ed1f1651f574ad8875aad Mon Sep 17 00:00:00 2001 From: "Jonas B. Lim" Date: Fri, 17 Nov 2006 10:24:04 +0000 Subject: [PATCH] applied patch for http://issues.apache.org/activemq/browse/AMQ-1010 git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@476099 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/security/AuthenticationUser.java | 64 +++++++++++++++++++ .../security/SimpleAuthenticationPlugin.java | 39 ++++++++++- .../SimpleAuthenticationPluginTest.java | 52 +++++++++++++++ .../activemq/security/simple-auth-broker.xml | 59 +++++++++++++++++ 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 activemq-core/src/main/java/org/apache/activemq/security/AuthenticationUser.java create mode 100644 activemq-core/src/test/java/org/apache/activemq/security/SimpleAuthenticationPluginTest.java create mode 100644 activemq-core/src/test/resources/org/apache/activemq/security/simple-auth-broker.xml diff --git a/activemq-core/src/main/java/org/apache/activemq/security/AuthenticationUser.java b/activemq-core/src/main/java/org/apache/activemq/security/AuthenticationUser.java new file mode 100644 index 0000000000..aebc923443 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/security/AuthenticationUser.java @@ -0,0 +1,64 @@ +/** + * + * 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.activemq.security; + + +/** + * A helper object used to configure simple authentiaction plugin + * + * @org.apache.xbean.XBean + * + * @version $Revision + */ +public class AuthenticationUser { + + String username; + String password; + String group; + + + + public AuthenticationUser(String username, String password, String group) { + this.username = username; + this.password = password; + this.group = group; + } + + + public String getGroup() { + return group; + } + public void setGroup(String group) { + this.group = group; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + + + +} diff --git a/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java b/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java index 81cd855a23..5001ea746b 100644 --- a/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java +++ b/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java @@ -17,6 +17,16 @@ */ package org.apache.activemq.security; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; + +import org.apache.activemq.jaas.GroupPrincipal; + import org.apache.activemq.broker.Broker; import org.apache.activemq.broker.BrokerPlugin; @@ -26,7 +36,7 @@ import java.util.Map; * A simple authentication plugin * * @org.apache.xbean.XBean element="simpleAuthenticationPlugin" description="Provides a simple authentication - * plugin configured with a map of user-passwords and a map of user-groups" + * plugin configured with a map of user-passwords and a map of user-groups or a list of authentication users" * * @version $Revision$ */ @@ -34,6 +44,12 @@ public class SimpleAuthenticationPlugin implements BrokerPlugin { private Map userPasswords; private Map userGroups; + public SimpleAuthenticationPlugin() {} + + public SimpleAuthenticationPlugin(List users) { + setUsers(users); + } + public Broker installPlugin(Broker broker) { return new SimpleAuthenticationBroker(broker, userPasswords, userGroups); } @@ -41,6 +57,27 @@ public class SimpleAuthenticationPlugin implements BrokerPlugin { public Map getUserGroups() { return userGroups; } + + /** + * Sets individual users for authentication + * + * @org.apache.xbean.ElementType class="org.apache.activemq.security.AuthenticationUser" + */ + public void setUsers(List users) { + userPasswords = new HashMap(); + userGroups = new HashMap(); + for (Iterator it = users.iterator(); it.hasNext();) { + AuthenticationUser user = (AuthenticationUser)it.next(); + userPasswords.put(user.getUsername(), user.getPassword()); + Set groups = new HashSet(); + StringTokenizer iter = new StringTokenizer(user.getGroup(), ","); + while (iter.hasMoreTokens()) { + String name = iter.nextToken().trim(); + groups.add(new GroupPrincipal(name)); + } + userGroups.put(user.getUsername(), groups); + } + } /** * Sets the groups a user is in. The key is the user name and the value is a Set of groups diff --git a/activemq-core/src/test/java/org/apache/activemq/security/SimpleAuthenticationPluginTest.java b/activemq-core/src/test/java/org/apache/activemq/security/SimpleAuthenticationPluginTest.java new file mode 100644 index 0000000000..d883f0e17e --- /dev/null +++ b/activemq-core/src/test/java/org/apache/activemq/security/SimpleAuthenticationPluginTest.java @@ -0,0 +1,52 @@ +/** + * + * 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.activemq.security; + +import java.net.URI; + +import junit.framework.Test; + +import org.apache.activemq.broker.BrokerFactory; +import org.apache.activemq.broker.BrokerService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class SimpleAuthenticationPluginTest extends SecurityTestSupport { + + private static final Log log = LogFactory.getLog(XBeanSecurityTest.class); + + public static Test suite() { + return suite(XBeanSecurityTest.class); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + + protected BrokerService createBroker() throws Exception { + return createBroker("org/apache/activemq/security/simple-auth-broker.xml"); + } + + protected BrokerService createBroker(String uri) throws Exception { + log.info("Loading broker configuration from the classpath with URI: " + uri); + return BrokerFactory.createBroker(new URI("xbean:" + uri)); + } + + +} \ No newline at end of file diff --git a/activemq-core/src/test/resources/org/apache/activemq/security/simple-auth-broker.xml b/activemq-core/src/test/resources/org/apache/activemq/security/simple-auth-broker.xml new file mode 100644 index 0000000000..10c33338ac --- /dev/null +++ b/activemq-core/src/test/resources/org/apache/activemq/security/simple-auth-broker.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file