24225a86f95dfbd9ad27581f7cdc9cdc0274c9e7
[sdc.git] /
1 package org.openecomp.sdc.securityutil;
2
3 import org.openecomp.sdc.securityutil.filters.SessionValidationFilter;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import javax.servlet.http.Cookie;
8 import java.io.IOException;
9
10 public class AuthenticationCookieUtils {
11
12     private static final Logger log = LoggerFactory.getLogger(SessionValidationFilter.class.getName());
13
14     /**
15      * Update given cookie session time value to current time
16      *
17      * @param cookie
18      * @param filterConfiguration
19      * @return
20      * @throws CipherUtilException
21      * @throws IOException
22      */
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);
30     }
31
32     /**
33      * Create new Cookie object with same attributes as original cookie
34      * @param cookie
35      * @param encryptedCookie
36      * @param cookieConfiguration
37      * @return
38      */
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());
44         return updatedCookie;
45     }
46
47     /**
48      * Convert AuthenticationCookie to JSON and encrypt with given key
49      *
50      * @param authenticationCookie
51      * @param filterConfiguration
52      * @return
53      * @throws IOException
54      * @throws CipherUtilException
55      */
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());
59     }
60
61     /**
62      * Decrypt given Cookie to JSON and convert to AuthenticationCookie object
63      *
64      * @param cookie
65      * @param filterConfiguration
66      * @return
67      * @throws CipherUtilException
68      */
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);
72     }
73
74     /**
75      * session expired if session was idle or max time reached
76      *
77      * @param cookie
78      * @param filterConfiguration
79      * @return
80      * @throws CipherUtilException
81      */
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);
89     }
90
91     /**
92      * Session is idle if wasn't updated ( wasn't in use ) for more then value from filter configuration
93      *
94      * @param sessionTimeValue
95      * @param currentTime
96      * @param filterConfiguration
97      * @return
98      */
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;
104     }
105
106
107 }