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 java.io.IOException;
021    import java.security.Principal;
022    import java.util.HashSet;
023    import java.util.Map;
024    import java.util.Set;
025    import javax.security.auth.Subject;
026    import javax.security.auth.callback.Callback;
027    import javax.security.auth.callback.CallbackHandler;
028    import javax.security.auth.callback.PasswordCallback;
029    import javax.security.auth.callback.UnsupportedCallbackException;
030    import javax.security.auth.login.LoginException;
031    import javax.security.auth.spi.LoginModule;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    /**
036     * Always login the user with a default 'guest' identity.
037     *
038     * Useful for unauthenticated communication channels being used in the
039     * same broker as authenticated ones.
040     * 
041     */
042    public class GuestLoginModule implements LoginModule {
043    
044        private static final String GUEST_USER = "org.apache.activemq.jaas.guest.user";
045        private static final String GUEST_GROUP = "org.apache.activemq.jaas.guest.group";
046    
047        private static final Logger LOG = LoggerFactory.getLogger(GuestLoginModule.class);
048        
049    
050        private String userName = "guest";
051        private String groupName = "guests";
052        private Subject subject;
053        private boolean debug;
054        private boolean credentialsInvalidate;
055        private Set<Principal> principals = new HashSet<Principal>();
056        private CallbackHandler callbackHandler;
057        private boolean loginSucceeded;
058    
059    
060        public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
061            this.subject = subject;
062            this.callbackHandler = callbackHandler;
063            debug = "true".equalsIgnoreCase((String)options.get("debug"));
064            credentialsInvalidate = "true".equalsIgnoreCase((String)options.get("credentialsInvalidate"));
065            if (options.get(GUEST_USER) != null) {
066                userName = (String)options.get(GUEST_USER);
067            }
068            if (options.get(GUEST_GROUP) != null) {
069                groupName = (String)options.get(GUEST_GROUP);
070            }
071            principals.add(new UserPrincipal(userName));
072            principals.add(new GroupPrincipal(groupName));
073            
074            if (debug) {
075                LOG.debug("Initialized debug=" + debug + " guestUser=" + userName + " guestGroup=" + groupName);
076            }
077    
078        }
079    
080        public boolean login() throws LoginException {
081            loginSucceeded = true;
082            if (credentialsInvalidate) {
083                PasswordCallback passwordCallback = new PasswordCallback("Password: ", false);
084                try {
085                     callbackHandler.handle(new Callback[]{passwordCallback});
086                     if (passwordCallback.getPassword() != null) {
087                         if (debug) {
088                            LOG.debug("Guest login failing (credentialsInvalidate=true) on presence of a password");
089                         }
090                         loginSucceeded = false;
091                         passwordCallback.clearPassword();
092                     };
093                 } catch (IOException ioe) {
094                 } catch (UnsupportedCallbackException uce) {
095                 }
096            }
097            if (debug) {
098                LOG.debug("Guest login " + loginSucceeded);
099            }
100            return loginSucceeded;
101        }
102    
103        public boolean commit() throws LoginException {
104            if (loginSucceeded) {
105                subject.getPrincipals().addAll(principals);
106            }
107    
108            if (debug) {
109                LOG.debug("commit");
110            }
111            return loginSucceeded;
112        }
113    
114        public boolean abort() throws LoginException {
115    
116            if (debug) {
117                LOG.debug("abort");
118            }
119            return true;
120        }
121    
122        public boolean logout() throws LoginException {
123            subject.getPrincipals().removeAll(principals);
124    
125            if (debug) {
126                LOG.debug("logout");
127            }
128            return true;
129        }
130    }