KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > realm > web > catalina55 > Standard


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Standard.java,v 1.2 2005/04/28 08:43:26 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.security.realm.web.catalina55;
27
28 import java.security.Principal JavaDoc;
29 import java.security.cert.X509Certificate JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import org.apache.catalina.LifecycleException;
33 import org.apache.catalina.realm.GenericPrincipal;
34 import org.apache.catalina.realm.RealmBase;
35
36 import org.objectweb.jonas.common.Log;
37 import org.objectweb.jonas.security.SecurityService;
38 import org.objectweb.jonas.security.realm.factory.JResource;
39 import org.objectweb.jonas.security.realm.factory.JResourceException;
40 import org.objectweb.jonas.security.realm.principals.User;
41 import org.objectweb.jonas.service.ServiceManager;
42
43 import org.objectweb.security.context.SecurityContext;
44 import org.objectweb.security.context.SecurityCurrent;
45
46 import org.objectweb.util.monolog.api.BasicLevel;
47 import org.objectweb.util.monolog.api.Logger;
48
49 /**
50  * <p>
51  * Implementation of a Realm. (by a wrapper) Use any JOnAS realm by specifying
52  * the resource name
53  *
54  * @author Florent Benoit
55  */

56 public class Standard extends RealmBase {
57
58     /**
59      * Descriptive information about this Realm implementation.
60      */

61     private static final String JavaDoc NAME = "JRealmCatalina50";
62
63     /**
64      * Descriptive information about this Realm implementation.
65      */

66     private static final String JavaDoc INFO = "org.objectweb.jonas.security.realm.JRealmCatalina50/1.0";
67
68     /**
69      * The logger used in JOnAS
70      */

71     private static Logger logger = null;
72
73     /**
74      * The resource we will use to authenticate users and identify associated
75      * roles.
76      */

77     private JResource jResource = null;
78
79     /**
80      * The name of the resource
81      */

82     private String JavaDoc resourceName = null;
83
84     /**
85      * Reference to the JOnAS security service
86      */

87     private SecurityService securityService = null;
88
89     /**
90      * Return descriptive information about this Realm implementation and the
91      * corresponding version number, in the format
92      * <code>&lt;description&gt;/&lt;version&gt;</code>.
93      *
94      * @return the info.
95      */

96     public String JavaDoc getInfo() {
97         return INFO;
98     }
99
100     /**
101      * Return the resource name we will be using.
102      *
103      * @return the resource name.
104      */

105     public String JavaDoc getResourceName() {
106         return resourceName;
107     }
108
109     /**
110      * Set the resource name we will be using.
111      *
112      * @param resourceName The new resource name
113      */

114     public void setResourceName(String JavaDoc resourceName) {
115         this.resourceName = resourceName;
116
117     }
118
119     /**
120      * Return the Principal associated with the specified username and
121      * credentials, if there is one; otherwise return <code>null</code>.
122      *
123      * @param username Username of the Principal to look up
124      * @param credentials Password or other credentials to use in authenticating
125      * this username
126      * @return the principal associated
127      */

128     public Principal JavaDoc authenticate(String JavaDoc username, String JavaDoc credentials) {
129
130         // No authentication can be made with a null username
131
if (username == null) {
132             if (logger.isLoggable(BasicLevel.DEBUG)) {
133                 logger.log(BasicLevel.DEBUG, "No username so no authentication");
134             }
135             return null;
136         }
137
138         // Does a user with this username exist?
139
User user = null;
140         try {
141             user = jResource.findUser(username);
142         } catch (Exception JavaDoc jre) {
143             // could not retrieve user
144
logger.log(BasicLevel.ERROR, "Can not find the user : " + jre.getMessage());
145             return null;
146         }
147
148         // User was not found
149
if (user == null) {
150             if (logger.isLoggable(BasicLevel.DEBUG)) {
151                 logger.log(BasicLevel.DEBUG, "User " + username + " not found.");
152             }
153             return null;
154         }
155
156         boolean validated = jResource.isValidUser(user, credentials);
157         if (!validated) {
158             logger.log(BasicLevel.ERROR, "The password for the user " + username + " is not valid");
159             return null;
160         }
161
162         ArrayList JavaDoc combinedRoles = null;
163         try {
164             combinedRoles = jResource.getArrayListCombinedRoles(user);
165         } catch (JResourceException jre) {
166             logger.log(BasicLevel.ERROR, jre.getMessage());
167             return null;
168         }
169
170         GenericPrincipal principal = new GenericPrincipal(this, user.getName(), user.getPassword(), combinedRoles);
171         SecurityContext ctx = new SecurityContext(principal.getName(), combinedRoles);
172         SecurityCurrent current = SecurityCurrent.getCurrent();
173         current.setSecurityContext(ctx);
174
175         return principal;
176     }
177
178     /**
179      * Return the Principal associated with the specified chain of X509 client
180      * certificates. If there is none, return <code>null</code>.
181      *
182      * @param cert Array of client certificates, with the first one in the array
183      * being the certificate of the client itself.
184      * @return the associated Principal
185      */

186     public Principal JavaDoc authenticate(X509Certificate JavaDoc[] cert) {
187         String JavaDoc dn = cert[0].getSubjectDN().getName();
188         return authenticate(dn, "tomcat");
189     }
190
191     /**
192      * Return a short name for this Realm implementation.
193      *
194      * @return the name
195      */

196     protected String JavaDoc getName() {
197         return NAME;
198     }
199
200     /**
201      * Return the password associated with the given principal's user name.
202      *
203      * @param username the given principal's user name.
204      * @return the password associated.
205      */

206     protected String JavaDoc getPassword(String JavaDoc username) {
207         return null;
208     }
209
210     /**
211      * Return the Principal associated with the given user name.
212      *
213      * @param username the given principal's user name.
214      * @return the Principal associated
215      */

216     protected Principal JavaDoc getPrincipal(String JavaDoc username) {
217         return null;
218     }
219
220     /**
221      * Prepare for active use of the public methods of this Component.
222      *
223      * @exception LifecycleException if this component detects a fatal error
224      * that prevents it from being started
225      */

226     public synchronized void start() throws LifecycleException {
227
228         if (logger == null) {
229             logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
230         }
231
232         // Get the Security Service
233
try {
234             securityService = (SecurityService) ServiceManager.getInstance().getSecurityService();
235         } catch (Exception JavaDoc e) {
236             // Can't retrieve Security service
237
throw new LifecycleException("can't retrieve Security service");
238         }
239
240         // Get the resource from the security service
241
jResource = securityService.getJResource(resourceName);
242         if (jResource == null) {
243             throw new LifecycleException("Can't retrieve resource '" + resourceName + "' from the security service");
244         }
245
246         // Perform normal superclass initialization
247
super.start();
248
249     }
250
251     /**
252      * Gracefully shut down active use of the public methods of this Component.
253      *
254      * @exception LifecycleException if this component detects a fatal error
255      * that needs to be reported
256      */

257     public synchronized void stop() throws LifecycleException {
258         // Perform normal superclass finalization
259
super.stop();
260
261         // Release reference to our resource
262
jResource = null;
263     }
264
265     /**
266      * Log a message on the Logger associated with our Container (if any)
267      *
268      * @param message Message to be logged
269      */

270     protected void log(String JavaDoc message) {
271         if (logger.isLoggable(BasicLevel.DEBUG)) {
272             logger.log(BasicLevel.DEBUG, message);
273         }
274     }
275
276 }
Popular Tags