KickJava   Java API By Example, From Geeks To Geeks.

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


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

7 package org.jboss.security.auth.spi;
8
9 // $Id: DeploymentRolesLoginModule.java,v 1.1 2004/03/30 21:21:11 tdiesler Exp $
10

11 import org.jboss.metadata.SecurityRoleMetaData;
12 import org.jboss.security.SecurityRolesAssociation;
13 import org.jboss.security.SimpleGroup;
14 import org.jboss.security.SimplePrincipal;
15
16 import javax.security.auth.Subject JavaDoc;
17 import javax.security.auth.callback.CallbackHandler JavaDoc;
18 import javax.security.auth.login.LoginException JavaDoc;
19 import java.security.Principal JavaDoc;
20 import java.security.acl.Group JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * The DeploymentRolesLoginModule adds the roles to the subject that were declared in the
28  * assembly-descriptor element in jboss.xml.
29  *
30  * It gets the roles from the SecurityRolesAssociation, which holds a Map of SecurityRoleMetaData.
31  *
32  * @author Thomas.Diesler@jboss.org
33  * @version $Revision: 1.1 $
34  */

35 public class DeploymentRolesLoginModule extends AbstractServerLoginModule
36 {
37    /**
38     * Initialize the login module.
39     *
40     * @param subject the Subject to update after a successful login.
41     * @param callbackHandler the CallbackHandler that will be used to obtain the
42     * the user identity and credentials.
43     * @param sharedState a Map shared between all configured login module instances
44     * @param options the parameters passed to the login module.
45     */

46    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options)
47    {
48       super.initialize(subject, callbackHandler, sharedState, options);
49
50       // Relies on another LoginModule having done the authentication
51
useFirstPass = true;
52    }
53
54    /**
55     * Overriden by subclasses to return the Principal that corresponds to
56     * the user primary identity.
57     */

58    protected Principal getIdentity()
59    {
60       // Setup our view of the user
61
Object JavaDoc username = sharedState.get("javax.security.auth.login.name");
62       if(username == null)
63          throw new IllegalStateException JavaDoc("Expected to find the username in the shared state");
64
65       if (username instanceof Principal)
66          return (Principal)username;
67
68       return new SimplePrincipal((String JavaDoc)username);
69    }
70
71    /**
72     * Create the 'Roles' group and populate it with the
73     * principals security roles from the SecurityRolesAssociation
74     * @return Group[] containing the sets of roles
75     */

76    protected Group[] getRoleSets() throws LoginException JavaDoc
77    {
78       Group group = new SimpleGroup("Roles");
79       Iterator JavaDoc itRoleNames = getSecurityRoleNames().iterator();
80       while (itRoleNames.hasNext())
81       {
82          String JavaDoc roleName = (String JavaDoc) itRoleNames.next();
83          group.addMember(new SimplePrincipal(roleName));
84       }
85
86       return new Group[]{group};
87    }
88
89    /**
90     * Get the securtiy role names for the current principal from the
91     * SecurityRolesAssociation.
92     */

93    private Set JavaDoc getSecurityRoleNames()
94    {
95       HashSet JavaDoc roleNames = new HashSet JavaDoc();
96       String JavaDoc userName = getIdentity().getName();
97
98       Map JavaDoc securityRoles = SecurityRolesAssociation.getSecurityRoles();
99       if (securityRoles != null)
100       {
101          Iterator JavaDoc it = securityRoles.values().iterator();
102          while (it.hasNext())
103          {
104             SecurityRoleMetaData srMetaData = (SecurityRoleMetaData) it.next();
105             if (srMetaData.getPrincipals().contains(userName))
106                roleNames.add(srMetaData.getRoleName());
107          }
108       }
109       return roleNames;
110    }
111 }
112
Popular Tags