1 7 package org.jboss.test; 8 9 import java.util.Map ; 10 import javax.security.auth.Subject ; 11 import javax.security.auth.callback.CallbackHandler ; 12 import javax.security.auth.login.LoginException ; 13 import javax.security.auth.spi.LoginModule ; 14 import org.jboss.security.SimplePrincipal; 15 16 public class TestLoginModule implements LoginModule 17 { 18 Subject subject; 19 String principal; 20 String name; 21 boolean succeed; 22 boolean throwEx; 23 24 public TestLoginModule() 25 { 26 } 27 28 public void initialize(Subject subject, CallbackHandler handler, Map sharedState, Map options) 29 { 30 this.subject = subject; 31 principal = (String ) options.get("principal"); 32 if( principal == null ) 33 principal = "guest"; 34 name = (String ) options.get("name"); 35 String opt = (String ) options.get("succeed"); 36 succeed = Boolean.valueOf(opt).booleanValue(); 37 opt = (String ) options.get("throwEx"); 38 throwEx = Boolean.valueOf(opt).booleanValue(); 39 System.out.println("initialize, name="+name); 40 opt = (String ) options.get("initEx"); 41 if( Boolean.valueOf(opt) == Boolean.TRUE ) 42 throw new IllegalArgumentException ("Failed during init, name="+name); 43 } 44 45 public boolean login() throws LoginException 46 { 47 System.out.println("login, name="+name+", succeed="+succeed); 48 if( throwEx ) 49 throw new LoginException ("Failed during login, name="+name); 50 return succeed; 51 } 52 53 public boolean commit() throws LoginException 54 { 55 System.out.println("commit, name="+name); 56 subject.getPrincipals().add(new SimplePrincipal(principal)); 57 subject.getPublicCredentials().add("A public credential"); 58 subject.getPrivateCredentials().add("A private credential"); 59 return true; 60 } 61 62 public boolean abort() throws LoginException 63 { 64 System.out.println("abort, name="+name); 65 return true; 66 } 67 68 public boolean logout() throws LoginException 69 { 70 System.out.println("logout, name="+name); 71 subject.getPrincipals().remove(new SimplePrincipal(principal)); 72 return succeed; 73 } 74 75 } 76 | Popular Tags |