2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.pap.xacml.restAuth;
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;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
39 * Servlet Filter implementation class PAPAuthenticationFilter
42 public class PAPAuthenticationFilter implements Filter {
44 private static final Log logger = LogFactory.getLog(PAPAuthenticationFilter.class);
45 public static final String AUTHENTICATION_HEADER = "Authorization";
48 public void doFilter(ServletRequest request, ServletResponse response,
49 FilterChain filter) throws IOException, ServletException {
52 if (request instanceof HttpServletRequest) {
53 HttpServletRequest httpServletRequest = (HttpServletRequest) request;
55 String authCredentials = null;
56 String url = httpServletRequest.getRequestURI();
58 logger.info("Request URI: " + url);
60 //getting authentication credentials
61 authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
63 // Check Authentication credentials
64 AuthenticationService authenticationService = new AuthenticationService();
65 boolean authenticationStatus = authenticationService.authenticate(authCredentials);
67 if (authenticationStatus) {
68 //indicates the request comes from Traditional Admin Console or PolicyEngineAPI
69 if ("/pap/".equals(url)){
70 logger.info("Request comes from Traditional Admin Console or PolicyEngineAPI");
71 //forward request to the XACMLPAPServlet if authenticated
72 request.getRequestDispatcher("/pap/pap/").forward(request, response);
73 }else if (url.startsWith("/pap/onap/") && response instanceof HttpServletResponse){
74 //indicates the request comes from the ONAP Portal onap-sdk-app
75 HttpServletResponse alteredResponse = ((HttpServletResponse)response);
76 addCorsHeader(alteredResponse);
77 logger.info("Request comes from Onap Portal");
78 //Spring dispatcher servlet is at the end of the filter chain at /pap/onap/ path
79 filter.doFilter(request, response);
82 if (response instanceof HttpServletResponse) {
83 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
84 httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
91 //method to add CorsHeaders for onap portal rest call
92 private void addCorsHeader(HttpServletResponse response) {
93 logger.info("Adding Cors Response Headers!!!");
94 response.addHeader("Access-Control-Allow-Origin", "*");
95 response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
96 response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
97 response.addHeader("Access-Control-Max-Age", "1728000");
101 public void destroy() {
106 public void init(FilterConfig arg0) throws ServletException {