KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.Locale JavaDoc;
33
34 import javax.security.auth.Subject JavaDoc;
35 import javax.security.auth.callback.CallbackHandler JavaDoc;
36 import javax.security.auth.login.Configuration JavaDoc;
37 import javax.security.auth.login.LoginContext JavaDoc;
38 import javax.security.auth.login.LoginException JavaDoc;
39
40 import net.sf.jguard.core.authentication.configuration.LocalLoginContext;
41 import net.sf.jguard.core.principals.UserPrincipal;
42 import net.sf.jguard.ext.util.ThrowableUtils;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47
48 /**
49  * Authentication utility class.
50  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
51  */

52 public class AuthenticationUtils {
53
54     private LoginContext JavaDoc loginContext = null;
55     private LocalLoginContext localLoginContext = null;
56     private Subject JavaDoc subject = null;
57     private boolean loggedOut = false;
58     private boolean local = false;
59     private static final Log logger = LogFactory.getLog(AuthenticationUtils.class);
60     private Configuration JavaDoc configuration = null;
61     
62     public AuthenticationUtils(){
63         super();
64     }
65     
66     
67     public AuthenticationUtils(Configuration JavaDoc config){
68         super();
69         configuration = config;
70         local= true;
71     }
72     
73     /**
74      * authenticate user against the application's configuration.
75      * @param applicationName
76      * @param cbh
77      * @throws LoginException raised if authentication failed
78      */

79     public void login( String JavaDoc applicationName, CallbackHandler JavaDoc cbh,Locale JavaDoc locale) throws LoginException JavaDoc {
80
81         try{
82             if(local){
83             localLoginContext = new LocalLoginContext(applicationName,cbh,configuration);
84             }else{
85             loginContext = new LoginContext JavaDoc(applicationName,cbh);
86             }
87         } catch(LoginException JavaDoc se) {
88               logger.error("LoginContext cannot be created. "+ se.getMessage(),se);
89               throw se;
90         }
91
92         try{
93             if(local){
94                 localLoginContext.login();
95             }else{
96                 loginContext.login();
97             }
98
99         }catch(LoginException JavaDoc le){
100             Throwable JavaDoc localizedThrowable = ThrowableUtils.localizeThrowable(le,locale);
101             throw (LoginException JavaDoc)localizedThrowable;
102         }
103         
104         if(local){
105             subject = localLoginContext.getSubject();
106         }else{
107             subject = loginContext.getSubject();
108         }
109         if(subject != null){
110             // used in ABAC permissions
111
UserPrincipal userPrincipal = new UserPrincipal(subject);
112             subject.getPrincipals().add(userPrincipal);
113         }
114     }
115
116
117     
118     
119      /**
120      * retrieve the subject from the loginContext.
121      * @return authenticated Subject, otherwise <strong>null</strong>.
122      */

123     public Subject JavaDoc getSubject(){
124         return subject;
125     }
126     
127     /**
128      * logout the user with the related LoginContext.
129      */

130     public void logout() {
131       if(loggedOut==false){
132          try {
133             if(local){
134                     if(localLoginContext!=null){
135                         localLoginContext.logout();
136                         loggedOut = true;
137                     }else{
138                         logger.debug(" user is not logged, so we don't logout him ");
139                     }
140             }else{
141                     if(loginContext!=null){
142                         loginContext.logout();
143                         loggedOut = true;
144                     }else{
145                         logger.debug(" user is not logged, so we don't logout him ");
146                     }
147             }
148          } catch (LoginException JavaDoc e) {
149             logger.error(" error raised when the user logout ", e);
150          }
151       }
152     }
153
154     public boolean isLocal() {
155         return local;
156     }
157     
158     public static String JavaDoc exportAsXMLString(AuthenticationManager authenticationManager){
159         XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager);
160         return xmlAuthenticationManager.exportAsXMLString();
161     }
162     
163     /**
164      * return a <strong>new</strong> XmlAuthenticationManager although if the parameter is already an XmlAuthenticationManager.
165      * @param authenticationManager
166      * @return
167      */

168     public static XmlAuthenticationManager exportAsXmlAuthenticationManager(AuthenticationManager authenticationManager) {
169         XmlAuthenticationManager xmlAuthenticationManager = null;
170         xmlAuthenticationManager = new XmlAuthenticationManager();
171         xmlAuthenticationManager.importAuthenticationManager(authenticationManager);
172         
173         return xmlAuthenticationManager;
174     }
175     
176     public static void writeAsHTML(AuthenticationManager authenticationManager,OutputStream JavaDoc outputStream) throws IOException JavaDoc{
177             XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager);
178             xmlAuthenticationManager.writeAsHTML(outputStream);
179     }
180         
181     public static void writeAsXML(AuthenticationManager authenticationManager,OutputStream JavaDoc outputStream,String JavaDoc encodingScheme) throws IOException JavaDoc{
182             XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager);
183             xmlAuthenticationManager.writeAsXML(outputStream, encodingScheme);
184     }
185         
186     public static void exportAsXMLFile(AuthenticationManager authenticationManager,String JavaDoc fileName) throws IOException JavaDoc{
187             XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager);
188             xmlAuthenticationManager.exportAsXMLFile(fileName);
189     }
190 }
191
Popular Tags