2 * ============LICENSE_START=======================================================
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
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.onap.policy.pdp.rest.restauth;
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;
35 * Servlet Filter implementation class PdpAuthenticationFilter.
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";
45 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
46 throws IOException, ServletException {
47 if (!(request instanceof HttpServletRequest)) {
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);
57 boolean authenticationStatus =
58 AuthenticationService.checkPermissions(clientAuthHeader, authHeader, resource, environment, request);
60 if (authenticationStatus) {
62 path = path.substring(path.indexOf('/', 1));
63 if (!path.contains(APISTR)) {
64 request.getRequestDispatcher(APISTR + path).forward(request, response);
66 request.getRequestDispatcher(path).forward(request, response);
70 filter.doFilter(request, response);
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);
79 if (response instanceof HttpServletResponse) {
80 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
81 httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
86 private boolean check(String path) {
87 return !(path.endsWith("/pdp/") || path.endsWith("/pdp") || path.endsWith("/test"));
91 public void destroy() {
96 public void init(FilterConfig arg0) throws ServletException {