KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > jaas > JaasSimpleAuthenticationProvider


1 /*
2  * $Id: JaasSimpleAuthenticationProvider.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.jaas;
12
13 import java.io.IOException JavaDoc;
14 import java.security.Security JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
19 import javax.security.auth.login.Configuration JavaDoc;
20 import javax.security.auth.login.LoginContext JavaDoc;
21 import javax.security.auth.login.LoginException JavaDoc;
22
23 import org.mule.config.i18n.Messages;
24 import org.mule.impl.security.MuleAuthentication;
25 import org.mule.umo.lifecycle.InitialisationException;
26 import org.mule.umo.security.UMOAuthentication;
27 import org.mule.umo.security.UMOSecurityContext;
28 import org.mule.umo.security.UMOSecurityContextFactory;
29 import org.mule.umo.security.UMOSecurityProvider;
30 import org.mule.umo.security.UnauthorisedException;
31 import org.mule.umo.security.UnknownAuthenticationTypeException;
32
33 /**
34  * @author Marie.Rizzo This is the Provider for Mule's Jaas Security
35  */

36 public class JaasSimpleAuthenticationProvider implements UMOSecurityProvider
37 {
38
39     private String JavaDoc loginConfig;
40     private String JavaDoc loginContextName;
41     private String JavaDoc credentials;
42     private String JavaDoc loginModule;
43     private String JavaDoc defaultModule = "org.mule.extras.jaas.loginmodule.DefaultLoginModule";
44     private String JavaDoc name;
45     private UMOSecurityContextFactory factory;
46
47     // ~ Getters and Setters
48
// ================================================================
49

50     /**
51      * Sets the login Configuration
52      *
53      * @param loginConfig
54      */

55     public final void setLoginConfig(String JavaDoc loginConfig)
56     {
57         this.loginConfig = loginConfig;
58     }
59
60     /**
61      * Gets the Login Configuration
62      *
63      * @return loginConfig
64      */

65     public final String JavaDoc getLoginConfig()
66     {
67         return loginConfig;
68     }
69
70     /**
71      * Sets the Login Context name
72      *
73      * @param loginContextName
74      */

75     public final void setLoginContextName(String JavaDoc loginContextName)
76     {
77         this.loginContextName = loginContextName;
78     }
79
80     /**
81      * Gets the Login Context Name
82      *
83      * @return loginContextName
84      */

85     public final String JavaDoc getLoginContextName()
86     {
87         return loginContextName;
88     }
89
90     /**
91      * Gets the user's credentials, i.e. the username and password
92      *
93      * @return credentials
94      */

95     public final String JavaDoc getCredentials()
96     {
97         return credentials;
98     }
99
100     /**
101      * Sets the user's credentials.
102      *
103      * @param credentials
104      */

105     public final void setCredentials(String JavaDoc credentials)
106     {
107         this.credentials = credentials;
108     }
109
110     /**
111      * Gets the login module name
112      *
113      * @return loginModule
114      */

115     public final String JavaDoc getLoginModule()
116     {
117         return loginModule;
118     }
119
120     /**
121      * sets the login module name
122      *
123      * @param loginModule
124      */

125     public final void setLoginModule(String JavaDoc loginModule)
126     {
127         this.loginModule = loginModule;
128     }
129
130     /**
131      * @return name
132      */

133     public final String JavaDoc getName()
134     {
135         return name;
136     }
137
138     /**
139      * @param name
140      */

141     public final void setName(String JavaDoc name)
142     {
143         this.name = name;
144     }
145
146     // ~ Methods ================================================================
147

148     /**
149      * @throws IOException The configureJaas method gets the resource path of the
150      * jaas configuration file and constructs the URL for the login
151      * configuration.
152      */

153     private void configureJaas() throws IOException JavaDoc
154     {
155
156         String JavaDoc loginConfigUrl = "file://"
157                                 + org.mule.util.FileUtils.getResourcePath(loginConfig,
158                                     JaasSimpleAuthenticationProvider.class);
159
160         boolean alreadySet = false;
161
162         int n = 1;
163         String JavaDoc prefix = "login.config.url.";
164         String JavaDoc existing = null;
165
166         while ((existing = Security.getProperty(prefix + n)) != null)
167         {
168             alreadySet = existing.equals(loginConfigUrl);
169
170             if (alreadySet)
171             {
172                 break;
173             }
174             n++;
175         }
176
177         if (!alreadySet)
178         {
179             String JavaDoc key = prefix + n;
180             Security.setProperty(key, loginConfigUrl);
181         }
182     }
183
184     /**
185      * The authenticate method first creates the jaas Login Context using the
186      * callback handler and the name of the class or directory to prtect. If the
187      * Login Context is successfully created, it will then attempt to login.
188      *
189      * @param UMOAuthentication
190      * @return UMOAuthentication
191      * @throws org.mule.umo.security.SecurityException
192      */

193     public final UMOAuthentication authenticate(UMOAuthentication authentication)
194         throws org.mule.umo.security.SecurityException
195     {
196
197         LoginContext JavaDoc loginContext;
198         MuleAuthentication auth = (MuleAuthentication)authentication;
199
200         // Create the Mule Callback Handler
201
MuleCallbackHandler cbh = new MuleCallbackHandler(auth);
202
203         // Create the LoginContext object, and pass it to the CallbackHandler
204
try
205         {
206             loginContext = new LoginContext JavaDoc(loginContextName, cbh);
207         }
208         catch (LoginException JavaDoc e)
209         {
210             throw new org.mule.umo.security.UnauthorisedException(new org.mule.config.i18n.Message(
211                 org.mule.config.i18n.Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, loginContextName));
212         }
213
214         // Attempt to login the user
215
try
216         {
217             loginContext.login();
218         }
219         catch (LoginException JavaDoc le)
220         {
221             throw new UnauthorisedException(new org.mule.config.i18n.Message(
222                 org.mule.config.i18n.Messages.AUTH_FAILED_FOR_USER_X, auth.getPrincipal()));
223         }
224
225         auth.setAuthenticated(true);
226
227         return auth;
228     }
229
230     /**
231      * checks whether the class is supported.
232      *
233      * @return
234      * @param aClass
235      */

236     public final boolean supports(Class JavaDoc aClass)
237     {
238         return UMOAuthentication.class.isAssignableFrom(aClass);
239     }
240
241     /**
242      * @throws UnknownAuthenticationTypeException This occurs when the Security
243      * Factory cannot be created
244      * @return
245      */

246     public final UMOSecurityContext createSecurityContext(UMOAuthentication auth)
247         throws UnknownAuthenticationTypeException
248     {
249         return factory.create(auth);
250     }
251
252     /**
253      * The initialise method checks whether a jaas configuration file exists. If it
254      * exists, it will call the configureJaas() method to create the context URL of
255      * that file. If such a configuration file is not present, it will then try to
256      * configure jaas programmatically. It also attempts to create the
257      * JaasSecurityContextFactory.
258      *
259      * @throws InitialisationException
260      */

261     public final void initialise() throws InitialisationException
262     {
263         // configure jaas from properties passed to the provider from the Mule XML
264
// configuration file
265
if (loginConfig == null)
266         {
267             try
268             {
269                 AppConfigurationEntry JavaDoc entry = null;
270                 JaasConfig.init();
271
272                 HashMap JavaDoc options = new HashMap JavaDoc();
273                 options.put("credentials", credentials);
274
275                 // if a custom login module is not found, it will use the Default
276
// Login Module
277
if (loginModule != null)
278                 {
279                     entry = new AppConfigurationEntry JavaDoc(loginModule,
280                         AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
281                 }
282                 else
283                 {
284                     entry = new AppConfigurationEntry JavaDoc(defaultModule,
285                         AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
286                 }
287
288                 JaasConfig.addApplicationConfigEntry(loginContextName, entry);
289             }
290             catch (Exception JavaDoc e)
291             {
292                 throw new InitialisationException(e, this);
293             }
294         }
295         else
296         {
297             // configure jaas from a jaas configuration file
298
try
299             {
300                 configureJaas();
301             }
302             catch (IOException JavaDoc e)
303             {
304                 throw new InitialisationException(e, this);
305             }
306         }
307
308         // create the Jaas SecurityContext Factory
309
try
310         {
311             factory = new JaasSecurityContextFactory();
312         }
313         catch (Exception JavaDoc e)
314         {
315             throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X,
316                 "JaasProvider"), e);
317         }
318     }
319
320     /**
321      * The JaasConfig class extends the Jaas Configuration in order to be able to
322      * configure the jaas security programmatically.
323      */

324     public static class JaasConfig extends Configuration JavaDoc
325     {
326
327         private static Map JavaDoc appConfigEntries = new HashMap JavaDoc();
328         private static JaasConfig jaasConfig;
329
330         /**
331          * Initializes and sets the Jaas Configuration
332          */

333         public static void init()
334         {
335             jaasConfig = new JaasConfig();
336             Configuration.setConfiguration(jaasConfig);
337         }
338
339         /**
340          * Returns the Jas Configuration
341          *
342          * @return jaasConfig
343          */

344         public static JaasConfig getJaasConfig()
345         {
346             return jaasConfig;
347         }
348
349         /**
350          * Adds the Configuration Entries
351          *
352          * @param name
353          * @param entry
354          */

355         public static void addApplicationConfigEntry(String JavaDoc name, AppConfigurationEntry JavaDoc entry)
356         {
357             appConfigEntries.put(name, entry);
358         }
359
360         /**
361          * Gets the configuration entries using the application Name
362          *
363          * @param applicationName
364          * @return
365          */

366         public final AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(String JavaDoc applicationName)
367         {
368
369             if (applicationName == null)
370             {
371                 throw new NullPointerException JavaDoc("applicationName passed in was null.");
372             }
373
374             AppConfigurationEntry JavaDoc entry = (AppConfigurationEntry JavaDoc)appConfigEntries.get(applicationName);
375             if (entry == null)
376             {
377                 return new AppConfigurationEntry JavaDoc[]{};
378             }
379             else
380             {
381                 AppConfigurationEntry JavaDoc e[] = new AppConfigurationEntry JavaDoc[1];
382                 e[0] = entry;
383                 return e;
384             }
385         }
386
387         public void refresh()
388         {
389             // Nothing to do here
390
}
391     }
392 }
393
Popular Tags