Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / authorization / AuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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.openecomp.policy.pypdp.authorization;
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 @WebFilter("/*")
36 public class AuthenticationFilter implements Filter {
37
38         public static final String AUTHENTICATION_HEADER = "Authorization";
39         public static final String ENVIRONMENT_HEADER = "Environment";
40
41         @Override
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();
49
50                         // better injected
51                         AuthenticationService authenticationService = new AuthenticationService();
52
53                         boolean authenticationStatus = authenticationService.authenticate(authCredentials);
54                         
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);
59                         } else {
60                                 if (response instanceof HttpServletResponse) {
61                                         HttpServletResponse httpServletResponse = (HttpServletResponse) response;
62                                         httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
63                                 }
64                         }
65                         if (path.contains("error")){
66                                 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
67                                 httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
68                         }
69                 }
70         }
71
72         @Override
73         public void destroy() {
74         }
75
76         @Override
77         public void init(FilterConfig arg0) throws ServletException {
78                 Config.setProperty();
79         }
80 }