Catalog alignment
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / filters / SecurityFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.fe.filters;
22
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
27
28 import javax.servlet.Filter;
29 import javax.servlet.FilterChain;
30 import javax.servlet.FilterConfig;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.http.Cookie;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.NoSuchElementException;
41
42 public class SecurityFilter implements Filter {
43     private static final Logger log = Logger.getLogger(SecurityFilter.class.getName());
44
45     private static final String PORTAL_COOKIE_NAME_IS_NOT_SET = "Portal cookie name is not set in portal.properties file";
46
47     private List<String> excludedUrls;
48
49     static final String PORTAL_COOKIE_NAME_KEY = "portal_cookie_name";
50     static final String PORTAL_REDIRECT_URL_KEY = "ecomp_redirect_url";
51     static final String FILTER_EXLUDED_URLS_KEY ="excludedUrls";
52
53     @Override
54     public void init(FilterConfig filterConfig) throws ServletException {
55         excludedUrls = Arrays.asList(filterConfig.getInitParameter(FILTER_EXLUDED_URLS_KEY).split(","));
56     }
57
58     @Override
59     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
60
61         final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
62         final HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
63
64         log.debug("SecurityFilter received request with URL {}", httpRequest.getRequestURL());
65         //add redirecting to Portal if cookie is not provided
66         if (!excludedUrls.contains(httpRequest.getServletPath()) && !isRequestFromPortal(httpRequest.getCookies())) {
67             //redirect to portal app
68             log.debug("Request {} is not from Portal, redirecting there", httpRequest.getServletPath());
69             httpResponse.sendRedirect(PortalApiProperties.getProperty(PORTAL_REDIRECT_URL_KEY));
70         }
71         else {
72             filterChain.doFilter(servletRequest, servletResponse);
73         }
74     }
75
76     @Override
77     public void destroy() {
78
79     }
80
81     private boolean isRequestFromPortal(Cookie[] cookies) {
82         String portalCookieValue = PortalApiProperties.getProperty(PORTAL_COOKIE_NAME_KEY);
83         if (StringUtils.isEmpty(portalCookieValue)) {
84             log.error(PORTAL_COOKIE_NAME_IS_NOT_SET);
85             throw new NoSuchElementException(PORTAL_COOKIE_NAME_IS_NOT_SET);
86         }
87         return cookies != null && Arrays.stream(cookies)
88                 .anyMatch(c->StringUtils.equals(c.getName(), portalCookieValue));
89     }
90 }