2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.pypdp.authorization;
23 import java.io.IOException;
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.FilterConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.annotation.WebFilter;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
36 public class AuthenticationFilter implements Filter {
38 public static final String AUTHENTICATION_HEADER = "Authorization";
39 public static final String ENVIRONMENT_HEADER = "Environment";
42 public void doFilter(ServletRequest request, ServletResponse response,
43 FilterChain filter) throws IOException, ServletException {
44 if (request instanceof HttpServletRequest) {
45 HttpServletRequest httpServletRequest = (HttpServletRequest) request;
46 String authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
47 String environment = httpServletRequest.getHeader(ENVIRONMENT_HEADER);
48 String path = ((HttpServletRequest) request).getRequestURI();
51 AuthenticationService authenticationService = new AuthenticationService();
53 boolean authenticationStatus = authenticationService.authenticate(authCredentials);
55 if (authenticationStatus && environment!=null && (environment.equalsIgnoreCase(Config.getEnvironment()))) {
56 filter.doFilter(request, response);
57 } else if(environment==null| path.contains("org.openecomp.policy.pypdp.notifications") || path.contains("swagger") || path.contains("api-docs") || path.contains("configuration") || path.contains("pdps") || path.contains("count") || path.contains("paps")){
58 filter.doFilter(request, response);
60 if (response instanceof HttpServletResponse) {
61 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
62 httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
65 if (path.contains("error")){
66 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
67 httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
73 public void destroy() {
77 public void init(FilterConfig arg0) throws ServletException {