KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > DefaultAuthenticationScheme


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.sslexplorer.policyframework.AbstractResource;
36 import com.sslexplorer.policyframework.PolicyConstants;
37
38 /**
39  * This is the default implementation of an <i>Authentication Scheme</i> that
40  * loads the scheme from the <i>System Database</i> as if it were a {@link com.sslexplorer.policyframework.Resource}.
41  *
42  * @author Brett Smith <brett@3sp.com>
43  */

44 public class DefaultAuthenticationScheme extends AbstractResource implements AuthenticationScheme, Serializable JavaDoc {
45     
46     final static Log log = LogFactory.getLog(DefaultAuthenticationScheme.class);
47     
48     // Private instance variables
49

50     private List JavaDoc<String JavaDoc> modules;
51     private HttpSession JavaDoc servletSession;
52     private int current;
53     private List JavaDoc<AuthenticationModule> authenticationModules;
54     private User user;
55     private List JavaDoc<Credentials> allCredentials;
56     private AccountLock lock;
57     private boolean enabled;
58     private int priority;
59
60     /**
61      * Constructor
62      * @param realmID
63      * @param resourceId
64      * @param resourceName
65      * @param resourceDescription
66      * @param dateAmended
67      * @param dateCreated
68      * @param enabled enabled
69      * @param priority
70      */

71     public DefaultAuthenticationScheme(int realmID, int resourceId, String JavaDoc resourceName, String JavaDoc resourceDescription, Calendar JavaDoc dateAmended, Calendar JavaDoc dateCreated, boolean enabled, int priority) {
72         super(realmID, PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, resourceId, resourceName, resourceDescription, dateAmended, dateCreated);
73         current = -1;
74         modules = new ArrayList JavaDoc<String JavaDoc>();
75         allCredentials = new ArrayList JavaDoc<Credentials>();
76         this.enabled = enabled;
77         this.priority = priority;
78     }
79     
80     /* (non-Javadoc)
81      * @see com.sslexplorer.security.AuthenticationScheme#addModule(java.lang.String)
82      */

83     public void addModule(String JavaDoc module) {
84         if(!modules.contains(module)) {
85             modules.add(module);
86         }
87     }
88     
89     /* (non-Javadoc)
90      * @see com.sslexplorer.security.AuthenticationScheme#hasModule(java.lang.String)
91      */

92     public boolean hasModule(String JavaDoc name) {
93         return modules.contains(name);
94     }
95     
96     /* (non-Javadoc)
97      * @see com.sslexplorer.security.AuthenticationScheme#removeModule(java.lang.String)
98      */

99     public void removeModule(String JavaDoc module) {
100         modules.remove(module);
101     }
102     
103     /* (non-Javadoc)
104      * @see com.sslexplorer.security.AuthenticationScheme#modules()
105      */

106     public Iterator JavaDoc<String JavaDoc> modules() {
107         return modules.iterator();
108     }
109
110     /* (non-Javadoc)
111      * @see com.sslexplorer.security.AuthenticationScheme#init(javax.servlet.http.HttpSession)
112      */

113     public void init(HttpSession JavaDoc servletSession) throws Exception JavaDoc {
114         this.servletSession = servletSession;
115         authenticationModules = new ArrayList JavaDoc<AuthenticationModule>();
116         for(Iterator JavaDoc<String JavaDoc> i = modules.iterator(); i.hasNext(); ) {
117             String JavaDoc moduleId = i.next();
118             AuthenticationModule module = AuthenticationModuleManager.getInstance().createModule(moduleId);
119             authenticationModules.add(module);
120             module.init(this);
121         }
122     }
123
124     /* (non-Javadoc)
125      * @see com.sslexplorer.security.AuthenticationScheme#getCurrentModuleIndex()
126      */

127     public int getCurrentModuleIndex() {
128         return current;
129     }
130
131     /* (non-Javadoc)
132      * @see com.sslexplorer.security.AuthenticationScheme#getUser()
133      */

134     public User getUser() {
135         return user;
136     }
137
138     /* (non-Javadoc)
139      * @see com.sslexplorer.security.AuthenticationScheme#setUser(com.sslexplorer.security.User)
140      */

141     public void setUser(User user) {
142         this.user = user;
143     }
144
145     /* (non-Javadoc)
146      * @see com.sslexplorer.security.AuthenticationScheme#getServletSession()
147      */

148     public HttpSession JavaDoc getServletSession() {
149         return servletSession;
150     }
151
152     /* (non-Javadoc)
153      * @see com.sslexplorer.security.AuthenticationScheme#nextAuthenticationModule()
154      */

155     public AuthenticationModule nextAuthenticationModule() {
156         if( ( current + 1 ) < authenticationModules.size()) {
157             AuthenticationModule mod = (AuthenticationModule)authenticationModules.get(++current);
158             return mod;
159         }
160         return null;
161     }
162
163     /* (non-Javadoc)
164      * @see com.sslexplorer.security.AuthenticationScheme#currentAuthenticationModule()
165      */

166     public AuthenticationModule currentAuthenticationModule() {
167         if(current != -1) {
168             AuthenticationModule mod = (AuthenticationModule)authenticationModules.get(current);
169             return mod;
170         }
171         return null;
172     }
173
174     /* (non-Javadoc)
175      * @see com.sslexplorer.security.AuthenticationScheme#authenticationComplete(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
176      */

177     public void authenticationComplete(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
178         for(Iterator JavaDoc i = authenticationModules.iterator();i.hasNext(); ) {
179             AuthenticationModule mod = (AuthenticationModule)i.next();
180             if (log.isDebugEnabled())
181                 log.debug("Informing module " + mod.getName() + " that authentication is complete");
182             mod.authenticationComplete();
183             
184             // Only inform the first module when the session is locked
185
if(request.getSession().getAttribute(Constants.SESSION_LOCKED) != null) {
186                 break;
187             }
188         }
189         LogonControllerFactory.getInstance().logon(request, response, this);
190         
191     }
192
193     /* (non-Javadoc)
194      * @see com.sslexplorer.security.AuthenticationScheme#getUsername()
195      */

196     public String JavaDoc getUsername() {
197         return user.getPrincipalName();
198     }
199
200
201     /* (non-Javadoc)
202      * @see com.sslexplorer.security.AuthenticationScheme#addCredentials(com.sslexplorer.security.Credentials)
203      */

204     public void addCredentials(Credentials credentials) {
205         allCredentials.add(credentials);
206     }
207
208     /* (non-Javadoc)
209      * @see com.sslexplorer.security.AuthenticationScheme#credentials()
210      */

211     public Iterator JavaDoc credentials() {
212         return allCredentials.iterator();
213     }
214
215     /* (non-Javadoc)
216      * @see com.sslexplorer.security.AuthenticationScheme#setAccountLock(com.sslexplorer.security.AccountLock)
217      */

218     public void setAccountLock(AccountLock lock) {
219         this.lock = lock;
220     }
221
222     /* (non-Javadoc)
223      * @see com.sslexplorer.security.AuthenticationScheme#getAccountLock()
224      */

225     public AccountLock getAccountLock() {
226         return lock;
227     }
228
229     /* (non-Javadoc)
230      * @see com.sslexplorer.security.AuthenticationScheme#moveUp(java.lang.String)
231      */

232     public void moveUp(String JavaDoc module) {
233         int idx = modules.indexOf(module);
234         if(idx > 0) {
235             String JavaDoc swap = modules.get(idx - 1);
236             modules.remove(idx - 1);
237             modules.add(idx, swap);
238         }
239     }
240
241     /* (non-Javadoc)
242      * @see com.sslexplorer.security.AuthenticationScheme#clearModules()
243      */

244     public void clearModules() {
245         modules.clear();
246         
247     }
248
249     /* (non-Javadoc)
250      * @see com.sslexplorer.security.AuthenticationScheme#moveDown(java.lang.String)
251      */

252     public void moveDown(String JavaDoc module) {
253         int idx = modules.indexOf(module);
254         if( ( idx + 1 ) < modules.size() ) {
255             String JavaDoc swap = modules.get(idx + 1);
256             modules.remove(idx + 1);
257             modules.add(idx, swap);
258         }
259     }
260
261     /* (non-Javadoc)
262      * @see com.sslexplorer.security.AuthenticationScheme#getModuleCount()
263      */

264     public int getModuleCount() {
265         return modules.size();
266     }
267     
268     /* (non-Javadoc)
269      * @see com.sslexplorer.security.AuthenticationScheme#getModule(int)
270      */

271     public String JavaDoc getModule(int index) {
272         return modules.get(index);
273     }
274     
275     /* (non-Javadoc)
276      * @see com.sslexplorer.security.AuthenticationScheme#getSessionLocked()
277      */

278     public boolean getSessionLocked() {
279         return getServletSession() != null ? ( getServletSession().getAttribute(Constants.SESSION_LOCKED) != null ) : false;
280     }
281
282     /* (non-Javadoc)
283      * @see com.sslexplorer.security.AuthenticationScheme#getSchemeName()
284      */

285     public String JavaDoc getSchemeName() {
286         return getResourceName();
287     }
288
289     /* (non-Javadoc)
290      * @see com.sslexplorer.security.AuthenticationScheme#getEnabled()
291      */

292     public boolean getEnabled() {
293         return enabled;
294     }
295     
296     /* (non-Javadoc)
297      * @see com.sslexplorer.security.AuthenticationScheme#setEnabled(boolean)
298      */

299     public void setEnabled(boolean enabled) {
300         this.enabled = enabled;
301     }
302
303     /* (non-Javadoc)
304      * @see com.sslexplorer.security.AuthenticationScheme#isSystemScheme()
305      */

306     public boolean isSystemScheme() {
307         if(modules.size() == 0) {
308             return false;
309         }
310         for(Iterator JavaDoc i = modules(); i.hasNext(); ) {
311             String JavaDoc mod = (String JavaDoc)i.next();
312             if(!AuthenticationModuleManager.getInstance().getModuleDefinition(mod).getSystem()) {
313                 return false;
314             }
315         }
316         return true;
317     }
318
319     /* (non-Javadoc)
320      * @see com.sslexplorer.security.AuthenticationScheme#getModules()
321      */

322     public String JavaDoc[] getModules(){
323         return modules.toArray(new String JavaDoc[modules.size()]);
324     }
325     
326     /* (non-Javadoc)
327      * @see com.sslexplorer.security.AuthenticationScheme#getPriority()
328      */

329     public String JavaDoc getPriority() {
330         return String.valueOf(priority);
331     }
332
333     /* (non-Javadoc)
334      * @see com.sslexplorer.security.AuthenticationScheme#getPriorityInt()
335      */

336     public int getPriorityInt(){
337         return priority;
338     }
339
340     /* (non-Javadoc)
341      * @see com.sslexplorer.security.AuthenticationScheme#setPriorityInt(int)
342      */

343     public void setPriorityInt(int priority){
344         this.priority = priority;
345     }
346 }
347
Popular Tags