01: package com.technoetic.xplanner.actions;
02:
03: import com.technoetic.xplanner.security.AuthenticationException;
04: import com.technoetic.xplanner.security.Authenticator;
05: import com.technoetic.xplanner.security.CredentialCookie;
06: import com.technoetic.xplanner.security.SecurityHelper;
07: import org.apache.commons.lang.StringUtils;
08: import org.apache.log4j.Logger;
09: import org.apache.struts.Globals;
10: import org.apache.struts.action.*;
11:
12: import java.util.Iterator;
13: import java.util.Map;
14: import javax.servlet.http.HttpServletRequest;
15: import javax.servlet.http.HttpServletResponse;
16:
17: public class AuthenticationAction extends Action {
18: private Logger log = Logger.getLogger(getClass());
19: private Authenticator authenticator;
20: public static final String AUTHENTICATION_MODULE_NAME_KEY = "authentication.module.name";
21: public static final String MODULE_MESSAGES_KEY = "moduleMessages";
22:
23: public void setAuthenticator(Authenticator authenticator) {
24: this .authenticator = authenticator;
25: }
26:
27: public ActionForward execute(ActionMapping actionMapping,
28: ActionForm actionForm,
29: HttpServletRequest httpServletRequest,
30: HttpServletResponse httpServletResponse) throws Exception {
31: ActionForward forward = actionMapping
32: .findForward("notAuthenticated");
33: DynaActionForm form = (DynaActionForm) actionForm;
34: if (StringUtils.isEmpty((String) form.get("action"))) {
35: return forward;
36: }
37: try {
38: String userId = (String) form.get("userId");
39: String password = (String) form.get("password");
40: authenticator.authenticate(httpServletRequest, userId,
41: password);
42: if (StringUtils.equals(httpServletRequest
43: .getParameter("remember"), "Y")) {
44: CredentialCookie credentials = new CredentialCookie(
45: httpServletRequest, httpServletResponse);
46: credentials.set(userId, password);
47: }
48: String savedUrl = SecurityHelper
49: .getSavedUrl(httpServletRequest);
50: if (savedUrl != null) {
51: return new ActionForward(savedUrl, true);
52: } else {
53: forward = actionMapping.findForward("authenticated");
54: }
55: } catch (AuthenticationException e) {
56: // Using message since text will be formatted slightly differently than the normal "error".
57: log.warn(e.getMessage() + ": " + e.getCause());
58: ActionMessages errors = new ActionMessages();
59: Map errorMap = e.getErrorsByModule();
60: errors.add(ActionMessages.GLOBAL_MESSAGE,
61: new ActionMessage("login.failed"));
62: for (Iterator iterator = errorMap.keySet().iterator(); iterator
63: .hasNext();) {
64: String moduleName = (String) iterator.next();
65: String message = (String) errorMap.get(moduleName);
66: errors.add(MODULE_MESSAGES_KEY, new ActionMessage(
67: message, moduleName));
68:
69: }
70: httpServletRequest
71: .setAttribute(Globals.MESSAGE_KEY, errors);
72: }
73: return forward;
74: }
75: }
|