Merge "Enhancement to use the common CryptoUtils"
[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 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
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.onap.policy.pdp.rest.config.PDPApiAuth;
36
37 /**
38  * Servlet Filter implementation class PDPAuthenticationFilter
39  */
40 @WebFilter("/*")
41 public class PDPAuthenticationFilter implements Filter {
42
43         public static final String AUTHENTICATION_HEADER = "Authorization";
44         public static final String ENVIRONMENT_HEADER = "Environment";
45
46         @Override
47         public void doFilter(ServletRequest request, ServletResponse response,
48                         FilterChain filter) throws IOException, ServletException {
49                 if (request instanceof HttpServletRequest) {
50                         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
51                         String environment = httpServletRequest.getHeader(ENVIRONMENT_HEADER);
52                         String authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
53                         String path = ((HttpServletRequest) request).getRequestURI();
54                         // better injected
55                         AuthenticationService authenticationService = new AuthenticationService();
56
57                         boolean authenticationStatus = authenticationService.authenticate(authCredentials);
58
59                         if (authenticationStatus) {
60                                 if (check(path)) {
61                     // New API request. 
62                     path = path.substring(path.substring(1).indexOf("/") + 1);
63                     if (environment == null) {
64                         // Allow Old clients.
65                         if(!path.contains("/api/")){
66                             request.getRequestDispatcher("/api/" + path).forward(request,response);
67                         }else{
68                             request.getRequestDispatcher(path).forward(request,response);
69                         }
70                     } else if (environment.equalsIgnoreCase(PDPApiAuth.getEnvironment())) {
71                         // Validated new Clients. 
72                         if(!path.contains("/api/")){
73                             request.getRequestDispatcher("/api/" + path).forward(request,response);
74                         }else{
75                             request.getRequestDispatcher(path).forward(request,response);
76                         }
77                     } else if(response instanceof HttpServletResponse) {
78                             HttpServletResponse httpServletResponse = (HttpServletResponse) response;
79                             httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
80                     }
81                 } else {
82                     filter.doFilter(request, response);
83                 }
84             } else if (path.contains("swagger") || path.contains("api-docs")
85                     || path.contains("configuration") || path.contains("count")) {
86                 path = path.substring(path.substring(1).indexOf("/") + 2);
87                 request.getRequestDispatcher("/api/" + path).forward(request,response);
88             } else if(path.contains("notifications")){
89                                 filter.doFilter(request, response);
90                         } else {
91                                 if (response instanceof HttpServletResponse) {
92                                         HttpServletResponse httpServletResponse = (HttpServletResponse) response;
93                                         httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
94                                 }
95                         }
96                 }
97         }
98         
99         private boolean check(String path) {
100         if(path.endsWith("/pdp/")|| path.endsWith("/pdp")|| path.endsWith("/test")){
101             return false;
102         }else{
103             return true;
104         }
105     }
106
107         @Override
108         public void destroy() {
109                 // Do nothing.
110         }
111
112         @Override
113         public void init(FilterConfig arg0) throws ServletException {
114                 // Do nothing.
115         }
116
117 }