KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > authentication > manager > AuthenticationHelper


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.authentication.manager;
29
30 import java.security.NoSuchAlgorithmException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 import net.sf.jguard.core.CoreConstants;
40 import net.sf.jguard.core.authentication.configuration.ConfigurationHelper;
41 import net.sf.jguard.core.authentication.configuration.JGuardConfiguration;
42 import net.sf.jguard.ext.SecurityConstants;
43 import net.sf.jguard.ext.authentication.AuthenticationException;
44 import net.sf.jguard.ext.util.CryptUtils;
45 import net.sf.jguard.ext.util.XMLUtils;
46
47 import org.dom4j.Document;
48 import org.dom4j.Element;
49
50 /**
51  * utility class used to load Authentication configuration
52  * and to create and register an {@link AuthenticationManager}.
53  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
54  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
55  */

56 public class AuthenticationHelper {
57
58     private static final Logger JavaDoc logger = Logger.getLogger(AuthenticationHelper.class.getName());
59
60
61     /**
62      * create an {@link AuthenticationManager} and register it.
63      * @param authenticationOptions
64      * @param applicationName
65      * @return authenticationManager created
66      * @throws IllegalArgumentException
67      */

68     public static AuthenticationManager initAuthentication(JGuardConfiguration jGuardConf,Map JavaDoc authenticationOptions, String JavaDoc applicationName) throws IllegalArgumentException JavaDoc {
69
70         //grab authentication options
71
boolean debugOnAuthentication = Boolean.valueOf((String JavaDoc)authenticationOptions.get(CoreConstants.DEBUG)).booleanValue();
72
73         if (logger.isLoggable(Level.FINEST)) {
74             logger.log(Level.FINEST,"applicationName="+ applicationName);
75             logger.log(Level.FINEST,"authenticationOptions="+ authenticationOptions);
76             logger.log(Level.FINEST,"debugOnAuthentication="+ debugOnAuthentication);
77         }
78
79         initCryptOptions(authenticationOptions);
80         String JavaDoc scope = (String JavaDoc)authenticationOptions.get(SecurityConstants.SCOPE);
81         if(scope.equalsIgnoreCase(SecurityConstants.JVM_SCOPE)){
82             boolean includeOldConfig = Boolean.valueOf((String JavaDoc)authenticationOptions.get(CoreConstants.INCLUDE_OLD_CONFIG)).booleanValue();
83             boolean includeConfigFromJavaParam = new Boolean JavaDoc((String JavaDoc)authenticationOptions.get(CoreConstants.INCLUDE_CONFIG_FROM_JAVA_PARAM)).booleanValue();
84             ConfigurationHelper.installConfiguration(includeOldConfig, includeConfigFromJavaParam);
85         }
86         //register the authentication part
87
ConfigurationHelper.addConfigurationEntryForWebapp(jGuardConf,applicationName, authenticationOptions, debugOnAuthentication);
88         Map JavaDoc authManagerOptions = (Map JavaDoc)authenticationOptions.get(SecurityConstants.AUTHENTICATION_MANAGER_OPTIONS);
89         authManagerOptions.put(CoreConstants.APPLICATION_NAME, applicationName);
90
91         try {
92             AuthenticationManagerFactory.createAuthenticationManager((String JavaDoc)authenticationOptions.get(SecurityConstants.AUTHENTICATION_MANAGER), authManagerOptions);
93         } catch (AuthenticationException e) {
94             logger.log(Level.SEVERE,"authenticationManager initialization failed");
95         }
96
97         return AuthenticationManagerFactory.getAuthenticationManager();
98     }
99
100
101     private static void initCryptOptions(Map JavaDoc authenticationOptions) {
102         String JavaDoc salt = ((String JavaDoc)authenticationOptions.get(SecurityConstants.SALT));
103         String JavaDoc digestAlgorithm = ((String JavaDoc)authenticationOptions.get(SecurityConstants.DIGEST_ALGORITHM));
104         if(digestAlgorithm!= null && !"".equals(digestAlgorithm)){
105             try {
106                 CryptUtils.setDigestAlgorithm(digestAlgorithm);
107             } catch (NoSuchAlgorithmException JavaDoc e) {
108                 logger.log(Level.SEVERE, e.getMessage());
109                 throw new IllegalArgumentException JavaDoc(e.getMessage());
110             }
111
112             if( salt!=null && !salt.equals("")){
113                 boolean setSaltSuccess = CryptUtils.setSalt(salt.toCharArray());
114                 if(!setSaltSuccess){
115                     logger.log(Level.WARNING, " salt for message digest has not been set ");
116                 }
117             }
118         }
119     }
120
121
122     /**
123      *
124      * @param configurationLocation
125      * @param appHomePath
126      * @return
127      */

128     public static Map JavaDoc loadConfiguration(String JavaDoc configurationLocation, String JavaDoc appHomePath){
129
130         Document doc = XMLUtils.read(configurationLocation);
131
132         //authentication part
133
Element authentication = doc.getRootElement().element(SecurityConstants.AUTHENTICATION);
134         Map JavaDoc authenticationMap = new HashMap JavaDoc();
135         authenticationMap.put(SecurityConstants.SCOPE, authentication.element(SecurityConstants.SCOPE).getTextTrim());
136         authenticationMap.put(CoreConstants.DEBUG, authentication.element(CoreConstants.DEBUG).getTextTrim());
137         authenticationMap.put(CoreConstants.INCLUDE_OLD_CONFIG, authentication.element(CoreConstants.INCLUDE_OLD_CONFIG).getTextTrim());
138         authenticationMap.put(CoreConstants.INCLUDE_CONFIG_FROM_JAVA_PARAM, authentication.element(CoreConstants.INCLUDE_CONFIG_FROM_JAVA_PARAM).getTextTrim());
139         authenticationMap.put(SecurityConstants.INCLUDE_POLICY_FROM_JAVA_PARAM, authentication.element(SecurityConstants.INCLUDE_POLICY_FROM_JAVA_PARAM).getTextTrim());
140         if(authentication.element(SecurityConstants.DIGEST_ALGORITHM)!=null){
141             authenticationMap.put(SecurityConstants.DIGEST_ALGORITHM,authentication.element(SecurityConstants.DIGEST_ALGORITHM).getTextTrim());
142         }
143         if(authentication.element(SecurityConstants.SALT)!=null){
144             authenticationMap.put(SecurityConstants.SALT,authentication.element(SecurityConstants.SALT).getTextTrim());
145         }
146         //loginModules configuration
147
List JavaDoc loginModuleElementsList = authentication.element(CoreConstants.LOGIN_MODULES).elements(SecurityConstants.LOGIN_MODULE);
148         List JavaDoc loginModules = new ArrayList JavaDoc();
149         Iterator JavaDoc itLoginModuleElementsList = loginModuleElementsList.iterator();
150         while(itLoginModuleElementsList.hasNext()){
151             Element loginModule = (Element)itLoginModuleElementsList.next();
152
153             Map JavaDoc loginModuleMap = new HashMap JavaDoc();
154             loginModuleMap.put(CoreConstants.NAME,loginModule.element(CoreConstants.NAME).getTextTrim());
155             loginModuleMap.put(CoreConstants.FLAG,loginModule.element(CoreConstants.FLAG).getTextTrim());
156             Element loginModuleOpts = loginModule.element(CoreConstants.LOGIN_MODULE_OPTIONS);
157             if(loginModuleOpts!=null){
158                 List JavaDoc loginModuleOptsList = loginModuleOpts.elements(SecurityConstants.OPTION);
159                 Iterator JavaDoc itLoginModuleOpts = loginModuleOptsList.iterator();
160                 Map JavaDoc loginModulesOptions = new HashMap JavaDoc();
161                 while (itLoginModuleOpts.hasNext()){
162                     Element option = (Element)itLoginModuleOpts.next();
163                     String JavaDoc name = option.element(CoreConstants.NAME).getTextTrim();
164                     String JavaDoc value = option.element(SecurityConstants.VALUE_MARKUP).getTextTrim();
165                     if(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION.equals(name)||SecurityConstants.AUTHENTICATION_DATABASE_FILE_LOCATION.equals(name)){
166                         value=appHomePath+value;
167                     }
168                     loginModulesOptions.put(name,value);
169                 }
170                 loginModuleMap.put(CoreConstants.LOGIN_MODULE_OPTIONS, loginModulesOptions);
171             }else{
172                 //there are no options for the loginmodule
173
loginModuleMap.put(CoreConstants.LOGIN_MODULE_OPTIONS,new HashMap JavaDoc());
174             }
175             loginModules.add(loginModuleMap);
176         }
177
178         authenticationMap.put(CoreConstants.LOGIN_MODULES, loginModules);
179
180         //authenticationManager configuration
181
authenticationMap.put(SecurityConstants.AUTHENTICATION_MANAGER, authentication.element(SecurityConstants.AUTHENTICATION_MANAGER).getTextTrim());
182
183         Map JavaDoc authenticationManagerOptions = new HashMap JavaDoc();
184         Element authentManagerOptsElement = authentication.element(SecurityConstants.AUTHENTICATION_MANAGER_OPTIONS);
185         List JavaDoc authentManagerOptsList = authentManagerOptsElement.elements(SecurityConstants.OPTION);
186         Iterator JavaDoc authentManagerOpts = authentManagerOptsList.iterator();
187         while (authentManagerOpts.hasNext()){
188             Element option = (Element)authentManagerOpts.next();
189             String JavaDoc name = option.element(CoreConstants.NAME).getTextTrim();
190             String JavaDoc value = option.element(SecurityConstants.VALUE_MARKUP).getTextTrim();
191             if(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION.equals(name)||SecurityConstants.AUTHENTICATION_DATABASE_FILE_LOCATION.equals(name)){
192                 value=appHomePath+value;
193             }
194             authenticationManagerOptions.put(name,value);
195         }
196
197         authenticationMap.put(SecurityConstants.AUTHENTICATION_MANAGER_OPTIONS, authenticationManagerOptions);
198
199         return authenticationMap;
200     }
201 }
202
Popular Tags