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.servicemix.http.jetty;
018    
019    import java.security.GeneralSecurityException;
020    import java.security.Principal;
021    import java.util.Map;
022    import java.util.concurrent.ConcurrentHashMap;
023    
024    import javax.security.auth.Subject;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.servicemix.common.security.AuthenticationService;
029    import org.mortbay.jetty.Request;
030    import org.mortbay.jetty.security.UserRealm;
031    
032    /**
033     * A JAAS based implementation of a realm for jetty 6.
034     * 
035     * @author gnodet
036     */
037    public class JaasUserRealm implements UserRealm {
038    
039        private static final Log LOG = LogFactory.getLog(JaasUserRealm.class);
040    
041        private String name = getClass().getName();
042        private String domain = "servicemix-domain";
043        private AuthenticationService authenticationService;
044        private final Map<String, JaasJettyPrincipal> userMap = new ConcurrentHashMap<String, JaasJettyPrincipal>();
045    
046        /**
047         * @return the authenticationService
048         */
049        public AuthenticationService getAuthenticationService() {
050            return authenticationService;
051        }
052    
053        /**
054         * @param authenticationService
055         *            the authenticationService to set
056         */
057        public void setAuthenticationService(AuthenticationService authenticationService) {
058            this.authenticationService = authenticationService;
059        }
060    
061        /**
062         * @return the domain
063         */
064        public String getDomain() {
065            return domain;
066        }
067    
068        /**
069         * @param domain
070         *            the domain to set
071         */
072        public void setDomain(String domain) {
073            this.domain = domain;
074        }
075    
076        /**
077         * @return the name
078         */
079        public String getName() {
080            return this.name;
081        }
082    
083        /**
084         * @param name
085         *            the name to set
086         */
087        public void setName(String name) {
088            this.name = name;
089        }
090    
091        public Principal authenticate(final String username, final Object credentials, Request request) {
092            try {
093                if ((username != null) && (!username.equals(""))) {
094    
095                    JaasJettyPrincipal userPrincipal = userMap.get(username);
096    
097                    // user has been previously authenticated, but
098                    // re-authentication has been requested, so remove them
099                    if (userPrincipal != null) {
100                        userMap.remove(username);
101                    }
102    
103                    // set up the login context
104                    Subject subject = new Subject();
105                    authenticationService.authenticate(subject, domain, username, credentials);
106                    // login success
107                    userPrincipal = new JaasJettyPrincipal(username);
108                    userPrincipal.setSubject(subject);
109    
110                    userMap.put(username, userPrincipal);
111    
112                    return userPrincipal;
113                } else {
114                    LOG.debug("Login Failed - null userID");
115                    return null;
116                }
117    
118            } catch (GeneralSecurityException e) {
119                LOG.debug("Login Failed", e);
120                return null;
121            }
122        }
123    
124        public void disassociate(Principal user) {
125        }
126    
127        public Principal getPrincipal(String username) {
128            return userMap.get(username);
129        }
130    
131        public boolean isUserInRole(Principal user, String role) {
132            // TODO: ?
133            return false;
134        }
135    
136        public void logout(Principal user) {
137            JaasJettyPrincipal principal = (JaasJettyPrincipal) user;
138            userMap.remove(principal.getName());
139        }
140    
141        public Principal popRole(Principal user) {
142            // TODO: ?
143            return null;
144        }
145    
146        public Principal pushRole(Principal user, String role) {
147            // TODO: ?
148            return null;
149        }
150    
151        public boolean reauthenticate(Principal user) {
152            // get the user out of the cache
153            return userMap.get(user.getName()) != null;
154        }
155    
156    }