KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.portal.common.util.Tools;
12 import org.jboss.portal.common.util.UUIDGenerator;
13 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
14 import org.jboss.security.SimpleGroup;
15
16 import java.util.Collections JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.WeakHashMap JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.security.acl.Group JavaDoc;
21
22 import javax.security.auth.Subject JavaDoc;
23 import javax.security.auth.callback.Callback JavaDoc;
24 import javax.security.auth.callback.CallbackHandler JavaDoc;
25 import javax.security.auth.callback.PasswordCallback JavaDoc;
26 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
27 import javax.security.auth.login.LoginException JavaDoc;
28 import javax.security.auth.login.FailedLoginException JavaDoc;
29 import javax.security.auth.spi.LoginModule JavaDoc;
30
31 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
32
33 /**
34  *
35  * This login module provides identity propagation in the same virtual machine.
36  * The way to use it is to call the static method propagate that will give a password
37  * back. That password then can be used to authenticate the user against this module.
38  *
39  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
40  * @version $Revision: 1.3 $
41  */

42 public class IdentityPropagationLoginModule extends UsernamePasswordLoginModule
43 {
44
45    /** Where we keep the user name and password. */
46    private static final Map JavaDoc knownIdentities = new ConcurrentHashMap();
47
48    /** The password generator. */
49    private static final UUIDGenerator generator = new UUIDGenerator();
50
51    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options)
52    {
53       super.initialize(subject, callbackHandler, sharedState, options);
54    }
55    
56    protected String JavaDoc getUsersPassword() throws LoginException JavaDoc
57    {
58       String JavaDoc userName = getUsername();
59       String JavaDoc password = (String JavaDoc)knownIdentities.get(userName);
60       if (password == null)
61       {
62          throw new FailedLoginException JavaDoc("No matching username found in Principals");
63       }
64       return password;
65    }
66
67    protected Group[] getRoleSets() throws LoginException JavaDoc
68    {
69       try
70       {
71          Group rolesGroup = new SimpleGroup("Roles");
72
73          // Hardcoded, could be specified as parameter in the propagate method.
74
rolesGroup.addMember(createIdentity("root"));
75          return new Group[]{rolesGroup};
76       }
77       catch (Exception JavaDoc e)
78       {
79          throw new LoginException JavaDoc(e.toString());
80       }
81    }
82
83    /**
84     * Store the username and give it a temporary password.
85     * The userName/password is valid only during the callback
86     * of the specified runnable argument.
87     */

88    public static void propagate(String JavaDoc userName, Runnable JavaDoc runnable)
89    {
90       if (userName == null)
91       {
92          throw new IllegalArgumentException JavaDoc("Null user name not accepted");
93       }
94       if (runnable == null)
95       {
96          throw new IllegalArgumentException JavaDoc("Runnable is null");
97       }
98       final String JavaDoc password = Tools.md5AsHexString(generator.generateKey());
99       try
100       {
101          knownIdentities.put(userName, password);
102          runnable.run(userName, password);
103       }
104       finally
105       {
106          knownIdentities.remove(userName);
107       }
108    }
109
110    public interface Runnable
111    {
112       void run(String JavaDoc userName, String JavaDoc password);
113    }
114 }
115
Popular Tags