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
018 package org.apache.activemq.jaas;
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022
023 import javax.security.auth.Subject;
024 import javax.security.auth.callback.CallbackHandler;
025 import javax.security.auth.login.LoginException;
026 import javax.security.auth.spi.LoginModule;
027 import java.security.Principal;
028 import java.util.HashSet;
029 import java.util.Map;
030 import java.util.Set;
031
032 /**
033 * Always login the user with a default 'guest' identity.
034 *
035 * Useful for unauthenticated communication channels being used in the
036 * same broker as authenticated ones.
037 *
038 */
039 public class GuestLoginModule implements LoginModule {
040
041 private static final String GUEST_USER = "org.apache.activemq.jaas.guest.user";
042 private static final String GUEST_GROUP = "org.apache.activemq.jaas.guest.group";
043
044 private static final Log LOG = LogFactory.getLog(GuestLoginModule.class);
045
046
047 private String userName = "guest";
048 private String groupName = "guests";
049 private Subject subject;
050 private boolean debug;
051 private Set<Principal> principals = new HashSet<Principal>();
052
053
054 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
055 this.subject = subject;
056
057 debug = "true".equalsIgnoreCase((String)options.get("debug"));
058 if (options.get(GUEST_USER) != null) {
059 userName = (String)options.get(GUEST_USER);
060 }
061 if (options.get(GUEST_GROUP) != null) {
062 groupName = (String)options.get(GUEST_GROUP);
063 }
064 principals.add(new UserPrincipal(userName));
065 principals.add(new GroupPrincipal(groupName));
066
067 if (debug) {
068 LOG.debug("Initialized debug=" + debug + " guestUser=" + userName + " guestGroup=" + groupName);
069 }
070
071 }
072
073 public boolean login() throws LoginException {
074
075 if (debug) {
076 LOG.debug("login " + userName);
077 }return true;
078 }
079
080 public boolean commit() throws LoginException {
081 subject.getPrincipals().addAll(principals);
082
083 if (debug) {
084 LOG.debug("commit");
085 }
086 return true;
087 }
088
089 public boolean abort() throws LoginException {
090
091 if (debug) {
092 LOG.debug("abort");
093 }
094 return true; }
095
096 public boolean logout() throws LoginException {
097 subject.getPrincipals().removeAll(principals);
098
099 if (debug) {
100 LOG.debug("logout");
101 }
102 return true;
103 }
104 }