Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / restAuth / PDPAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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.openecomp.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.openecomp.policy.pdp.rest.restAuth.AuthenticationService;
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
45         @Override
46         public void doFilter(ServletRequest request, ServletResponse response,
47                         FilterChain filter) throws IOException, ServletException {
48                 if (request instanceof HttpServletRequest) {
49                         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
50                         String authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
51                         String path = ((HttpServletRequest) request).getRequestURI();
52                         // better injected
53                         AuthenticationService authenticationService = new AuthenticationService();
54
55                         boolean authenticationStatus = authenticationService.authenticate(authCredentials);
56
57                         if (authenticationStatus) {
58                                 filter.doFilter(request, response);
59                         } else if(path.contains("notifications")){
60                                 filter.doFilter(request, response);
61                         } else {
62                                 if (response instanceof HttpServletResponse) {
63                                         HttpServletResponse httpServletResponse = (HttpServletResponse) response;
64                                         httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
65                                 }
66                         }
67                 }
68         }
69
70         @Override
71         public void destroy() {
72         }
73
74         @Override
75         public void init(FilterConfig arg0) throws ServletException {
76         }
77
78 }