public boolean login() throws LoginException {
// If useFirstPass is true, look for the shared password
if( useFirstPass == true )
{
try
{
Object user = sharedState.get("javax.security.auth.login.name");
Principal principal;
if( (user instanceof Principal) == false )
{
String username = user != null ? user.toString() : "";
principal = new SimplePrincipal(username);
}
else
{
principal = (Principal) user;
}
Object credential = sharedState.get("javax.security.auth.login.password");
SecurityAssociation.setPrincipal(principal);
SecurityAssociation.setCredential(credential);
SecurityAssociation.setSubject(subject);
return true;
}
catch(Exception e)
{ // Dump the exception and continue
e.printStackTrace();
}
}
/* There is no password sharing or we are the first login module. Get
the username and password from the callback hander.
*/
if (callbackHandler == null)
throw new LoginException("Error: no CallbackHandler available " +
"to garner authentication information from the user");
PasswordCallback pc = new PasswordCallback("Password: ", false);
NameCallback nc = new NameCallback("User name: ", "guest");
Callback[] callbacks = {nc, pc};
try
{
String username;
char[] password = null;
char[] tmpPassword;
callbackHandler.handle(callbacks);
username = nc.getName();
SecurityAssociation.setPrincipal(new SimplePrincipal(username));
tmpPassword = pc.getPassword();
if (tmpPassword != null)
{
password = new char[tmpPassword.length];
System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
pc.clearPassword();
}
SecurityAssociation.setCredential(password);
SecurityAssociation.setSubject(subject);
}
catch (java.io.IOException ioe)
{
throw new LoginException(ioe.toString());
}
catch (UnsupportedCallbackException uce)
{
throw new LoginException("Error: " + uce.getCallback().toString() +
" not available to garner authentication information " +
"from the user");
}
return true;
}
Method to authenticate a Subject (phase 1). |