1 4 package com.openedit.users.authenticate; 5 6 import java.io.IOException ; 7 8 import javax.security.auth.callback.Callback ; 9 import javax.security.auth.callback.CallbackHandler ; 10 import javax.security.auth.callback.NameCallback ; 11 import javax.security.auth.callback.PasswordCallback ; 12 import javax.security.auth.callback.UnsupportedCallbackException ; 13 import javax.security.auth.login.LoginContext ; 14 import javax.security.auth.login.LoginException ; 15 import javax.security.sasl.RealmCallback ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 20 import com.openedit.users.Authenticator; 21 import com.openedit.users.User; 22 import com.openedit.users.UserManagerException; 23 24 public class JaasAuthenticator implements Authenticator 25 { 26 private static final Log log = LogFactory.getLog(JaasAuthenticator.class); 27 28 public boolean authenticate(User inUser, String inPassword) throws UserManagerException 29 { 30 try 35 { 36 LoginContext loginContext = new LoginContext ("OpenEditConfig", new UserPasswordCallbackHandler(inUser, inPassword)); 37 loginContext.login(); 38 39 } 42 catch (LoginException le) 43 { 44 log.error("Cannot create LoginContext. " + le.getMessage()); 45 return false; 46 } 47 catch (SecurityException se) 48 { 49 log.error("Cannot create LoginContext. " + se.getMessage()); 50 return false; 51 } 52 return true; 53 } 54 55 public class UserPasswordCallbackHandler implements CallbackHandler 56 { 57 private User fieldUser; 58 private String mPassword; 59 60 63 public UserPasswordCallbackHandler(User username, String password) 64 { 65 fieldUser = username; 66 mPassword = password; 67 } 68 69 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException 70 { 71 for (int ii = 0; ii < callbacks.length; ii++) 72 { 73 if (callbacks[ii] instanceof NameCallback ) 74 { 75 NameCallback ncb = (NameCallback ) callbacks[ii]; 76 ncb.setName(fieldUser.getUserName()); 77 } 78 else if (callbacks[ii] instanceof PasswordCallback ) 79 { 80 PasswordCallback pcb = (PasswordCallback ) callbacks[ii]; 81 pcb.setPassword(mPassword.toCharArray()); 82 } 83 else if (callbacks[ii] instanceof RealmCallback ) 84 { 85 RealmCallback rcb = (RealmCallback ) callbacks[ii]; 86 String realm = fieldUser.getString("realm"); 87 if( realm != null) 88 { 89 rcb.setText(realm); 90 } 91 } 92 } 93 } 94 95 } 96 97 } 98 | Popular Tags |