KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > security > jaas > CacheLoginModule


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.security.jaas;
10
11 import java.util.Collections JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.WeakHashMap JavaDoc;
14 import java.io.Serializable JavaDoc;
15
16 import javax.security.auth.Subject JavaDoc;
17 import javax.security.auth.callback.Callback JavaDoc;
18 import javax.security.auth.callback.CallbackHandler JavaDoc;
19 import javax.security.auth.callback.PasswordCallback JavaDoc;
20 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
21 import javax.security.auth.login.LoginException JavaDoc;
22 import javax.security.auth.spi.LoginModule JavaDoc;
23
24 /**
25  * This login module takes the incoming hash and fetch login/password from its cache and put it
26  * in the shared map. All the other module after this one must be configured to use userFirstPass
27  * in the password-stacking option.
28  *
29  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
30  * @version $Revision: 1.2 $
31  */

32 public class CacheLoginModule implements LoginModule JavaDoc
33 {
34
35    /** Where we keep the user name and password. */
36    private static final Map JavaDoc cache = Collections.synchronizedMap(new WeakHashMap JavaDoc());
37    
38    /**
39     * Cache a user name and password under the specified key.
40     *
41     * This handle is used as the real key in the cache. That means that
42     * it will stay here until there is a weak reference on that handle or
43     * the login() method is called on this login module.
44     *
45     * It is used by the LoginServlet and put in the HTTP session, so it will
46     * stay in the cache at least until this session expires.
47     */

48    public static Handle cache(String JavaDoc key, String JavaDoc userName, String JavaDoc password)
49    {
50       Handle handle = new Handle(key);
51       cache.put(handle, new UserNamePassword(userName, password));
52       return handle;
53    }
54
55    private Subject JavaDoc subject;
56    private CallbackHandler JavaDoc callbackHandler;
57    private Map JavaDoc sharedState;
58    private Map JavaDoc options;
59    
60    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options)
61    {
62       this.subject = subject;
63       this.callbackHandler = callbackHandler;
64       this.sharedState = sharedState;
65       this.options = options;
66    }
67    
68    public boolean login() throws LoginException JavaDoc
69    {
70       String JavaDoc key = null;
71       try
72       {
73          PasswordCallback JavaDoc pc = new PasswordCallback JavaDoc("Password: ", false);;
74          Callback JavaDoc[] callbacks = {pc};
75          callbackHandler.handle(callbacks);
76          char[] tmp = pc.getPassword();
77          if( tmp != null )
78          {
79             char[] keyAsChars = new char[tmp.length];
80             System.arraycopy(tmp, 0, keyAsChars, 0, tmp.length);
81             pc.clearPassword();
82             key = new String JavaDoc(keyAsChars);
83          }
84       }
85       catch(java.io.IOException JavaDoc ioe)
86       {
87          throw new LoginException JavaDoc(ioe.toString());
88       }
89       catch(UnsupportedCallbackException JavaDoc uce)
90       {
91          throw new LoginException JavaDoc("CallbackHandler does not support: " + uce.getCallback());
92       }
93   
94       //
95
UserNamePassword up = null;
96       if (key != null)
97       {
98          Handle handle = new Handle(key);
99          up = (UserNamePassword)cache.remove(handle);
100          if (up != null)
101          {
102             sharedState.put("javax.security.auth.login.name", up.userName);
103             sharedState.put("javax.security.auth.login.password", up.password);
104          }
105          else
106          {
107             //
108
}
109       }
110       else
111       {
112          //
113
}
114       return true;
115    }
116
117    public boolean commit() throws LoginException JavaDoc
118    {
119       return true;
120    }
121
122    public boolean abort() throws LoginException JavaDoc
123    {
124       return true;
125    }
126
127    public boolean logout() throws LoginException JavaDoc
128    {
129       return true;
130    }
131
132    /**
133     * User name and password object.
134     *
135     * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
136     * @version $Revision: 1.2 $
137     */

138    private static class UserNamePassword
139    {
140       private final String JavaDoc userName;
141       private final String JavaDoc password;
142       private UserNamePassword(String JavaDoc userName, String JavaDoc password)
143       {
144          if (userName == null)
145          {
146             throw new IllegalArgumentException JavaDoc("No null user name");
147          }
148          if (password == null)
149          {
150             throw new IllegalArgumentException JavaDoc("No null password");
151          }
152          this.userName = userName;
153          this.password = password;
154       }
155    }
156    
157    /**
158     * Handle returned to the client.
159     */

160    public static class Handle implements Serializable JavaDoc
161    {
162       private final String JavaDoc key;
163       private Handle(String JavaDoc key)
164       {
165          if (key == null)
166          {
167             throw new IllegalArgumentException JavaDoc();
168          }
169          this.key = key;
170       }
171       public int hashCode()
172       {
173          return key.hashCode();
174       }
175       public boolean equals(Object JavaDoc obj)
176       {
177          if (obj == this)
178          {
179             return true;
180          }
181          if (obj instanceof Handle)
182          {
183             Handle other = (Handle)obj;
184             return other.key.equals(key);
185          }
186          return false;
187       }
188    }
189 }
190
Popular Tags