KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > security > jaas > SimpleLoginModule


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

18
19
20 package sync4j.framework.security.jaas;
21
22 import java.util.logging.*;
23 import java.util.Map JavaDoc;
24 import java.security.Principal JavaDoc;
25
26 import javax.security.auth.*;
27 import javax.security.auth.spi.*;
28 import javax.security.auth.login.*;
29 import javax.security.auth.callback.*;
30
31 import sync4j.framework.security.Sync4jPrincipal;
32 import sync4j.framework.logging.Sync4jLogger;
33
34 /**
35  * This is a simple implementation of a JAAS login module. It always authenticates
36  * and uthorizes.
37  *
38  * @author Stefano Fornari @ Funambol.com
39  * @version $Id: SimpleLoginModule.java,v 1.6 2005/03/02 20:57:38 harrie Exp $
40  */

41 public class SimpleLoginModule implements LoginModule {
42     
43     // ------------------------------------------------------------ Private data
44

45     private Subject subject = null ;
46     private CallbackHandler handler = null ;
47     private boolean loginSucceded = false;
48     private boolean loginCommitted = false;
49     private Principal JavaDoc principal = null ;
50     private String JavaDoc username = null ;
51     private String JavaDoc password = null ;
52     
53     // ---------------------------------------------------------- Protected data
54

55     protected final Logger log = Sync4jLogger.getLogger();
56     
57     // ---------------------------------------------------------- Public methods
58

59     public void initialize(Subject subject ,
60                            CallbackHandler handler ,
61                            Map JavaDoc sharedState ,
62                            Map JavaDoc options ) {
63                                
64         this.subject = subject;
65         this.handler = handler;
66         this.loginSucceded = this.loginCommitted = false;
67         this.username = null;
68         this.password = null;
69         
70         if (log.isLoggable(Level.FINEST)) {
71             log.finest("sharedState: " + sharedState);
72             log.finest("options: " + options);
73         }
74     }
75     
76     public boolean login() throws LoginException {
77         if (handler == null) {
78             throw new LoginException("No CallbackHandler defined");
79         }
80         
81         //
82
// Creates two callbacks: one for user, one for password. They are for
83
// demonstration purpose only, because they will never be used by this
84
// implementation of <i>LoginModule</i>
85
//
86
Callback[] callbacks = new Callback[2];
87         
88         NameCallback nameCallback = new NameCallback("Username");
89         PasswordCallback passwordCallback = new PasswordCallback("Password", false);
90         
91         callbacks[0] = nameCallback;
92         callbacks[1] = passwordCallback;
93         
94         try {
95             handler.handle(callbacks);
96             
97             username = nameCallback.getName();
98             password = (passwordCallback.getPassword() == null) ? null
99                                                                 : new String JavaDoc(passwordCallback.getPassword());
100             
101             passwordCallback.clearPassword();
102         } catch (Exception JavaDoc e) {
103             throw new LoginException(e.toString());
104         }
105         
106         loginSucceded = true;
107         return true;
108     }
109     
110     public boolean commit() throws LoginException {
111         if (loginSucceded == false) {
112             return false;
113         }
114         
115         //
116
// Login succeded, so create a Principal and add it to the subject
117
//
118
principal = new Sync4jPrincipal(null, username, null);
119         
120         if (!(subject.getPrincipals().contains(principal))) {
121             subject.getPrincipals().add(principal);
122         }
123         
124         username = null;
125         password = null;
126         loginCommitted = true;
127         
128         return true;
129     }
130     
131     public boolean abort() throws LoginException {
132         if (loginSucceded == false) {
133             return false;
134         } else if (loginCommitted == false) {
135             loginSucceded = false;
136             username = password = null;
137             principal = null;
138         } else {
139             logout();
140         }
141         
142         return true;
143     }
144     
145     public boolean logout() throws LoginException {
146         subject.getPrincipals().remove(principal);
147         
148         loginSucceded = loginCommitted = false;
149         username = password = null;
150         principal = null;
151         
152         return true;
153     }
154     
155 }
Popular Tags