1 7 package org.jboss.security.auth.callback; 8 9 import java.lang.reflect.Method ; 10 import java.security.Principal ; 11 import javax.security.auth.callback.Callback ; 12 import javax.security.auth.callback.CallbackHandler ; 13 import javax.security.auth.callback.NameCallback ; 14 import javax.security.auth.callback.PasswordCallback ; 15 import javax.security.auth.callback.UnsupportedCallbackException ; 16 17 28 public class SecurityAssociationHandler implements CallbackHandler 29 { 30 private transient Principal principal; 31 private transient Object credential; 32 33 public SecurityAssociationHandler() 34 { 35 } 36 37 40 public SecurityAssociationHandler(Principal principal, Object credential) 41 { 42 this.principal = principal; 43 this.credential = credential; 44 } 45 46 public void setSecurityInfo(Principal principal, Object credential) 47 { 48 this.principal = principal; 49 this.credential = credential; 50 } 51 52 65 public void handle(Callback [] callbacks) throws 66 UnsupportedCallbackException 67 { 68 for (int i = 0; i < callbacks.length; i++) 69 { 70 Callback c = callbacks[i]; 71 if (c instanceof SecurityAssociationCallback) 72 { 73 SecurityAssociationCallback sac = (SecurityAssociationCallback) c; 74 sac.setPrincipal(principal); 75 sac.setCredential(credential); 76 } 77 else if (c instanceof ObjectCallback) 78 { 79 ObjectCallback oc = (ObjectCallback) c; 80 oc.setCredential(credential); 81 } 82 else if (c instanceof NameCallback ) 83 { 84 NameCallback nc = (NameCallback ) c; 85 if (principal != null) 86 nc.setName(principal.getName()); 87 } 88 else if (c instanceof PasswordCallback ) 89 { 90 PasswordCallback pc = (PasswordCallback ) c; 91 char[] password = getPassword(); 92 if (password != null) 93 pc.setPassword(password); 94 } 95 else 96 { 97 throw new UnsupportedCallbackException (c, "Unrecognized Callback"); 98 } 99 } 100 } 101 102 111 private char[] getPassword() 112 { 113 char[] password = null; 114 if (credential instanceof char[]) 115 { 116 password = (char[]) credential; 117 } 118 else if (credential instanceof String ) 119 { 120 String s = (String ) credential; 121 password = s.toCharArray(); 122 } 123 else 124 { 125 try 126 { 127 Class [] types = {}; 128 Method m = credential.getClass().getMethod("toCharArray", types); 129 Object [] args = {}; 130 password = (char[]) m.invoke(credential, args); 131 } 132 catch (Exception e) 133 { 134 if (credential != null) 135 { 136 String s = credential.toString(); 137 password = s.toCharArray(); 138 } 139 } 140 } 141 return password; 142 } 143 } 144 | Popular Tags |