1 22 package org.jboss.test.cmp2.audit.interfaces; 23 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 import javax.security.auth.login.LoginException ; 31 32 38 public class ApplicationCallbackHandler 39 implements CallbackHandler 40 { 41 43 private String user; 44 private char[] password; 45 46 48 private ApplicationCallbackHandler(String user, String password) 49 { 50 this.user = user; 51 this.password = password.toCharArray(); 52 } 53 54 56 public void handle(Callback [] callbacks) 57 throws UnsupportedCallbackException 58 { 59 for (int i = 0; i < callbacks.length; i++) 60 { 61 if (callbacks[i] instanceof NameCallback ) 62 { 63 NameCallback nameCallback = (NameCallback ) callbacks[i]; 64 nameCallback.setName(user); 65 } 66 else if (callbacks[i] instanceof PasswordCallback ) 67 { 68 PasswordCallback passwordCallback = (PasswordCallback ) callbacks[i]; 69 passwordCallback.setPassword(password); 70 } 71 else 72 { 73 throw new UnsupportedCallbackException (callbacks[i], "Unsupported callback"); 74 } 75 } 76 } 77 78 80 83 public static LoginContext login(String user, String password) 84 throws LoginException 85 { 86 return login("client-login", user, password); 87 } 88 89 97 public static LoginContext login(String config, String user, String password) 98 throws LoginException 99 { 100 ApplicationCallbackHandler handler = new ApplicationCallbackHandler(user, password); 101 LoginContext result = new LoginContext (config, handler); 102 result.login(); 103 return result; 104 } 105 } 106 | Popular Tags |