2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
39 package org.onap.portalapp.portal.utils;
41 import javax.servlet.http.Cookie;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
45 import org.onap.music.eelf.logging.EELFLoggerDelegate;
46 import org.onap.portalapp.music.service.MusicService;
47 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
48 import org.springframework.security.web.csrf.CsrfToken;
49 import org.springframework.security.web.csrf.CsrfTokenRepository;
50 import org.springframework.security.web.csrf.DefaultCsrfToken;
51 import org.springframework.util.StringUtils;
54 public final class MusicCookieCsrfTokenRepository implements CsrfTokenRepository {
55 static final String CSRF_COOKIE_NAME = "XSRF-TOKEN";
56 static final String CSRF_HEADER_NAME = "X-XSRF-TOKEN";
57 static final String CSRF_PARAMETER_NAME = "_csrf";
58 static final String EP_SERVICE = "EPService";
59 CookieCsrfTokenRepository cookieRepo = null;
60 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCookieCsrfTokenRepository.class);
61 public MusicCookieCsrfTokenRepository() {
64 public MusicCookieCsrfTokenRepository(CookieCsrfTokenRepository _cookieRepo) {
66 cookieRepo = _cookieRepo;
70 public CsrfToken generateToken(HttpServletRequest request) {
71 return cookieRepo.generateToken(request) ;
75 public void saveToken(CsrfToken token, HttpServletRequest request,
76 HttpServletResponse response) {
77 logger.debug(EELFLoggerDelegate.debugLogger, "initialize save csrf token ...");
78 cookieRepo.saveToken(token, request, response);
82 public CsrfToken loadToken(HttpServletRequest request) {
83 logger.debug(EELFLoggerDelegate.debugLogger, "initialize load csrf token ...");
84 CsrfToken cookieRepoToken = cookieRepo.loadToken(request);
85 if(cookieRepoToken==null){ // if cookieRepo does not has the token, check the cassandra for the values stored by other tomcats
86 try { // todo this part of the code needs to be replaced with out depending on EPService cookie
87 String sessionId = getSessionIdFromCookie(request);
90 String token = MusicService.getAttribute(CSRF_COOKIE_NAME, sessionId);
91 if (token==null || !StringUtils.hasLength(token))
93 cookieRepoToken = new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_PARAMETER_NAME , token);
94 } catch (Exception e) {
95 logger.error(EELFLoggerDelegate.errorLogger, "Error while calling csrf loadToken" , e);
98 return cookieRepoToken;
102 * Factory method to conveniently create an instance that has
103 * {@link #setCookieHttpOnly(boolean)} set to false.
105 * @return an instance of CookieCsrfTokenRepository with
106 * {@link #setCookieHttpOnly(boolean)} set to false
108 public static MusicCookieCsrfTokenRepository withHttpOnlyFalse() {
109 CookieCsrfTokenRepository result = new CookieCsrfTokenRepository();
110 result.setCookieHttpOnly(false);
111 return new MusicCookieCsrfTokenRepository(result);
114 private String getSessionIdFromCookie (HttpServletRequest request){
115 Cookie cookies[] = request.getCookies();
116 if (cookies != null) {
117 for (Cookie cookie : cookies) {
118 if (EP_SERVICE.equals(cookie.getName())) {
119 return cookie.getValue();