f0a33da8a72b9b6ed13af979f85659ce2ab64f78
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.securityutil;
22
23 import org.openecomp.sdc.securityutil.filters.SessionValidationFilter;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import javax.servlet.http.Cookie;
28 import java.io.IOException;
29
30 public class AuthenticationCookieUtils {
31
32     private static final Logger log = LoggerFactory.getLogger(SessionValidationFilter.class.getName());
33
34     private AuthenticationCookieUtils() {
35     }
36
37     /**
38      * Update given cookie session time value to current time
39      *
40      * @param cookie
41      * @param filterConfiguration
42      * @return
43      * @throws CipherUtilException
44      * @throws IOException
45      */
46     public static Cookie updateSessionTime(Cookie cookie, ISessionValidationFilterConfiguration filterConfiguration) throws CipherUtilException, IOException {
47         AuthenticationCookie authenticationCookie = getAuthenticationCookie(cookie, filterConfiguration);
48         long newTime = System.currentTimeMillis();
49         log.debug("SessionValidationFilter: Going to set new session time in cookie, old value: {}, new value: {}", authenticationCookie.getCurrentSessionTime(), newTime);
50         authenticationCookie.setCurrentSessionTime(newTime);
51         String encryptedCookie = getEncryptedCookie(authenticationCookie, filterConfiguration);
52         return createUpdatedCookie(cookie, encryptedCookie, filterConfiguration);
53     }
54
55     /**
56      * Create new Cookie object with same attributes as original cookie
57      * @param cookie
58      * @param encryptedCookie
59      * @param cookieConfiguration
60      * @return
61      */
62     public static Cookie createUpdatedCookie(Cookie cookie, String encryptedCookie, ISessionValidationCookieConfiguration cookieConfiguration) {
63         Cookie updatedCookie = new Cookie(cookie.getName(), encryptedCookie );
64         updatedCookie.setSecure(true);
65         updatedCookie.setPath(cookieConfiguration.getCookiePath());
66         updatedCookie.setDomain(cookieConfiguration.getCookieDomain());
67         updatedCookie.setHttpOnly(cookieConfiguration.isCookieHttpOnly());
68         return updatedCookie;
69     }
70
71     /**
72      * Convert AuthenticationCookie to JSON and encrypt with given key
73      *
74      * @param authenticationCookie
75      * @param filterConfiguration
76      * @return
77      * @throws IOException
78      * @throws CipherUtilException
79      */
80     public static String getEncryptedCookie(AuthenticationCookie authenticationCookie, ISessionValidationFilterConfiguration filterConfiguration) throws IOException, CipherUtilException {
81         String changedCookieJson = RepresentationUtils.toRepresentation(authenticationCookie);
82         return CipherUtil.encryptPKC(changedCookieJson, filterConfiguration.getSecurityKey());
83     }
84
85     /**
86      * Decrypt given Cookie to JSON and convert to AuthenticationCookie object
87      *
88      * @param cookie
89      * @param filterConfiguration
90      * @return
91      * @throws CipherUtilException
92      */
93     public static AuthenticationCookie getAuthenticationCookie(Cookie cookie, ISessionValidationFilterConfiguration filterConfiguration) throws CipherUtilException {
94         String originalCookieJson = CipherUtil.decryptPKC(cookie.getValue(), filterConfiguration.getSecurityKey());
95         return RepresentationUtils.fromRepresentation(originalCookieJson, AuthenticationCookie.class);
96     }
97
98     /**
99      * session expired if session was idle or max time reached
100      *
101      * @param cookie
102      * @param filterConfiguration
103      * @return
104      * @throws CipherUtilException
105      */
106     public static boolean isSessionExpired(Cookie cookie, ISessionValidationFilterConfiguration filterConfiguration) throws CipherUtilException {
107         AuthenticationCookie authenticationCookie = getAuthenticationCookie(cookie, filterConfiguration);
108         long sessionExpirationDate = authenticationCookie.getMaxSessionTime() + filterConfiguration.getMaxSessionTimeOut();
109         long sessionTime = authenticationCookie.getCurrentSessionTime();
110         long currentTime = System.currentTimeMillis();
111         log.debug("SessionValidationFilter: Checking if session expired: session time: {}, expiration time: {}, current time: {}", sessionTime, sessionExpirationDate, currentTime);
112         return currentTime > sessionExpirationDate || isSessionIdle(sessionTime, currentTime, filterConfiguration);
113     }
114
115     /**
116      * Session is idle if wasn't updated ( wasn't in use ) for more then value from filter configuration
117      *
118      * @param sessionTimeValue
119      * @param currentTime
120      * @param filterConfiguration
121      * @return
122      */
123     private static boolean isSessionIdle(long sessionTimeValue, long currentTime, ISessionValidationFilterConfiguration filterConfiguration) {
124         long currentIdleTime = currentTime - sessionTimeValue;
125         long maxIdleTime = filterConfiguration.getSessionIdleTimeOut();
126         log.debug("SessionValidationFilter: Checking if session idle: session time: {}, current idle time: {}, max idle time: {}", currentTime, currentIdleTime, maxIdleTime);
127         return currentIdleTime >= maxIdleTime;
128     }
129
130 }