1 package org.jbpm.identity.security; 2 3 import java.util.*; 4 import javax.security.auth.*; 5 import javax.security.auth.callback.*; 6 import javax.security.auth.login.*; 7 import javax.security.auth.spi.*; 8 import org.jbpm.identity.*; 9 10 16 public class IdentityLoginModule implements LoginModule { 17 18 Subject subject = null; 19 CallbackHandler callbackHandler = null; 20 Map sharedState = null; 21 Map options = null; 22 23 26 IdentityService identityService = null; 27 28 Object validatedUserId = null; 29 String validatedPwd = null; 30 31 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 32 this.subject = subject; 33 this.callbackHandler = callbackHandler; 34 this.sharedState = sharedState; 35 this.options = options; 36 } 37 38 public boolean login() throws LoginException { 39 40 NameCallback nameCallback = new NameCallback(null); 42 PasswordCallback passwordCallback = new PasswordCallback(null,false); 43 try { 44 callbackHandler.handle(new Callback[]{nameCallback, passwordCallback}); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 throw new LoginException("callback failed"); 48 } 49 String userName = nameCallback.getName(); 50 String pwd = new String (passwordCallback.getPassword()); 51 52 Object userId = identityService.verify(userName, pwd); 54 55 boolean success = (userId!=null); 56 if (success) { 58 validatedUserId = userId; 61 validatedPwd = pwd; 62 } else { 63 validatedUserId = null; 64 validatedPwd = null; 65 } 66 67 return success; 68 } 69 70 public boolean commit() throws LoginException { 71 72 User user = identityService.getUserById(validatedUserId); 73 if (user==null) { 74 throw new LoginException("no user for validated user id '"+validatedUserId); 75 } 76 77 subject.getPrincipals().add(user); 79 subject.getPrivateCredentials().add(new Username(user.getName())); 80 subject.getPrivateCredentials().add(new Password(validatedPwd)); 81 82 AuthenticatedUser.setAuthenticatedUser(user); 84 85 return true; 86 } 87 88 public boolean abort() throws LoginException { 89 return logout(); 90 } 91 92 public boolean logout() throws LoginException { 93 if(subject!= null){ 94 subject.getPrincipals().clear(); 97 subject.getPublicCredentials().clear(); 98 subject.getPrivateCredentials().clear(); 99 } 100 101 AuthenticatedUser.setAuthenticatedUser(null); 103 104 callbackHandler = null; 105 sharedState = null; 106 options = null; 107 validatedUserId = null; 108 validatedPwd = null; 109 return true; 110 } 111 } 112 | Popular Tags |