login and Certman AAF Integration changes
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / utils / MusicCookieCsrfTokenRepository.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 package org.onap.portalapp.portal.utils;
40
41 import javax.servlet.http.Cookie;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
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;
52
53
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() {
62         }
63         
64         public MusicCookieCsrfTokenRepository(CookieCsrfTokenRepository _cookieRepo) {
65                 this();
66                 cookieRepo = _cookieRepo;
67         }
68
69         @Override
70         public CsrfToken generateToken(HttpServletRequest request) {
71                 return cookieRepo.generateToken(request) ;
72         }
73
74         @Override
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);
79         }
80
81         @Override
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);
88                                 if(sessionId == null) 
89                                         return null;
90                                 String token = MusicService.getAttribute(CSRF_COOKIE_NAME, sessionId);
91                                 if (token==null || !StringUtils.hasLength(token)) 
92                                         return null;
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);
96                         }
97                 }
98                 return cookieRepoToken;
99         }
100
101         /**
102          * Factory method to conveniently create an instance that has
103          * {@link #setCookieHttpOnly(boolean)} set to false.
104          *
105          * @return an instance of CookieCsrfTokenRepository with
106          * {@link #setCookieHttpOnly(boolean)} set to false
107          */
108         public static MusicCookieCsrfTokenRepository withHttpOnlyFalse() {
109                 CookieCsrfTokenRepository result = new CookieCsrfTokenRepository();
110                 result.setCookieHttpOnly(false);
111                 return new MusicCookieCsrfTokenRepository(result);
112         }
113         
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();
120                                 }
121                         }
122                 }
123                 return null;
124         }
125 }