1 22 package org.jboss.test.security.test.auth; 23 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.StringTokenizer ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import java.util.Enumeration ; 30 import java.util.HashSet ; 31 import java.security.acl.Group ; 32 import java.security.Principal ; 33 import javax.security.auth.spi.LoginModule ; 34 import javax.security.auth.Subject ; 35 import javax.security.auth.login.LoginException ; 36 import javax.security.auth.callback.CallbackHandler ; 37 38 import org.jboss.security.SimplePrincipal; 39 40 46 public class RoleMappingLoginModule implements LoginModule 47 { 48 49 private HashMap roleMappings = new HashMap (); 50 51 HashSet addedRoles = new HashSet (); 52 private Subject theSubject; 53 54 public void initialize(Subject subject, CallbackHandler callbackHandler, 55 Map sharedState, Map options) 56 { 57 this.theSubject = subject; 58 59 int count = 1; 60 String key = "role."; 61 String mapping = (String ) options.get(key+count); 62 while( mapping != null ) 63 { 64 StringTokenizer tokenizer = new StringTokenizer (mapping, "=,"); 65 String appRole = tokenizer.nextToken(); 66 while( tokenizer.hasMoreTokens() ) 67 { 68 String secDomainRole = tokenizer.nextToken(); 69 roleMappings.put(secDomainRole, appRole); 70 } 71 count ++; 72 mapping = (String ) options.get(key+count); 73 } 74 } 75 76 80 public boolean login() 81 { 82 return true; 83 } 84 85 90 public boolean commit() throws LoginException 91 { 92 Set groups = theSubject.getPrincipals(Group .class); 93 Iterator iter = groups.iterator(); 94 Group roles = null; 95 while( iter.hasNext() ) 96 { 97 Group g = (Group ) iter.next(); 98 if( g.getName().equals("Roles") ) 99 { 100 roles = g; 101 break; 102 } 103 } 104 if( roles != null ) 106 { 107 108 Enumeration members = roles.members(); 109 while( members.hasMoreElements() ) 110 { 111 Principal role = (Principal ) members.nextElement(); 112 String name = role.getName(); 113 String mappedName = (String ) roleMappings.get(name); 114 if( mappedName != null ) 115 { 116 SimplePrincipal p = new SimplePrincipal(mappedName); 117 addedRoles.add(p); 118 } 119 } 120 121 Iterator riter = addedRoles.iterator(); 122 while( riter.hasNext() ) 123 { 124 Principal p = (Principal ) riter.next(); 125 roles.addMember(p); 126 } 127 } 128 129 return true; 130 } 131 132 public boolean abort() throws LoginException 133 { 134 return true; 135 } 136 137 141 public boolean logout() 142 { 143 if( theSubject.isReadOnly() == false ) 144 { 145 Set groups = theSubject.getPrincipals(Group .class); 146 Iterator iter = groups.iterator(); 147 Group roles = null; 148 while( iter.hasNext() ) 149 { 150 Group g = (Group ) iter.next(); 151 if( g.getName().equals("Roles") ) 152 { 153 roles = g; 154 break; 155 } 156 } 157 Iterator riter = addedRoles.iterator(); 159 while( riter.hasNext() ) 160 { 161 Principal p = (Principal ) riter.next(); 162 roles.removeMember(p); 163 } 164 } 165 return true; 166 } 167 } 168 | Popular Tags |