1 17 18 19 package org.apache.catalina.realm; 20 21 22 import java.io.IOException ; 23 import javax.security.auth.callback.Callback ; 24 import javax.security.auth.callback.CallbackHandler ; 25 import javax.security.auth.callback.NameCallback ; 26 import javax.security.auth.callback.PasswordCallback ; 27 import javax.security.auth.callback.UnsupportedCallbackException ; 28 29 import org.apache.catalina.util.StringManager; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 50 51 public class JAASCallbackHandler implements CallbackHandler { 52 private static Log log = LogFactory.getLog(JAASCallbackHandler.class); 53 54 56 57 66 public JAASCallbackHandler(JAASRealm realm, String username, 67 String password) { 68 69 super(); 70 this.realm = realm; 71 this.username = username; 72 73 if (realm.hasMessageDigest()) { 74 this.password = realm.digest(password); 75 } 76 else { 77 this.password = password; 78 } 79 } 80 81 82 84 87 protected static final StringManager sm = 88 StringManager.getManager(Constants.Package); 89 90 93 protected String password = null; 94 95 96 99 protected JAASRealm realm = null; 100 101 102 105 protected String username = null; 106 107 108 110 111 122 public void handle(Callback callbacks[]) 123 throws IOException , UnsupportedCallbackException { 124 125 for (int i = 0; i < callbacks.length; i++) { 126 127 if (callbacks[i] instanceof NameCallback ) { 128 if (realm.getContainer().getLogger().isTraceEnabled()) 129 realm.getContainer().getLogger().trace(sm.getString("jaasCallback.username", username)); 130 ((NameCallback ) callbacks[i]).setName(username); 131 } else if (callbacks[i] instanceof PasswordCallback ) { 132 final char[] passwordcontents; 133 if (password != null) { 134 passwordcontents = password.toCharArray(); 135 } else { 136 passwordcontents = new char[0]; 137 } 138 ((PasswordCallback ) callbacks[i]).setPassword 139 (passwordcontents); 140 } else { 141 throw new UnsupportedCallbackException (callbacks[i]); 142 } 143 } 144 } 145 } 146 | Popular Tags |