1 7 8 package org.jboss.security.auth.spi; 9 10 import java.security.Principal ; 11 import java.security.acl.Group ; 12 import java.util.Map ; 13 import java.util.StringTokenizer ; 14 import javax.security.auth.Subject ; 15 import javax.security.auth.callback.CallbackHandler ; 16 import javax.security.auth.login.LoginException ; 17 18 import org.jboss.security.SimpleGroup; 19 import org.jboss.security.SimplePrincipal; 20 import org.jboss.security.auth.spi.AbstractServerLoginModule; 21 22 37 public class IdentityLoginModule extends AbstractServerLoginModule 38 { 39 private String principalName; 40 private String roleNames; 41 42 public IdentityLoginModule() 43 { 44 } 45 46 public void initialize(Subject subject, CallbackHandler handler, Map sharedState, Map options) 47 { 48 super.initialize(subject, handler, sharedState, options); 49 principalName = (String ) options.get("principal"); 50 if( principalName == null ) 51 principalName = "guest"; 52 roleNames = (String ) options.get("roles"); 53 } 54 55 public boolean login() throws LoginException 56 { 57 if( super.login() == true ) 58 return true; 59 60 Principal principal = new SimplePrincipal(principalName); 61 subject.getPrincipals().add(principal); 62 sharedState.put("javax.security.auth.login.name", principalName); 64 super.loginOk = true; 65 return true; 66 } 67 68 protected Principal getIdentity() 69 { 70 Principal principal = new SimplePrincipal(principalName); 71 return principal; 72 } 73 74 protected Group [] getRoleSets() throws LoginException 75 { 76 SimpleGroup roles = new SimpleGroup("Roles"); 77 Group [] roleSets = {roles}; 78 if( roleNames != null ) 79 { 80 StringTokenizer tokenizer = new StringTokenizer (roleNames, ","); 81 while( tokenizer.hasMoreTokens() ) 82 { 83 String roleName = tokenizer.nextToken(); 84 roles.addMember(new SimplePrincipal(roleName)); 85 } 86 } 87 return roleSets; 88 } 89 } 90 | Popular Tags |