KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > auth > RoleMappingLoginModule


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test.auth;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.security.acl.Group JavaDoc;
32 import java.security.Principal JavaDoc;
33 import javax.security.auth.spi.LoginModule JavaDoc;
34 import javax.security.auth.Subject JavaDoc;
35 import javax.security.auth.login.LoginException JavaDoc;
36 import javax.security.auth.callback.CallbackHandler JavaDoc;
37
38 import org.jboss.security.SimplePrincipal;
39
40 /**
41  A role mapping login module.
42
43  @author Scott.Stark@jboss.org
44  @version $Revision: 38581 $
45  */

46 public class RoleMappingLoginModule implements LoginModule JavaDoc
47 {
48    /** The sec domain to app domaon role mappings */
49    private HashMap JavaDoc roleMappings = new HashMap JavaDoc();
50    /** The mapped roles added to the subject */
51    HashSet JavaDoc addedRoles = new HashSet JavaDoc();
52    private Subject JavaDoc theSubject;
53
54    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler,
55       Map JavaDoc sharedState, Map JavaDoc options)
56    {
57       this.theSubject = subject;
58
59       int count = 1;
60       String JavaDoc key = "role.";
61       String JavaDoc mapping = (String JavaDoc) options.get(key+count);
62       while( mapping != null )
63       {
64          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(mapping, "=,");
65          String JavaDoc appRole = tokenizer.nextToken();
66          while( tokenizer.hasMoreTokens() )
67          {
68             String JavaDoc secDomainRole = tokenizer.nextToken();
69             roleMappings.put(secDomainRole, appRole);
70          }
71          count ++;
72          mapping = (String JavaDoc) options.get(key+count);
73       }
74    }
75
76    /**
77     there is nothing to do here
78     @return true
79     */

80    public boolean login()
81    {
82       return true;
83    }
84
85    /**
86     Add the mapped roles
87     @return true
88     @throws LoginException
89     */

90    public boolean commit() throws LoginException JavaDoc
91    {
92       Set JavaDoc groups = theSubject.getPrincipals(Group JavaDoc.class);
93       Iterator JavaDoc iter = groups.iterator();
94       Group JavaDoc roles = null;
95       while( iter.hasNext() )
96       {
97          Group JavaDoc g = (Group JavaDoc) iter.next();
98          if( g.getName().equals("Roles") )
99          {
100             roles = g;
101             break;
102          }
103       }
104       // Map the group roles
105
if( roles != null )
106       {
107          
108          Enumeration JavaDoc members = roles.members();
109          while( members.hasMoreElements() )
110          {
111             Principal JavaDoc role = (Principal JavaDoc) members.nextElement();
112             String JavaDoc name = role.getName();
113             String JavaDoc mappedName = (String JavaDoc) roleMappings.get(name);
114             if( mappedName != null )
115             {
116                SimplePrincipal p = new SimplePrincipal(mappedName);
117                addedRoles.add(p);
118             }
119          }
120
121          Iterator JavaDoc riter = addedRoles.iterator();
122          while( riter.hasNext() )
123          {
124             Principal JavaDoc p = (Principal JavaDoc) riter.next();
125             roles.addMember(p);
126          }
127       }
128
129       return true;
130    }
131
132    public boolean abort() throws LoginException JavaDoc
133    {
134       return true;
135    }
136
137    /**
138     Remove the added roles
139     @return true
140     */

141    public boolean logout()
142    {
143       if( theSubject.isReadOnly() == false )
144       {
145          Set JavaDoc groups = theSubject.getPrincipals(Group JavaDoc.class);
146          Iterator JavaDoc iter = groups.iterator();
147          Group JavaDoc roles = null;
148          while( iter.hasNext() )
149          {
150             Group JavaDoc g = (Group JavaDoc) iter.next();
151             if( g.getName().equals("Roles") )
152             {
153                roles = g;
154                break;
155             }
156          }
157          // Remove the added roles
158
Iterator JavaDoc riter = addedRoles.iterator();
159          while( riter.hasNext() )
160          {
161             Principal JavaDoc p = (Principal JavaDoc) riter.next();
162             roles.removeMember(p);
163          }
164       }
165       return true;
166    }
167 }
168
Popular Tags