return;
}
if (userManager.userExists(username))
{
User user = null;
try
{
user = userManager.getUser(username);
}
catch (Exception e)
{
StatusMessage msg = new StatusMessage("User not accessible.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
userBean.setInvalidUser(true);
userBean.setQuestion( questionFactory.getRandomQuestion() );
setView(actionRequest, phase, SUCCESS2); // act like nothing happening
return;
}
userBean.setUsername(username);
userBean.setUser(user);
UserCredential credential = null;
try
{
credential = SecurityHelper.getCredential(userManager, user);
}
catch (SecurityException e)
{}
if (credential != null)
{
if (credential.isEnabled() == false)
{
userBean.setInvalidUser(true);
setView(actionRequest, phase, SUCCESS2);
userBean.setQuestion( questionFactory.getRandomQuestion() );
StatusMessage msg = new StatusMessage("The account has been disabled.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
return;
}
}
userBean.setUser(user);
userBean.setUsername(username);
// see if we have a valid MFA Cookie
Cookie mfaCookie = SecurityHelper.getMFACookie(actionRequest, username);
if (mfaCookie == null)
{
if (generateQuestionAndAnswer(userBean))
{
setView(actionRequest, phase, SUCCESS2);
}
else
{
// go into enrollment mode
setView(actionRequest, phase, SUCCESS3);
}
}
else
{
Map<String, SecurityAttribute> userAttributes = userBean.getUser().getSecurityAttributes().getAttributeMap();
String cookie = getUserAttribute(userAttributes, "user.cookie", username);
if (mfaCookie.getValue().equals(cookie))
{
userBean.setHasCookie(true);
userBean.setPassPhrase(getUserAttribute(userAttributes, "user.passphrase", ""));
setView(actionRequest, phase, SUCCESS1);
}
else
{
userBean.setHasCookie(false);
if (generateQuestionAndAnswer(userBean))
{
setView(actionRequest, phase, SUCCESS2);
}
else
{
// go into enrollment mode
setView(actionRequest, phase, SUCCESS3);
}
}
}
}
else
{
// Proceed on but mark the User Bean as invalid user to prevent user harvesting
// Also need to supply a random challenge question.
userBean.setInvalidUser(true);
userBean.setQuestion( questionFactory.getRandomQuestion() );
StatusMessage msg = new StatusMessage("The text entered does not match the displayed text.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, SUCCESS2);
}
}
else if (phase.equals("two"))
{
if (userBean.isInvalidUser())
{
// prevent harvesting
StatusMessage msg = new StatusMessage("Invalid User.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE1);
}
else
{
if (userBean.getUser() == null)
{
StatusMessage msg = new StatusMessage("User not accessible.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE1);
return;
}
String typedAnswer = actionRequest.getParameter("answer");
String publicTerminal = actionRequest.getParameter("publicTerminal");
userBean.setPublicTerminal(publicTerminal != null);
Map<String, SecurityAttribute> userAttributes = userBean.getUser().getSecurityAttributes().getAttributeMap();
int failures = Integer.parseInt(getUserAttribute(userAttributes, "user.question.failures", "0"));
if (SecurityHelper.isEmpty(typedAnswer) || !typedAnswer.equalsIgnoreCase(userBean.getAnswer()))
{
int count = failures + 1;
if (count >= this.maxNumberOfAuthenticationFailures)
{
try
{
RequestContext rc = SecurityHelper.getRequestContext(actionRequest);
User user = userManager.getUser(userBean.getUsername());
PasswordCredential pwc = userManager.getPasswordCredential(user);
pwc.setEnabled(false);
userManager.storePasswordCredential(pwc);
SecurityHelper.updateCredentialInSession(rc, pwc);
userBean.setUser(user);
userAttributes = userBean.getUser().getSecurityAttributes().getAttributeMap();
user.getSecurityAttributes().getAttribute("user.question.failures", true).setStringValue("0");
userManager.updateUser(user);
audit.logUserActivity(userBean.getUsername(),
rc.getRequest().getRemoteAddr(),
AuditActivity.USER_DISABLE, "Failed question and answer limit reached");
}
catch (Exception e)
{
}
StatusMessage msg = new StatusMessage("Disabling user after too many failed questions.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE2);
}
else
{
try
{
userBean.getUser().getSecurityAttributes().getAttribute("user.question.failures", true).setStringValue(Integer.toString(count));
userManager.updateUser(userBean.getUser());
}
catch (SecurityException e)
{}
StatusMessage msg = new StatusMessage("Invalid answer to question.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE1);
}
}
else
{
try
{
userBean.setPassPhrase(getUserAttribute(userAttributes, "user.passphrase", ""));
userBean.getUser().getSecurityAttributes().getAttribute("user.question.failures", true).setStringValue("0");
userManager.updateUser(userBean.getUser());
}
catch (Exception e)
{}
setView(actionRequest, phase, SUCCESS1);
}
}
}
else if (phase.equals("enroll"))
{
boolean success = false;
String password = userBean.getPassword();
User user = userBean.getUser();
if (user != null && password != null)
{
AuthenticatedUser authUser = null;
try
{
authUser = authorization.authenticate(userBean.getUsername(), password);
}
catch (SecurityException e)
{
RequestContext rc = SecurityHelper.getRequestContext(actionRequest);
audit.logUserActivity(userBean.getUsername(), rc.getRequest().getRemoteAddr(),
AuditActivity.AUTHENTICATION_FAILURE, "PortalFilter");
}
if (authUser != null)
{
// validate request parameers, if valid update user preferences
String question1 = actionRequest.getParameter("question1");
String question2 = actionRequest.getParameter("question2");
String question3 = actionRequest.getParameter("question3");
String answer1 = actionRequest.getParameter("answer1");
String answer2 = actionRequest.getParameter("answer2");
String answer3 = actionRequest.getParameter("answer3");
String passPhrase = actionRequest.getParameter("passphrase");
// validation (SecurityHelper.isEmpty, unique questions)
if (SecurityHelper.isEmpty(answer1) || SecurityHelper.isEmpty(answer2) || SecurityHelper.isEmpty(answer3))
{
StatusMessage msg = new StatusMessage("Please enter a valid answer for all 3 questions.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE1);
return;
}
if (SecurityHelper.isEmpty(passPhrase))
{
StatusMessage msg = new StatusMessage("Please enter a valid pass phrase.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE1);
return;
}
if (question1.equals(question2) || question1.equals(question3) || question2.equals(question3))
{
StatusMessage msg = new StatusMessage("Please select a unique question in all cases.", StatusMessage.ERROR);
PortletMessaging.publish(actionRequest, STATUS_MESSAGE, msg);
setView(actionRequest, phase, FAILURE1);
return;
}
// update the prefs db (we are not logged in yet
user = userBean.getUser();
try
{
user.getSecurityAttributes().getAttribute("user.question.1", true).setStringValue(question1);
user.getSecurityAttributes().getAttribute("user.question.2", true).setStringValue(question2);
user.getSecurityAttributes().getAttribute("user.question.3", true).setStringValue(question3);
user.getSecurityAttributes().getAttribute("user.answer.1", true).setStringValue(answer1);
user.getSecurityAttributes().getAttribute("user.answer.2", true).setStringValue(answer2);
user.getSecurityAttributes().getAttribute("user.answer.3", true).setStringValue(answer3);
user.getSecurityAttributes().getAttribute("user.passphrase", true).setStringValue(passPhrase);
user.getSecurityAttributes().getAttribute("user.cookie", true).setStringValue(CaptchaImageResource.randomString(8, 16));
userManager.updateUser(user);
success = true;
}
catch (SecurityException e)
{