1 17 package org.apache.servicemix.jbi.security.auth.impl; 18 19 import java.io.IOException ; 20 import java.security.GeneralSecurityException ; 21 import java.security.cert.X509Certificate ; 22 23 import javax.security.auth.Subject ; 24 import javax.security.auth.callback.Callback ; 25 import javax.security.auth.callback.CallbackHandler ; 26 import javax.security.auth.callback.NameCallback ; 27 import javax.security.auth.callback.PasswordCallback ; 28 import javax.security.auth.callback.UnsupportedCallbackException ; 29 import javax.security.auth.login.LoginContext ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.servicemix.jbi.security.auth.AuthenticationService; 34 import org.apache.servicemix.jbi.security.login.CertificateCallback; 35 36 41 public class JAASAuthenticationService implements AuthenticationService { 42 43 private static final Log log = LogFactory.getLog(JAASAuthenticationService.class); 44 45 public void authenticate(Subject subject, 46 String domain, 47 final String user, 48 final Object credentials) throws GeneralSecurityException { 49 if (log.isDebugEnabled()) { 50 log.debug("Authenticating '" + user + "' with '" + credentials + "'"); 51 } 52 LoginContext loginContext = new LoginContext (domain, subject, new CallbackHandler () { 53 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 54 for (int i = 0; i < callbacks.length; i++) { 55 if (callbacks[i] instanceof NameCallback ) { 56 ((NameCallback ) callbacks[i]).setName(user); 57 } else if (callbacks[i] instanceof PasswordCallback && credentials instanceof String ) { 58 ((PasswordCallback ) callbacks[i]).setPassword(((String ) credentials).toCharArray()); 59 } else if (callbacks[i] instanceof CertificateCallback && credentials instanceof X509Certificate ) { 60 ((CertificateCallback) callbacks[i]).setCertificate((X509Certificate ) credentials); 61 } else { 62 throw new UnsupportedCallbackException (callbacks[i]); 63 } 64 } 65 } 66 }); 67 loginContext.login(); 68 } 69 70 } 71 | Popular Tags |