| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package org.itracker.web.util; |
| 20 |
|
|
| 21 |
|
import java.util.Enumeration; |
| 22 |
|
import java.util.Locale; |
| 23 |
|
import java.util.Map; |
| 24 |
|
import java.util.ResourceBundle; |
| 25 |
|
import java.util.Set; |
| 26 |
|
|
| 27 |
|
import javax.servlet.http.Cookie; |
| 28 |
|
import javax.servlet.http.HttpServletRequest; |
| 29 |
|
import javax.servlet.http.HttpServletResponse; |
| 30 |
|
import javax.servlet.http.HttpSession; |
| 31 |
|
|
| 32 |
|
import org.apache.log4j.Logger; |
| 33 |
|
import org.itracker.core.resources.ITrackerResources; |
| 34 |
|
import org.itracker.model.PermissionType; |
| 35 |
|
import org.itracker.model.User; |
| 36 |
|
import org.itracker.model.UserPreferences; |
| 37 |
|
import org.itracker.services.UserService; |
| 38 |
|
import org.itracker.services.util.AuthenticationConstants; |
| 39 |
|
|
|
|
|
| 0% |
Uncovered Elements: 239 (239) |
Complexity: 53 |
Complexity Density: 0.38 |
|
| 40 |
|
public class LoginUtilities { |
| 41 |
|
|
| 42 |
|
private static final Logger logger = Logger.getLogger(LoginUtilities.class); |
| 43 |
|
private static final int DEFAULT_SESSION_TIMEOUT = 30; |
| 44 |
|
|
|
|
|
| 0% |
Uncovered Elements: 51 (51) |
Complexity: 11 |
Complexity Density: 0.38 |
|
| 45 |
0
|
public static boolean checkAutoLogin(HttpServletRequest request,... |
| 46 |
|
boolean allowSaveLogin) { |
| 47 |
0
|
boolean foundLogin = false; |
| 48 |
|
|
| 49 |
0
|
if (request != null) { |
| 50 |
0
|
int authType = getRequestAuthType(request); |
| 51 |
|
|
| 52 |
|
|
| 53 |
0
|
if (!foundLogin) { |
| 54 |
0
|
if (authType == AuthenticationConstants.AUTH_TYPE_REQUEST) { |
| 55 |
0
|
String redirectURL = request.getRequestURI().substring( |
| 56 |
|
request.getContextPath().length()) |
| 57 |
0
|
+ (request.getQueryString() != null ? "?" |
| 58 |
|
+ request.getQueryString() : ""); |
| 59 |
0
|
request.setAttribute(Constants.AUTH_TYPE_KEY, |
| 60 |
|
AuthenticationConstants.AUTH_TYPE_REQUEST); |
| 61 |
0
|
request.setAttribute(Constants.AUTH_REDIRECT_KEY, |
| 62 |
|
redirectURL); |
| 63 |
0
|
request.setAttribute("processLogin", "true"); |
| 64 |
0
|
foundLogin = true; |
| 65 |
|
} |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
0
|
if (allowSaveLogin && !foundLogin) { |
| 74 |
0
|
Cookie[] cookies = request.getCookies(); |
| 75 |
0
|
if (cookies != null) { |
| 76 |
0
|
for (int i = 0; i < cookies.length; i++) { |
| 77 |
0
|
if (Constants.COOKIE_NAME.equals(cookies[i].getName())) { |
| 78 |
0
|
int seperator = cookies[i].getValue().indexOf('~'); |
| 79 |
0
|
final String login; |
| 80 |
0
|
if (seperator > 0) { |
| 81 |
0
|
login = cookies[i].getValue() |
| 82 |
|
.substring(0, |
| 83 |
|
seperator); |
| 84 |
0
|
if (logger.isDebugEnabled()) { |
| 85 |
0
|
logger |
| 86 |
|
.debug("Attempting autologin for user " |
| 87 |
|
+ login |
| 88 |
|
+ "."); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
0
|
String redirectURL = request.getRequestURI() |
| 92 |
|
.substring( |
| 93 |
|
request.getContextPath() |
| 94 |
|
.length()) |
| 95 |
0
|
+ (request.getQueryString() != null ? "?" |
| 96 |
|
+ request.getQueryString() |
| 97 |
|
: ""); |
| 98 |
0
|
request.setAttribute(Constants.AUTH_LOGIN_KEY, |
| 99 |
|
cookies[i].getValue().substring(0, |
| 100 |
|
seperator)); |
| 101 |
0
|
request.setAttribute(Constants.AUTH_TYPE_KEY, |
| 102 |
|
AuthenticationConstants.AUTH_TYPE_PASSWORD_ENC); |
| 103 |
|
|
| 104 |
0
|
request.setAttribute(Constants.AUTH_VALUE_KEY, |
| 105 |
|
cookies[i].getValue().substring( |
| 106 |
|
seperator + 1)); |
| 107 |
0
|
request.setAttribute( |
| 108 |
|
Constants.AUTH_REDIRECT_KEY, |
| 109 |
|
redirectURL); |
| 110 |
0
|
request.setAttribute("processLogin", "true"); |
| 111 |
0
|
foundLogin = true; |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
} |
| 119 |
|
|
| 120 |
0
|
return foundLogin; |
| 121 |
|
} |
| 122 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 123 |
0
|
public static int getRequestAuthType(HttpServletRequest request) {... |
| 124 |
0
|
int authType = AuthenticationConstants.AUTH_TYPE_UNKNOWN; |
| 125 |
|
|
| 126 |
0
|
try { |
| 127 |
0
|
if (request.getAttribute(Constants.AUTH_TYPE_KEY) != null) { |
| 128 |
0
|
authType = ((Integer) request |
| 129 |
|
.getAttribute(Constants.AUTH_TYPE_KEY)).intValue(); |
| 130 |
|
} |
| 131 |
0
|
if (request.getParameter(Constants.AUTH_TYPE_KEY) != null) { |
| 132 |
0
|
authType = Integer.valueOf(request |
| 133 |
|
.getParameter(Constants.AUTH_TYPE_KEY)); |
| 134 |
|
} |
| 135 |
|
} catch (Exception e) { |
| 136 |
0
|
logger |
| 137 |
|
.debug("Error retrieving auth type while checking auto login. " |
| 138 |
|
+ e.getMessage()); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
0
|
return authType; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
@param |
| 160 |
|
@return |
| 161 |
|
|
|
|
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 11 |
Complexity Density: 0.38 |
|
| 162 |
0
|
@SuppressWarnings("unchecked")... |
| 163 |
|
public static Locale getCurrentLocale(HttpServletRequest request) { |
| 164 |
0
|
Locale requestLocale = null; |
| 165 |
0
|
HttpSession session = request.getSession(true); |
| 166 |
0
|
try { |
| 167 |
|
|
| 168 |
0
|
requestLocale = (Locale) request.getAttribute(Constants.LOCALE_KEY); |
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
0
|
if (null == requestLocale) { |
| 176 |
|
|
| 177 |
0
|
String loc = request |
| 178 |
|
.getParameter("loc"); |
| 179 |
0
|
if (null != loc && loc.trim().length() > 1) { |
| 180 |
0
|
requestLocale = ITrackerResources.getLocale(loc); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
} |
| 187 |
|
|
| 188 |
0
|
if (null == requestLocale) { |
| 189 |
|
|
| 190 |
0
|
requestLocale = (Locale) session |
| 191 |
|
.getAttribute(Constants.LOCALE_KEY); |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
} |
| 197 |
|
|
| 198 |
0
|
if (null == requestLocale) { |
| 199 |
0
|
ResourceBundle bundle = ITrackerResources.getBundle(request |
| 200 |
|
.getLocale()); |
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
0
|
if (bundle.getLocale().getLanguage().equals( |
| 207 |
|
request.getLocale().getLanguage())) { |
| 208 |
0
|
requestLocale = request.getLocale(); |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
} |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
0
|
if (null == requestLocale) { |
| 220 |
0
|
Enumeration<Locale> locales = (Enumeration<Locale>) request.getLocales(); |
| 221 |
0
|
ResourceBundle bundle; |
| 222 |
0
|
Locale locale; |
| 223 |
0
|
while (locales.hasMoreElements()) { |
| 224 |
0
|
locale = (Locale) locales.nextElement(); |
| 225 |
0
|
bundle = ITrackerResources.getBundle(locale); |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
0
|
if (bundle.getLocale().getLanguage().equals( |
| 232 |
|
locale.getLanguage())) { |
| 233 |
0
|
requestLocale = locale; |
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
} |
| 240 |
|
} |
| 241 |
|
} |
| 242 |
|
|
| 243 |
|
} finally { |
| 244 |
0
|
if (null == requestLocale) { |
| 245 |
|
|
| 246 |
0
|
requestLocale = ITrackerResources.getLocale(); |
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
} |
| 253 |
0
|
session.setAttribute(Constants.LOCALE_KEY, requestLocale); |
| 254 |
0
|
request.setAttribute(Constants.LOCALE_KEY, requestLocale); |
| 255 |
0
|
request.setAttribute("currLocale", requestLocale); |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
} |
| 262 |
|
|
| 263 |
0
|
return requestLocale; |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
@param |
| 270 |
|
@return |
| 271 |
|
@throws |
| 272 |
|
|
| 273 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 274 |
0
|
public static final User getCurrentUser(HttpServletRequest request) {... |
| 275 |
|
|
| 276 |
0
|
User currUser = (User) request.getAttribute("currUser"); |
| 277 |
0
|
if (null == currUser) { |
| 278 |
0
|
currUser = (User) request.getSession().getAttribute("currUser"); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
|
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
0
|
return currUser; |
| 303 |
|
} |
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
|
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
|
| 313 |
|
|
| 314 |
|
|
| 315 |
|
|
| 316 |
|
|
| 317 |
|
|
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
|
| 324 |
|
|
| 325 |
|
|
| 326 |
|
|
| 327 |
|
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 331 |
0
|
public static final Boolean allowSaveLogin(HttpServletRequest request) {... |
| 332 |
0
|
return Boolean.valueOf((String)request.getAttribute("allowSaveLogin")); |
| 333 |
|
} |
| 334 |
|
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 7 |
Complexity Density: 0.44 |
|
| 335 |
0
|
public static User setupSession(String login, HttpServletRequest request,... |
| 336 |
|
HttpServletResponse response) { |
| 337 |
0
|
if (null == login) { |
| 338 |
0
|
logger.warn("setupSession: null login", (logger.isDebugEnabled()? new RuntimeException(): null)); |
| 339 |
0
|
throw new IllegalArgumentException("null login"); |
| 340 |
|
} |
| 341 |
0
|
UserService userService = ServletContextUtils.getItrackerServices().getUserService(); |
| 342 |
0
|
User user = userService.getUserByLogin(login); |
| 343 |
0
|
if (user != null) { |
| 344 |
0
|
String encPassword = null; |
| 345 |
0
|
Cookie[] cookies = request.getCookies(); |
| 346 |
0
|
if (cookies != null) { |
| 347 |
0
|
for (int i = 0; i < cookies.length; i++) { |
| 348 |
0
|
if (Constants.COOKIE_NAME.equals(cookies[i].getName())) { |
| 349 |
0
|
int seperator = cookies[i].getValue().indexOf('~'); |
| 350 |
0
|
if (seperator > 0) { |
| 351 |
0
|
encPassword = cookies[i].getValue().substring( |
| 352 |
|
seperator + 1); |
| 353 |
|
} |
| 354 |
|
} |
| 355 |
|
} |
| 356 |
|
} |
| 357 |
|
|
| 358 |
0
|
return setupSession(user, encPassword, request, response); |
| 359 |
|
} |
| 360 |
0
|
return null; |
| 361 |
|
} |
| 362 |
|
|
|
|
|
| 0% |
Uncovered Elements: 83 (83) |
Complexity: 16 |
Complexity Density: 0.31 |
|
| 363 |
0
|
public static User setupSession(User user, String encPassword,... |
| 364 |
|
HttpServletRequest request, HttpServletResponse response) { |
| 365 |
0
|
if (user == null) { |
| 366 |
0
|
logger.warn("setupSession: null user", (logger.isDebugEnabled()? new RuntimeException(): null)); |
| 367 |
0
|
throw new IllegalArgumentException("null user"); |
| 368 |
|
} |
| 369 |
|
|
| 370 |
0
|
UserService userService = ServletContextUtils.getItrackerServices().getUserService(); |
| 371 |
|
|
| 372 |
0
|
if (logger.isDebugEnabled()) { |
| 373 |
0
|
logger.debug("Creating new session"); |
| 374 |
|
} |
| 375 |
0
|
HttpSession session = request.getSession(true); |
| 376 |
|
|
| 377 |
0
|
if (logger.isDebugEnabled()) { |
| 378 |
0
|
logger.debug("Setting session timeout to " |
| 379 |
|
+ getConfiguredSessionTimeout() + " minutes"); |
| 380 |
|
} |
| 381 |
0
|
session.setMaxInactiveInterval(getConfiguredSessionTimeout() * 60); |
| 382 |
|
|
| 383 |
0
|
if (logger.isDebugEnabled()) { |
| 384 |
0
|
logger.debug("Setting session tracker"); |
| 385 |
|
} |
| 386 |
0
|
session.setAttribute(Constants.SESSION_TRACKER_KEY, new SessionTracker( |
| 387 |
|
user.getLogin(), session.getId())); |
| 388 |
|
|
| 389 |
0
|
if (logger.isDebugEnabled()) { |
| 390 |
0
|
logger.debug("Setting user information"); |
| 391 |
|
} |
| 392 |
0
|
session.setAttribute(Constants.USER_KEY, user); |
| 393 |
|
|
| 394 |
0
|
if (logger.isDebugEnabled()) { |
| 395 |
0
|
logger.debug("Setting preferences for user " + user.getLogin()); |
| 396 |
|
} |
| 397 |
0
|
UserPreferences userPrefs = user.getPreferences(); |
| 398 |
|
|
| 399 |
0
|
if (userPrefs == null) { |
| 400 |
0
|
logger.warn("setupSession: got user with no preferences!: " + user + " (prefs: " + user.getPreferences() + ")"); |
| 401 |
0
|
userPrefs = new UserPreferences(); |
| 402 |
|
} |
| 403 |
0
|
session.setAttribute(Constants.PREFERENCES_KEY, userPrefs); |
| 404 |
|
|
| 405 |
0
|
if (logger.isDebugEnabled()) { |
| 406 |
0
|
logger.debug("Setting user " + user + " locale to "+ ITrackerResources |
| 407 |
|
.getLocale(userPrefs.getUserLocale())); |
| 408 |
|
} |
| 409 |
0
|
session.setAttribute(Constants.LOCALE_KEY, ITrackerResources |
| 410 |
|
.getLocale(userPrefs.getUserLocale())); |
| 411 |
|
|
| 412 |
0
|
if (logger.isDebugEnabled()) { |
| 413 |
0
|
logger.debug("Setting autologin cookie for user " + user.getLogin()); |
| 414 |
|
} |
| 415 |
0
|
Cookie cookie = new Cookie(Constants.COOKIE_NAME, ""); |
| 416 |
0
|
cookie.setPath(request.getContextPath()); |
| 417 |
0
|
if (userPrefs.getSaveLogin()) { |
| 418 |
0
|
if (encPassword != null) { |
| 419 |
0
|
if (logger.isDebugEnabled()) { |
| 420 |
0
|
logger.debug("User allows autologin"); |
| 421 |
|
} |
| 422 |
0
|
cookie.setComment("ITracker autologin cookie"); |
| 423 |
0
|
cookie.setValue(user.getLogin() + "~" + encPassword); |
| 424 |
0
|
cookie.setMaxAge(30 * 24 * 60 * 60); |
| 425 |
|
} |
| 426 |
|
} else { |
| 427 |
0
|
if (logger.isDebugEnabled()) { |
| 428 |
0
|
logger.debug("User does not allow autologin"); |
| 429 |
|
} |
| 430 |
0
|
cookie.setValue(""); |
| 431 |
0
|
cookie.setMaxAge(0); |
| 432 |
|
} |
| 433 |
0
|
response.addCookie(cookie); |
| 434 |
|
|
| 435 |
0
|
if (logger.isDebugEnabled()) { |
| 436 |
0
|
logger.debug("Setting permissions for user " + user.getLogin()); |
| 437 |
|
} |
| 438 |
0
|
Map<Integer, Set<PermissionType>> usersMapOfProjectIdsAndSetOfPermissionTypes = userService |
| 439 |
|
.getUsersMapOfProjectIdsAndSetOfPermissionTypes(user, |
| 440 |
|
AuthenticationConstants.REQ_SOURCE_WEB); |
| 441 |
0
|
session.setAttribute(Constants.PERMISSIONS_KEY, |
| 442 |
|
usersMapOfProjectIdsAndSetOfPermissionTypes); |
| 443 |
|
|
| 444 |
|
|
| 445 |
0
|
session.setAttribute(Constants.SEARCH_QUERY_KEY, null); |
| 446 |
|
|
| 447 |
0
|
SessionManager.clearSessionNeedsReset(user.getLogin()); |
| 448 |
0
|
if (logger.isDebugEnabled()) { |
| 449 |
0
|
logger.debug("User session data updated."); |
| 450 |
|
} |
| 451 |
0
|
return user; |
| 452 |
|
} |
| 453 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 454 |
0
|
public static int getConfiguredSessionTimeout() {... |
| 455 |
0
|
return (ServletContextUtils.getItrackerServices().getConfigurationService() |
| 456 |
|
.getIntegerProperty("web_session_timeout", DEFAULT_SESSION_TIMEOUT)); |
| 457 |
|
} |
| 458 |
|
} |