1 7 package org.jboss.security.auth.callback; 8 9 import javax.security.auth.callback.Callback ; 10 import javax.security.auth.callback.CallbackHandler ; 11 import javax.security.auth.callback.NameCallback ; 12 import javax.security.auth.callback.PasswordCallback ; 13 import javax.security.auth.callback.UnsupportedCallbackException ; 14 15 26 public class UsernamePasswordHandler implements CallbackHandler 27 { 28 private transient String username; 29 private transient char[] password; 30 private transient Object credential; 31 32 35 public UsernamePasswordHandler(String username, char[] password) 36 { 37 this.username = username; 38 this.password = password; 39 this.credential = password; 40 } 41 42 public UsernamePasswordHandler(String username, Object credential) 43 { 44 this.username = username; 45 this.credential = credential; 46 } 47 48 54 public void handle(Callback [] callbacks) throws 55 UnsupportedCallbackException 56 { 57 for (int i = 0; i < callbacks.length; i++) 58 { 59 Callback c = callbacks[i]; 60 if (c instanceof NameCallback ) 61 { 62 NameCallback nc = (NameCallback ) c; 63 nc.setName(username); 64 } 65 else if (c instanceof PasswordCallback ) 66 { 67 PasswordCallback pc = (PasswordCallback ) c; 68 if( password == null ) 69 { 70 if( credential != null ) 72 { 73 String tmp = credential.toString(); 74 password = tmp.toCharArray(); 75 } 76 } 77 pc.setPassword(password); 78 } 79 else if (c instanceof ObjectCallback) 80 { 81 ObjectCallback oc = (ObjectCallback) c; 82 oc.setCredential(credential); 83 } 84 else 85 { 86 throw new UnsupportedCallbackException (callbacks[i], "Unrecognized Callback"); 87 } 88 } 89 } 90 } 91 | Popular Tags |