1 17 18 package org.apache.geronimo.connector.outbound.security; 19 20 import java.io.IOException ; 21 import java.util.Map ; 22 23 import javax.resource.spi.security.PasswordCredential ; 24 import javax.security.auth.Subject ; 25 import javax.security.auth.callback.Callback ; 26 import javax.security.auth.callback.CallbackHandler ; 27 import javax.security.auth.callback.NameCallback ; 28 import javax.security.auth.callback.PasswordCallback ; 29 import javax.security.auth.callback.UnsupportedCallbackException ; 30 import javax.security.auth.login.LoginException ; 31 import javax.security.auth.spi.LoginModule ; 32 33 39 public class PasswordCredentialLoginModule implements LoginModule { 40 41 private Subject subject; 42 private CallbackHandler callbackHandler; 43 44 private PasswordCredentialRealm passwordCredentialRealm; 45 46 private String resourcePrincipalName; 47 private String userName; 48 private char[] password; 49 50 public void initialize(Subject subject, CallbackHandler callbackHandler, 51 Map sharedState, Map options) { 52 this.subject = subject; 53 this.callbackHandler = callbackHandler; 54 passwordCredentialRealm = (PasswordCredentialRealm) options.get(PasswordCredentialRealm.REALM_INSTANCE); 55 if (passwordCredentialRealm == null) { 56 throw new IllegalArgumentException ("No realm supplied in options"); 57 } 58 } 59 60 public boolean login() throws LoginException { 61 if (passwordCredentialRealm == null || passwordCredentialRealm.managedConnectionFactory == null) { 62 return false; 63 } 64 Callback [] callbacks = new Callback [3]; 65 66 callbacks[0] = new NameCallback ("Resource Principal"); 67 callbacks[1] = new NameCallback ("User name"); 68 callbacks[2] = new PasswordCallback ("Password", false); 69 try { 70 callbackHandler.handle(callbacks); 71 } catch (IOException ioe) { 72 throw (LoginException ) new LoginException ().initCause(ioe); 73 } catch (UnsupportedCallbackException uce) { 74 throw (LoginException ) new LoginException ().initCause(uce); 75 } 76 resourcePrincipalName = ((NameCallback ) callbacks[0]).getName(); 77 userName = ((NameCallback ) callbacks[1]).getName(); 78 password = ((PasswordCallback ) callbacks[2]).getPassword(); 79 return resourcePrincipalName != null && userName != null && password != null; 80 } 81 82 public boolean commit() throws LoginException { 83 subject.getPrincipals().add(new ResourcePrincipal(resourcePrincipalName)); 84 PasswordCredential passwordCredential = new PasswordCredential (userName, password); 85 passwordCredential.setManagedConnectionFactory(passwordCredentialRealm.getManagedConnectionFactory()); 86 subject.getPrivateCredentials().add(passwordCredential); 87 return true; 88 } 89 90 public boolean abort() throws LoginException { 91 subject = null; 92 userName = null; 93 password = null; 94 return true; 95 } 96 97 public boolean logout() throws LoginException { 98 subject = null; 99 userName = null; 100 password = null; 101 return true; 102 } 103 } 104 | Popular Tags |