1 7 package org.jboss.security.auth.spi; 8 9 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 ; 17 import javax.security.auth.callback.CallbackHandler ; 18 import javax.security.auth.login.LoginException ; 19 import java.security.Principal ; 20 import java.security.acl.Group ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 35 public class DeploymentRolesLoginModule extends AbstractServerLoginModule 36 { 37 46 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) 47 { 48 super.initialize(subject, callbackHandler, sharedState, options); 49 50 useFirstPass = true; 52 } 53 54 58 protected Principal getIdentity() 59 { 60 Object username = sharedState.get("javax.security.auth.login.name"); 62 if(username == null) 63 throw new IllegalStateException ("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 )username); 69 } 70 71 76 protected Group[] getRoleSets() throws LoginException 77 { 78 Group group = new SimpleGroup("Roles"); 79 Iterator itRoleNames = getSecurityRoleNames().iterator(); 80 while (itRoleNames.hasNext()) 81 { 82 String roleName = (String ) itRoleNames.next(); 83 group.addMember(new SimplePrincipal(roleName)); 84 } 85 86 return new Group[]{group}; 87 } 88 89 93 private Set getSecurityRoleNames() 94 { 95 HashSet roleNames = new HashSet (); 96 String userName = getIdentity().getName(); 97 98 Map securityRoles = SecurityRolesAssociation.getSecurityRoles(); 99 if (securityRoles != null) 100 { 101 Iterator 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 |