1 package com.atlassian.seraph.filter; 2 3 import org.apache.log4j.Category; 4 5 import javax.servlet.*; 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.http.HttpServletResponse ; 8 import java.io.IOException ; 9 import java.util.List ; 10 import java.util.Iterator ; 11 12 import com.atlassian.seraph.config.SecurityConfig; 13 import com.atlassian.seraph.auth.AuthenticatorException; 14 import com.atlassian.seraph.auth.Authenticator; 15 import com.atlassian.seraph.interceptor.LoginInterceptor; 16 17 41 public class LoginFilter implements Filter 42 { 43 private FilterConfig config = null; 44 45 private static final Category log = Category.getInstance(LoginFilter.class); 46 public static final String ALREADY_FILTERED = "loginfilter.already.filtered"; 47 48 public static final String LOGIN_SUCCESS = "success"; 49 public static final String LOGIN_FAILED = "failed"; 50 public static final String LOGIN_ERROR = "error"; 51 public static final String LOGIN_NOATTEMPT = null; 52 public static final String OS_AUTHSTATUS_KEY = "os_authstatus"; 53 private SecurityConfig securityConfig = null; 54 55 public void init(FilterConfig config) 56 { 57 this.config = config; 59 } 60 61 public void destroy() 62 { 63 config = null; 65 } 66 67 68 public FilterConfig getFilterConfig() 70 { 71 return config; 72 } 73 74 75 public void setFilterConfig(FilterConfig filterConfig) 77 { 78 if (filterConfig != null) init(filterConfig); 80 } 81 82 83 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 84 throws IOException , ServletException 85 { 86 req = new SecurityHttpRequestWrapper((HttpServletRequest ) req); 88 89 if (req.getAttribute(ALREADY_FILTERED) != null || !getSecurityConfig().getController().isSecurityEnabled()) 90 { 91 chain.doFilter(req, res); 92 return; 93 } 94 else 95 { 96 req.setAttribute(ALREADY_FILTERED, Boolean.TRUE); 97 } 98 99 req.setAttribute(OS_AUTHSTATUS_KEY, LOGIN_NOATTEMPT); 100 101 HttpServletRequest request = (HttpServletRequest ) req; 102 HttpServletResponse response = (HttpServletResponse ) res; 103 104 String username = request.getParameter("os_username"); 106 String password = request.getParameter("os_password"); 107 boolean persistentLogin = "true".equals(request.getParameter("os_cookie")); 108 109 boolean loggedIn = false; 110 111 if (username != null && password != null) 113 { 114 List interceptors = getSecurityConfig().getInterceptors(LoginInterceptor.class); 115 116 log.debug("Username and Password are not null - processing login request"); 117 try 118 { 119 for (Iterator iterator = interceptors.iterator(); iterator.hasNext();) 120 { 121 LoginInterceptor loginInterceptor = (LoginInterceptor) iterator.next(); 122 loginInterceptor.beforeLogin(request, response, username, password, persistentLogin); 123 } 124 125 loggedIn = getAuthenticator().login(request, response, username, password, persistentLogin); 126 127 if (loggedIn) 128 { 129 log.debug("Login was successful - setting attribute to \"Success\""); 130 request.setAttribute(OS_AUTHSTATUS_KEY, LOGIN_SUCCESS); 131 } 132 else 133 { 134 log.debug("Login was not successful - setting attribute to \"Failed\""); 135 request.setAttribute(OS_AUTHSTATUS_KEY, LOGIN_FAILED); 136 } 137 } 138 catch (AuthenticatorException e) 139 { 140 log.debug("Login was not successful, and exception was thrown - setting attribute to \"Error\""); 141 request.setAttribute(OS_AUTHSTATUS_KEY, LOGIN_ERROR); 142 e.printStackTrace(); 143 log.warn("Exception was thrown whilst logging in: " + e.getMessage(), e); 144 } 145 146 for (Iterator iterator = interceptors.iterator(); iterator.hasNext();) 147 { 148 LoginInterceptor loginInterceptor = (LoginInterceptor) iterator.next(); 149 loginInterceptor.afterLogin(request, response, username, password, persistentLogin, (String ) request.getAttribute(OS_AUTHSTATUS_KEY)); 150 } 151 } 152 153 if (loggedIn) 155 { 156 String originalURL = (String ) request.getSession().getAttribute(getSecurityConfig().getOriginalURLKey()); 157 158 if (originalURL != null) 159 { 160 if (log.isDebugEnabled()) 161 log.debug("Logged In - Redirecting to Original URL: " + request.getContextPath() + originalURL); 162 163 request.getSession().setAttribute(getSecurityConfig().getOriginalURLKey(), null); 164 ((HttpServletResponse ) res).sendRedirect(request.getContextPath() + originalURL); 165 return; 166 } 167 else if (request.getParameter("os_destination") != null) 168 { 169 if (log.isDebugEnabled()) 170 log.debug("Logged In - redirecting to os_destination: " + request.getContextPath() + request.getParameter("os_destination")); 171 172 ((HttpServletResponse ) res).sendRedirect(request.getContextPath() + request.getParameter("os_destination")); 173 return; 174 } 175 } 176 177 chain.doFilter(req, res); 178 } 179 180 protected Authenticator getAuthenticator() { 181 return getSecurityConfig().getAuthenticator(); 182 } 183 184 protected SecurityConfig getSecurityConfig() { 185 if (securityConfig == null) { 186 securityConfig = (SecurityConfig) config.getServletContext().getAttribute(SecurityConfig.STORAGE_KEY); 187 } 188 return securityConfig; 189 } 190 } 191 | Popular Tags |