Make clientAuth header optional and log request
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / restauth / PdpAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017,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.onap.policy.pdp.rest.restauth;
22
23 import java.io.IOException;
24 import javax.servlet.Filter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.annotation.WebFilter;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 /**
35  * Servlet Filter implementation class PdpAuthenticationFilter.
36  */
37 @WebFilter("/*")
38 public class PdpAuthenticationFilter implements Filter {
39     private static final String APISTR = "/api/";
40     public static final String AUTHENTICATION_HEADER = "Authorization";
41     public static final String ENVIRONMENT_HEADER = "Environment";
42     public static final String CLIENTAUTH_HEADER = "ClientAuth";
43
44     @Override
45     public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
46             throws IOException, ServletException {
47         if (!(request instanceof HttpServletRequest)) {
48             return;
49         }
50         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
51         String environment = httpServletRequest.getHeader(ENVIRONMENT_HEADER);
52         String authHeader = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
53         String clientAuthHeader = httpServletRequest.getHeader(CLIENTAUTH_HEADER);
54         String path = ((HttpServletRequest) request).getRequestURI();
55         String resource = path.substring(path.lastIndexOf('/') + 1);
56
57         boolean authenticationStatus =
58                 AuthenticationService.checkPermissions(clientAuthHeader, authHeader, resource, environment, request);
59
60         if (authenticationStatus) {
61             if (check(path)) {
62                 path = path.substring(path.indexOf('/', 1));
63                 if (!path.contains(APISTR)) {
64                     request.getRequestDispatcher(APISTR + path).forward(request, response);
65                 } else {
66                     request.getRequestDispatcher(path).forward(request, response);
67                 }
68
69             } else {
70                 filter.doFilter(request, response);
71             }
72         } else if (path.contains("swagger") || path.contains("api-docs") || path.contains("configuration")
73                 || path.contains("count")) {
74             path = path.substring(path.indexOf('/', 1) + 1);
75             request.getRequestDispatcher(APISTR + path).forward(request, response);
76         } else if (path.contains("notifications")) {
77             filter.doFilter(request, response);
78         } else {
79             if (response instanceof HttpServletResponse) {
80                 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
81                 httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
82             }
83         }
84     }
85
86     private boolean check(String path) {
87         return !(path.endsWith("/pdp/") || path.endsWith("/pdp") || path.endsWith("/test"));
88     }
89
90     @Override
91     public void destroy() {
92         // Do nothing.
93     }
94
95     @Override
96     public void init(FilterConfig arg0) throws ServletException {
97         // Do nothing.
98     }
99
100 }