KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > authenticate > JaasAuthenticator


1 /*
2  * Created on Jul 20, 2006
3  */

4 package com.openedit.users.authenticate;
5
6 import java.io.IOException JavaDoc;
7
8 import javax.security.auth.callback.Callback JavaDoc;
9 import javax.security.auth.callback.CallbackHandler JavaDoc;
10 import javax.security.auth.callback.NameCallback JavaDoc;
11 import javax.security.auth.callback.PasswordCallback JavaDoc;
12 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
13 import javax.security.auth.login.LoginContext JavaDoc;
14 import javax.security.auth.login.LoginException JavaDoc;
15 import javax.security.sasl.RealmCallback JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import com.openedit.users.Authenticator;
21 import com.openedit.users.User;
22 import com.openedit.users.UserManagerException;
23
24 public class JaasAuthenticator implements Authenticator
25 {
26     private static final Log log = LogFactory.getLog(JaasAuthenticator.class);
27
28     public boolean authenticate(User inUser, String JavaDoc inPassword) throws UserManagerException
29     {
30         // Obtain a LoginContext, needed for authentication. Tell it
31
// to use the LoginModule implementation specified by the
32
// entry named "JaasSample" in the JAAS login configuration
33
// file and to also use the specified CallbackHandler.
34
try
35         {
36             LoginContext JavaDoc loginContext = new LoginContext JavaDoc("OpenEditConfig", new UserPasswordCallbackHandler(inUser, inPassword));
37             loginContext.login();
38
39             // Now we're logged in, so we can get the current subject.
40
//Subject subject = loginContext.getSubject();
41
}
42         catch (LoginException JavaDoc le)
43         {
44             log.error("Cannot create LoginContext. " + le.getMessage());
45             return false;
46         }
47         catch (SecurityException JavaDoc se)
48         {
49             log.error("Cannot create LoginContext. " + se.getMessage());
50             return false;
51         }
52         return true;
53     }
54
55     public class UserPasswordCallbackHandler implements CallbackHandler JavaDoc
56     {
57         private User fieldUser;
58         private String JavaDoc mPassword;
59
60         /**
61          * We need a stateful handler to return the username and password.
62          */

63         public UserPasswordCallbackHandler(User username, String JavaDoc password)
64         {
65             fieldUser = username;
66             mPassword = password;
67         }
68
69         public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
70         {
71             for (int ii = 0; ii < callbacks.length; ii++)
72             {
73                 if (callbacks[ii] instanceof NameCallback JavaDoc)
74                 {
75                     NameCallback JavaDoc ncb = (NameCallback JavaDoc) callbacks[ii];
76                     ncb.setName(fieldUser.getUserName());
77                 }
78                 else if (callbacks[ii] instanceof PasswordCallback JavaDoc)
79                 {
80                     PasswordCallback JavaDoc pcb = (PasswordCallback JavaDoc) callbacks[ii];
81                     pcb.setPassword(mPassword.toCharArray());
82                 }
83                 else if (callbacks[ii] instanceof RealmCallback JavaDoc)
84                 {
85                     RealmCallback JavaDoc rcb = (RealmCallback JavaDoc) callbacks[ii];
86                     String JavaDoc realm = fieldUser.getString("realm");
87                     if( realm != null)
88                     {
89                         rcb.setText(realm);
90                     }
91                 }
92             }
93         }
94
95     }
96
97 }
98
Popular Tags