1 package org.openecomp.sdc.securityutil;
3 import org.openecomp.sdc.securityutil.filters.SessionValidationFilter;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
7 import javax.servlet.http.Cookie;
8 import java.io.IOException;
10 public class AuthenticationCookieUtils {
12 private static final Logger log = LoggerFactory.getLogger(SessionValidationFilter.class.getName());
15 * Update given cookie session time value to current time
18 * @param filterConfiguration
20 * @throws CipherUtilException
23 public static Cookie updateSessionTime(Cookie cookie, ISessionValidationFilterConfiguration filterConfiguration) throws CipherUtilException, IOException {
24 AuthenticationCookie authenticationCookie = getAuthenticationCookie(cookie, filterConfiguration);
25 long newTime = System.currentTimeMillis();
26 log.debug("SessionValidationFilter: Going to set new session time in cookie, old value: {}, new value: {}", authenticationCookie.getCurrentSessionTime(), newTime);
27 authenticationCookie.setCurrentSessionTime(newTime);
28 String encryptedCookie = getEncryptedCookie(authenticationCookie, filterConfiguration);
29 return createUpdatedCookie(cookie, encryptedCookie, filterConfiguration);
33 * Create new Cookie object with same attributes as original cookie
35 * @param encryptedCookie
36 * @param cookieConfiguration
39 public static Cookie createUpdatedCookie(Cookie cookie, String encryptedCookie, ISessionValidationCookieConfiguration cookieConfiguration) {
40 Cookie updatedCookie = new Cookie(cookie.getName(), encryptedCookie );
41 updatedCookie.setPath(cookieConfiguration.getCookiePath());
42 updatedCookie.setDomain(cookieConfiguration.getCookieDomain());
43 updatedCookie.setHttpOnly(cookieConfiguration.isCookieHttpOnly());
48 * Convert AuthenticationCookie to JSON and encrypt with given key
50 * @param authenticationCookie
51 * @param filterConfiguration
54 * @throws CipherUtilException
56 public static String getEncryptedCookie(AuthenticationCookie authenticationCookie, ISessionValidationFilterConfiguration filterConfiguration) throws IOException, CipherUtilException {
57 String changedCookieJson = RepresentationUtils.toRepresentation(authenticationCookie);
58 return CipherUtil.encryptPKC(changedCookieJson, filterConfiguration.getSecurityKey());
62 * Decrypt given Cookie to JSON and convert to AuthenticationCookie object
65 * @param filterConfiguration
67 * @throws CipherUtilException
69 public static AuthenticationCookie getAuthenticationCookie(Cookie cookie, ISessionValidationFilterConfiguration filterConfiguration) throws CipherUtilException {
70 String originalCookieJson = CipherUtil.decryptPKC(cookie.getValue(), filterConfiguration.getSecurityKey());
71 return RepresentationUtils.fromRepresentation(originalCookieJson, AuthenticationCookie.class);
75 * session expired if session was idle or max time reached
78 * @param filterConfiguration
80 * @throws CipherUtilException
82 public static boolean isSessionExpired(Cookie cookie, ISessionValidationFilterConfiguration filterConfiguration) throws CipherUtilException {
83 AuthenticationCookie authenticationCookie = getAuthenticationCookie(cookie, filterConfiguration);
84 long sessionExpirationDate = authenticationCookie.getMaxSessionTime() + filterConfiguration.getMaxSessionTimeOut();
85 long sessionTime = authenticationCookie.getCurrentSessionTime();
86 long currentTime = System.currentTimeMillis();
87 log.debug("SessionValidationFilter: Checking if session expired: session time: {}, expiration time: {}, current time: {}", sessionTime, sessionExpirationDate, currentTime);
88 return currentTime > sessionExpirationDate || isSessionIdle(sessionTime, currentTime, filterConfiguration);
92 * Session is idle if wasn't updated ( wasn't in use ) for more then value from filter configuration
94 * @param sessionTimeValue
96 * @param filterConfiguration
99 public static boolean isSessionIdle(long sessionTimeValue, long currentTime, ISessionValidationFilterConfiguration filterConfiguration) {
100 long currentIdleTime = currentTime - sessionTimeValue;
101 long maxIdleTime = filterConfiguration.getSessionIdleTimeOut();
102 log.debug("SessionValidationFilter: Checking if session idle: session time: {}, current idle time: {}, max idle time: {}", currentTime, currentIdleTime, maxIdleTime);
103 return currentIdleTime >= maxIdleTime;