1 9 package com.atlassian.seraph.auth; 10 11 import com.atlassian.seraph.config.SecurityConfig; 12 import com.atlassian.seraph.config.SecurityConfigFactory; 13 import com.atlassian.seraph.util.CookieUtils; 14 import com.atlassian.seraph.interceptor.LogoutInterceptor; 15 import com.opensymphony.user.UserManager; 16 import com.opensymphony.user.EntityNotFoundException; 17 import com.opensymphony.user.User; 18 import org.apache.log4j.Category; 19 20 import javax.servlet.http.Cookie ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import java.util.List ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.LinkedList ; 27 import java.security.Principal ; 28 29 36 public class DefaultAuthenticator extends AbstractAuthenticator 37 { 38 private String loginCookieKey; 39 40 private static final Category log = Category.getInstance(DefaultAuthenticator.class); 41 42 45 public static String LOGGED_IN_KEY = "seraph_defaultauthenticator_user"; 46 47 51 public static String LOGGED_OUT_KEY = "seraph_defaultauthenticator_logged_out_user"; 52 53 private static int AUTOLOGIN_COOKIE_AGE = 365 * 24 * 60 * 60; 55 56 public void init(Map params, SecurityConfig config) 57 { 58 log.debug(this.getClass().getName()+" $Revision: 1.11 $ initializing"); 59 super.init(params, config); 60 this.loginCookieKey = config.getLoginCookieKey(); 61 } 62 63 64 public boolean isUserInRole(HttpServletRequest request, String role) 65 { 66 return getRoleMapper().hasRole(getUser(request), request, role); 67 } 68 69 75 public boolean login(HttpServletRequest request, HttpServletResponse response, String username, String password, boolean cookie) throws AuthenticatorException 76 { 77 Principal user = getUser(username); 78 79 if (user == null) 81 { 82 log.info("Cannot login user '" + username + "' as they do not exist."); 83 } 84 else 85 { 86 boolean authenticated = authenticate(user, password); 87 if (authenticated) 88 { 89 request.getSession().setAttribute(LOGGED_IN_KEY, user); 90 request.getSession().setAttribute(LOGGED_OUT_KEY, null); 91 92 if (getRoleMapper().canLogin(user, request)) 93 { 94 if (cookie && response != null) 95 { 96 CookieUtils.setCookie(request, response, getLoginCookieKey(), CookieUtils.encodePasswordCookie(username, password, getConfig().getCookieEncoding()), AUTOLOGIN_COOKIE_AGE, getCookiePath(request)); 97 } 98 return true; 99 } 100 else 101 { 102 request.getSession().removeAttribute(LOGGED_IN_KEY); 103 } 104 } 105 else 106 { 107 log.info("Cannot login user '" + username + "' as they used an incorrect password"); 108 } 109 } 110 111 112 if (response != null && CookieUtils.getCookie(request, getLoginCookieKey()) != null) 113 { 114 log.warn("User: " + username + " tried to login but they do not have USE permission or weren't found. Deleting cookie."); 115 116 try 117 { 118 CookieUtils.invalidateCookie(request, response, getLoginCookieKey(), getCookiePath(request)); 119 } 120 catch (Exception e) 121 { 122 log.error("Could not invalidate cookie: " + e, e); 123 } 124 } 125 126 return false; 127 } 128 129 protected RoleMapper getRoleMapper() { 131 return SecurityConfigFactory.getInstance().getRoleMapper(); 132 } 133 134 136 137 protected Principal getUser(String username) 138 { 139 try 140 { 141 return UserManager.getInstance().getUser(username); 142 } 143 catch (EntityNotFoundException e) 144 { 145 log.debug("Could not find user who tried to login: " + e); 146 } 147 return null; 148 } 149 150 151 protected boolean authenticate(Principal user, String password) 152 { 153 return ((User)user).authenticate(password); 154 } 155 156 public boolean logout(HttpServletRequest request, HttpServletResponse response) throws AuthenticatorException 157 { 158 List interceptors = getLogoutInterceptors(); 159 160 for (Iterator iterator = interceptors.iterator(); iterator.hasNext();) 161 { 162 LogoutInterceptor interceptor = (LogoutInterceptor) iterator.next(); 163 interceptor.beforeLogout(request, response); 164 } 165 166 request.getSession().setAttribute(LOGGED_IN_KEY, null); 167 request.getSession().setAttribute(LOGGED_OUT_KEY, Boolean.TRUE); 168 169 try 170 { 171 CookieUtils.invalidateCookie(request, response, getLoginCookieKey(), getCookiePath(request)); 172 } 173 catch (Exception e) 174 { 175 log.error("Could not invalidate cookie: " + e, e); 176 } 177 178 for (Iterator iterator = interceptors.iterator(); iterator.hasNext();) 179 { 180 LogoutInterceptor interceptor = (LogoutInterceptor) iterator.next(); 181 interceptor.afterLogout(request, response); 182 } 183 184 return true; 185 } 186 187 192 public Principal getUser(HttpServletRequest request, HttpServletResponse response) 193 { 194 Principal user = null; 195 196 try 197 { 198 if (request.getSession() != null && request.getSession().getAttribute(LOGGED_OUT_KEY) != null) 200 { 201 log.debug("Session found; user already logged in"); 202 user = null; 203 } 204 else if(request.getSession() != null && request.getSession().getAttribute(LOGGED_IN_KEY) != null) 205 { 206 log.debug("Session found; user already logged in"); 207 user = (Principal ) request.getSession().getAttribute(LOGGED_IN_KEY); 208 } 209 else 210 { 211 Cookie cookie = CookieUtils.getCookie(request, getLoginCookieKey()); 213 214 if (cookie != null) 215 { 216 String [] values = CookieUtils.decodePasswordCookie(cookie.getValue(), SecurityConfigFactory.getInstance().getCookieEncoding()); 217 218 if (values != null) 219 { 220 String username = values[0]; 221 String password = values[1]; 222 223 if (login(request, response, username, password, false)) 224 { 225 log.debug("Logged user in via a cookie"); 226 return getUser(request); 227 } 228 } 229 230 log.debug("Cannot log user in via a cookie"); 231 } 232 } 233 } 234 catch (Exception e) { 236 log.warn("Exception: " + e, e); 237 } 238 239 return user; 240 } 241 242 248 protected String getCookiePath(HttpServletRequest request) 249 { 250 String path = request.getContextPath(); 251 if (path == null || path.equals("")) 252 return "/"; 253 254 if (!path.startsWith("/")) 256 return "/" + path; 257 258 return path; 259 } 260 261 protected String getLoginCookieKey() { 262 return loginCookieKey; 263 } 264 265 protected List getLogoutInterceptors() { 266 return getConfig().getInterceptors(LogoutInterceptor.class); 267 } 268 } 269 | Popular Tags |