Update css file name in conf.py
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / restAuth / PAPAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-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.pap.xacml.restAuth;
22
23 import java.io.IOException;
24
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;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * Servlet Filter implementation class PAPAuthenticationFilter
40  */
41 @WebFilter("/*")
42 public class PAPAuthenticationFilter implements Filter {
43
44     private static final Log logger = LogFactory.getLog(PAPAuthenticationFilter.class);
45     public static final String AUTHENTICATION_HEADER = "Authorization";
46
47     @Override
48     public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
49             throws IOException, ServletException {
50
51         if (request instanceof HttpServletRequest) {
52             HttpServletRequest httpServletRequest = (HttpServletRequest) request;
53
54             String authCredentials = null;
55             String url = httpServletRequest.getRequestURI();
56
57             logger.info("Request URI: " + url);
58
59             // getting authentication credentials
60             authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
61
62             // Check Authentication credentials
63             AuthenticationService authenticationService = new AuthenticationService();
64             boolean authenticationStatus = authenticationService.authenticate(authCredentials);
65
66             if (authenticationStatus) {
67                 // indicates the request comes from Traditional Admin Console or PolicyEngineAPI
68                 if ("/pap/".equals(url)) {
69                     logger.info("Request comes from Traditional Admin Console or PolicyEngineAPI");
70                     // forward request to the XACMLPAPServlet if authenticated
71                     request.getRequestDispatcher("/pap/pap/").forward(request, response);
72                 } else if (url.startsWith("/pap/onap/") && response instanceof HttpServletResponse) {
73                     // indicates the request comes from the ONAP Portal onap-sdk-app
74                     HttpServletResponse alteredResponse = ((HttpServletResponse) response);
75                     addCorsHeader(alteredResponse);
76                     logger.info("Request comes from Onap Portal");
77                     // Spring dispatcher servlet is at the end of the filter chain at /pap/onap/ path
78                     filter.doFilter(request, response);
79                 }
80             } else {
81                 if (response instanceof HttpServletResponse) {
82                     HttpServletResponse httpServletResponse = (HttpServletResponse) response;
83                     httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
84                 }
85             }
86
87         }
88     }
89
90     // method to add CorsHeaders for onap portal rest call
91     private void addCorsHeader(HttpServletResponse response) {
92         logger.info("Adding Cors Response Headers!!!");
93         response.addHeader("Access-Control-Allow-Origin", "*");
94         response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
95         response.addHeader("Access-Control-Allow-Headers",
96                 "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
97         response.addHeader("Access-Control-Max-Age", "1728000");
98     }
99
100     @Override
101     public void destroy() {
102         // Empty
103     }
104
105     @Override
106     public void init(FilterConfig arg0) throws ServletException {
107         // Empty
108     }
109 }