2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.securityutil;
23 import org.openecomp.sdc.securityutil.filters.SessionValidationFilter;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import javax.servlet.http.Cookie;
28 import java.io.IOException;
30 public class AuthenticationCookieUtils {
32 private static final Logger log = LoggerFactory.getLogger(SessionValidationFilter.class.getName());
34 private AuthenticationCookieUtils() {
38 * Update given cookie session time value to current time
41 * @param filterConfiguration
43 * @throws CipherUtilException
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);
56 * Create new Cookie object with same attributes as original cookie
58 * @param encryptedCookie
59 * @param cookieConfiguration
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());
72 * Convert AuthenticationCookie to JSON and encrypt with given key
74 * @param authenticationCookie
75 * @param filterConfiguration
78 * @throws CipherUtilException
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());
86 * Decrypt given Cookie to JSON and convert to AuthenticationCookie object
89 * @param filterConfiguration
91 * @throws CipherUtilException
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);
99 * session expired if session was idle or max time reached
102 * @param filterConfiguration
104 * @throws CipherUtilException
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);
116 * Session is idle if wasn't updated ( wasn't in use ) for more then value from filter configuration
118 * @param sessionTimeValue
120 * @param filterConfiguration
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;