KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > jetty > JaasUserRealm


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http.jetty;
18
19 import java.security.GeneralSecurityException JavaDoc;
20 import java.security.Principal JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.security.auth.Subject JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
28 import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService;
29 import org.mortbay.jetty.Request;
30 import org.mortbay.jetty.security.UserRealm;
31
32 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
33
34 /**
35  * A JAAS based implementation of a realm for jetty 6.
36  *
37  * @author gnodet
38  */

39 public class JaasUserRealm implements UserRealm {
40     
41     private static final Log log = LogFactory.getLog(JaasUserRealm.class);
42     
43     private String JavaDoc name = getClass().getName();
44     private String JavaDoc domain = "servicemix-domain";
45     private AuthenticationService authenticationService = new JAASAuthenticationService();
46     private final Map JavaDoc userMap = new ConcurrentHashMap();
47
48     /**
49      * @return the authenticationService
50      */

51     public AuthenticationService getAuthenticationService() {
52         return authenticationService;
53     }
54
55     /**
56      * @param authenticationService the authenticationService to set
57      */

58     public void setAuthenticationService(AuthenticationService authenticationService) {
59         this.authenticationService = authenticationService;
60     }
61
62     /**
63      * @return the domain
64      */

65     public String JavaDoc getDomain() {
66         return domain;
67     }
68
69     /**
70      * @param domain the domain to set
71      */

72     public void setDomain(String JavaDoc domain) {
73         this.domain = domain;
74     }
75
76     /**
77      * @return the name
78      */

79     public String JavaDoc getName() {
80         return this.name;
81     }
82
83     /**
84      * @param name the name to set
85      */

86     public void setName(String JavaDoc name) {
87         this.name = name;
88     }
89     
90     public Principal JavaDoc authenticate(final String JavaDoc username, final Object JavaDoc credentials, Request request) {
91         try {
92             if ((username != null) && (!username.equals(""))) {
93
94                 JaasJettyPrincipal userPrincipal = (JaasJettyPrincipal) userMap.get(username);
95
96                 //user has been previously authenticated, but
97
//re-authentication has been requested, so remove them
98
if (userPrincipal != null) {
99                     userMap.remove(username);
100                 }
101
102                 //set up the login context
103
Subject JavaDoc subject = new Subject JavaDoc();
104                 authenticationService.authenticate(subject, domain, username, credentials);
105                 //login success
106
userPrincipal = new JaasJettyPrincipal(username);
107                 userPrincipal.setSubject(subject);
108
109                 userMap.put(username, userPrincipal);
110
111                 return userPrincipal;
112             } else {
113                 log.debug("Login Failed - null userID");
114                 return null;
115             }
116
117         } catch (GeneralSecurityException JavaDoc e) {
118             log.debug("Login Failed", e);
119             return null;
120         }
121     }
122
123     public void disassociate(Principal JavaDoc user) {
124     }
125
126     public Principal JavaDoc getPrincipal(String JavaDoc username) {
127         return (Principal JavaDoc) userMap.get(username);
128     }
129
130     public boolean isUserInRole(Principal JavaDoc user, String JavaDoc role) {
131         // TODO: ?
132
return false;
133     }
134
135     public void logout(Principal JavaDoc user) {
136         JaasJettyPrincipal principal = (JaasJettyPrincipal) user;
137         userMap.remove(principal.getName());
138     }
139
140     public Principal JavaDoc popRole(Principal JavaDoc user) {
141         // TODO: ?
142
return null;
143     }
144
145     public Principal JavaDoc pushRole(Principal JavaDoc user, String JavaDoc role) {
146         // TODO: ?
147
return null;
148     }
149
150     public boolean reauthenticate(Principal JavaDoc user) {
151         // get the user out of the cache
152
return (userMap.get(user.getName()) != null);
153     }
154
155 }
156
Popular Tags