KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > spi > IdentityLoginModule


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.security.auth.spi;
9
10 import java.security.Principal JavaDoc;
11 import java.security.acl.Group JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14 import javax.security.auth.Subject JavaDoc;
15 import javax.security.auth.callback.CallbackHandler JavaDoc;
16 import javax.security.auth.login.LoginException JavaDoc;
17
18 import org.jboss.security.SimpleGroup;
19 import org.jboss.security.SimplePrincipal;
20 import org.jboss.security.auth.spi.AbstractServerLoginModule;
21
22 /**
23  * A simple login module that simply associates the principal specified
24  * in the module options with any subject authenticated against the module.
25  * The type of Principal class used is
26  * <code>org.jboss.security.SimplePrincipal.</code>
27  * <p>
28  * If no principal option is specified a principal with the name of 'guest'
29  * is used.
30  *
31  * @see org.jboss.security.SimpleGroup
32  * @see org.jboss.security.SimplePrincipal
33  *
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 1.7 $
36  */

37 public class IdentityLoginModule extends AbstractServerLoginModule
38 {
39    private String JavaDoc principalName;
40    private String JavaDoc roleNames;
41
42    public IdentityLoginModule()
43    {
44    }
45
46    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc handler, Map JavaDoc sharedState, Map JavaDoc options)
47    {
48       super.initialize(subject, handler, sharedState, options);
49       principalName = (String JavaDoc) options.get("principal");
50       if( principalName == null )
51          principalName = "guest";
52       roleNames = (String JavaDoc) options.get("roles");
53    }
54
55    public boolean login() throws LoginException JavaDoc
56    {
57       if( super.login() == true )
58          return true;
59
60       Principal JavaDoc principal = new SimplePrincipal(principalName);
61       subject.getPrincipals().add(principal);
62       // Put the principal name into the sharedState map
63
sharedState.put("javax.security.auth.login.name", principalName);
64       super.loginOk = true;
65       return true;
66    }
67
68    protected Principal JavaDoc getIdentity()
69    {
70       Principal JavaDoc principal = new SimplePrincipal(principalName);
71       return principal;
72    }
73
74    protected Group JavaDoc[] getRoleSets() throws LoginException JavaDoc
75    {
76       SimpleGroup roles = new SimpleGroup("Roles");
77       Group JavaDoc[] roleSets = {roles};
78       if( roleNames != null )
79       {
80          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(roleNames, ",");
81          while( tokenizer.hasMoreTokens() )
82          {
83             String JavaDoc roleName = tokenizer.nextToken();
84             roles.addMember(new SimplePrincipal(roleName));
85          }
86       }
87       return roleSets;
88    }
89 }
90
Popular Tags