package org.jboss.security;
import java.util.Map;
import java.util.Set;
import java.security.Principal;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
public class AltClientLoginModule implements LoginModule
{
private Subject subject;
private CallbackHandler callbackHandler;
private Map sharedState;
private boolean useFirstPass;
private String username;
private char[] password = null;
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map sharedState, Map options)
{
this.subject = subject;
this.callbackHandler = callbackHandler;
this.sharedState = sharedState;
String mt = (String) options.get("multi-threaded");
if( mt != null && Boolean.valueOf(mt).booleanValue() == true )
{
SecurityAssociationActions.setServer();
}
String passwordStacking = (String) options.get("password-stacking");
useFirstPass = passwordStacking != null;
}
public boolean login() throws LoginException
{
if( useFirstPass == true )
{
return true;
}
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
{
char[] tmpPassword;
callbackHandler.handle(callbacks);
username = nc.getName();
tmpPassword = pc.getPassword();
if (tmpPassword != null)
{
password = new char[tmpPassword.length];
System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
pc.clearPassword();
}
}
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;
}
public boolean commit() throws LoginException
{
Set principals = subject.getPrincipals();
Principal p = null;
Object credential = password;
if( useFirstPass == true )
{
Object user = sharedState.get("javax.security.auth.login.name");
if( (user instanceof Principal) == false )
{
username = user != null ? user.toString() : "";
p = new SimplePrincipal(username);
}
else
{
p = (Principal) user;
}
credential = sharedState.get("javax.security.auth.login.password");
}
else
{
p = new SimplePrincipal(username);
}
if( principals.isEmpty() == false )
p = (Principal) principals.iterator().next();
SecurityAssociationActions.setPrincipalInfo(p, credential, subject);
return true;
}
public boolean abort() throws LoginException
{
int length = password != null ? password.length : 0;
for(int n = 0; n < length; n ++)
password[n] = 0;
SecurityAssociationActions.clear();
return true;
}
public boolean logout() throws LoginException
{
SecurityAssociationActions.clear();
return true;
}
}