Friday, January 18, 2008

Homemade WebSphere LDAP Authentication

import java.util.HashSet;
import java.util.Set;

import javax.faces.context.FacesContext;
import javax.security.auth.login.AccountExpiredException;
import javax.security.auth.login.CredentialExpiredException;
import javax.security.auth.login.FailedLoginException ;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl;
import uk.org.gth.exceptions.LoginException;

public class LoginUtils {

public static void doLogin(String username, String password, FacesContext facesContext)
throws LoginException {
LoginContext loginContext = null;

try {
loginContext = new LoginContext("WSLogin", new WSCallbackHandlerImpl(username, password));
loginContext.login();
} catch (AccountExpiredException e) {
throw new LoginException("Account has Expired.");
} catch (CredentialExpiredException e) {
throw new LoginException("Credentials have Expired.");
} catch (FailedLoginException e) {
throw new LoginException("Login Failure.");
} catch (LoginException e) {
if (e.getMessage().indexOf("52e") > 0) {
throw new LoginException("Invalid Password.");
} else if (e.getMessage().indexOf("532") > 0) {
throw new LoginException("Password Has Expired.");
} else if (e.getMessage().indexOf("533") > 0) {
throw new LoginException("Your account has been disabled.");
} else if (e.getMessage().indexOf("701") > 0) {
throw new LoginException("Your account has expired.");
} else if (e.getMessage().indexOf("773") > 0) {
throw new LoginException("Your password must be reset.");
} else {
throw new LoginException(e.getMessage());
}
} catch (SecurityException e) {
throw new LoginException("Cannot create LoginContext.");
}
}

}

No comments: