1 7 8 package org.jboss.security; 9 10 11 import java.io.IOException ; 12 import java.security.Principal ; 13 import java.util.Map ; 14 import java.util.Set ; 15 import javax.security.auth.Subject ; 16 import javax.security.auth.callback.Callback ; 17 import javax.security.auth.callback.CallbackHandler ; 18 import javax.security.auth.callback.NameCallback ; 19 import javax.security.auth.callback.PasswordCallback ; 20 import javax.security.auth.callback.UnsupportedCallbackException ; 21 import javax.security.auth.login.LoginException ; 22 import javax.security.auth.spi.LoginModule ; 23 24 import org.jboss.logging.Logger; 25 26 55 public class ClientLoginModule implements LoginModule 56 { 57 private static Logger log = Logger.getLogger(ClientLoginModule.class); 58 private Subject subject; 59 private CallbackHandler callbackHandler; 60 61 private Principal loginPrincipal; 62 63 private Object loginCredential; 64 65 private Map sharedState; 66 67 private boolean useFirstPass; 68 71 private boolean restoreLoginIdentity; 72 73 private Principal restorePrincipal; 74 75 private Object restoreCredential; 76 77 private Subject restoreSubject; 78 private boolean trace; 79 80 85 public void initialize(Subject subject, CallbackHandler callbackHandler, 86 Map sharedState, Map options) 87 { 88 this.trace = log.isTraceEnabled(); 89 this.subject = subject; 90 this.callbackHandler = callbackHandler; 91 this.sharedState = sharedState; 92 String flag = (String ) options.get("multi-threaded"); 94 if (Boolean.valueOf(flag).booleanValue() == true) 95 { 98 if( trace ) 99 log.trace("Enabling multi-threaded mode"); 100 SecurityAssociationActions.setServer(); 101 } 102 103 flag = (String ) options.get("restore-login-identity"); 104 restoreLoginIdentity = Boolean.valueOf(flag).booleanValue(); 105 106 110 String passwordStacking = (String ) options.get("password-stacking"); 111 useFirstPass = passwordStacking != null; 112 } 113 114 117 public boolean login() throws LoginException 118 { 119 if( trace ) 120 log.trace("Begin login"); 121 if( restoreLoginIdentity == true ) 122 { 123 restorePrincipal = SecurityAssociationActions.getPrincipal(); 124 restoreCredential = SecurityAssociationActions.getCredential(); 125 restoreSubject = SecurityAssociationActions.getSubject(); 126 } 127 128 if (useFirstPass == true) 130 { 131 try 132 { 133 Object name = sharedState.get("javax.security.auth.login.name"); 134 if ((name instanceof Principal ) == false) 135 { 136 String username = name != null ? name.toString() : ""; 137 loginPrincipal = new SimplePrincipal(username); 138 } else 139 { 140 loginPrincipal = (Principal ) name; 141 } 142 loginCredential = sharedState.get("javax.security.auth.login.password"); 143 return true; 144 } 145 catch (Exception e) 146 { log.debug("Failed to obtain shared state", e); 148 } 149 } 150 151 154 if (callbackHandler == null) 155 throw new LoginException ("Error: no CallbackHandler available " + 156 "to garner authentication information from the user"); 157 158 PasswordCallback pc = new PasswordCallback ("Password: ", false); 159 NameCallback nc = new NameCallback ("User name: ", "guest"); 160 Callback [] callbacks = {nc, pc}; 161 try 162 { 163 String username; 164 char[] password = null; 165 char[] tmpPassword; 166 167 callbackHandler.handle(callbacks); 168 username = nc.getName(); 169 loginPrincipal = new SimplePrincipal(username); 170 tmpPassword = pc.getPassword(); 171 if (tmpPassword != null) 172 { 173 password = new char[tmpPassword.length]; 174 System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length); 175 pc.clearPassword(); 176 } 177 loginCredential = password; 178 if( trace ) 179 { 180 String credType = "null"; 181 if( loginCredential != null ) 182 credType = loginCredential.getClass().getName(); 183 log.trace("Obtained login: "+loginPrincipal 184 +", credential.class: " + credType); 185 } 186 } 187 catch (IOException ioe) 188 { 189 LoginException ex = new LoginException (ioe.toString()); 190 ex.initCause(ioe); 191 throw ex; 192 } 193 catch (UnsupportedCallbackException uce) 194 { 195 LoginException ex = new LoginException ("Error: " + uce.getCallback().toString() + 196 ", not able to use this callback for username/password"); 197 ex.initCause(uce); 198 throw ex; 199 } 200 if( trace ) 201 log.trace("End login"); 202 return true; 203 } 204 205 208 public boolean commit() throws LoginException 209 { 210 if( trace ) 211 log.trace("commit, subject="+subject); 212 SecurityAssociationActions.setPrincipalInfo(loginPrincipal, loginCredential, subject); 214 215 Set principals = subject.getPrincipals(); 217 if (principals.contains(loginPrincipal) == false) 218 principals.add(loginPrincipal); 219 return true; 220 } 221 222 225 public boolean abort() throws LoginException 226 { 227 if( trace ) 228 log.trace("abort"); 229 SecurityAssociationActions.clear(); 230 if( restoreLoginIdentity == true ) 231 { 232 SecurityAssociationActions.setPrincipalInfo(restorePrincipal, 233 restoreCredential, restoreSubject); 234 } 235 236 return true; 237 } 238 239 public boolean logout() throws LoginException 240 { 241 if( trace ) 242 log.trace("logout"); 243 SecurityAssociationActions.clear(); 244 if( restoreLoginIdentity == true ) 245 { 246 SecurityAssociationActions.setPrincipalInfo(restorePrincipal, 247 restoreCredential, restoreSubject); 248 } 249 Set principals = subject.getPrincipals(); 250 principals.remove(loginPrincipal); 251 return true; 252 } 253 } 254 | Popular Tags |