f1129057f6fa182c6023e4ea7b10ee1e020f67d3
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / filter / ClientCredentialsFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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.vid.controller.filter;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.vid.scheduler.SchedulerProperties;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.portalsdk.core.util.SystemProperties;
27 import org.springframework.web.filter.GenericFilterBean;
28 import javax.servlet.FilterChain;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.annotation.WebFilter;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.IOException;
36
37 /**
38  * Created by amichai on 13/05/2018.
39  */
40 @WebFilter(urlPatterns = "/change-management/workflow/*")
41 public class ClientCredentialsFilter  extends GenericFilterBean {
42
43     private static final EELFLoggerDelegate filterLogger = EELFLoggerDelegate.getLogger(ClientCredentialsFilter.class);
44
45
46     @Override
47     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
48
49         if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse))
50             return;
51
52         String expectedAuthorization = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_BASIC_AUTH);
53         String actualAuthorization = ((HttpServletRequest)request).getHeader("Authorization");
54
55         if (verifyClientCredentials(actualAuthorization, expectedAuthorization)) {
56             filterLogger.warn(EELFLoggerDelegate.debugLogger,"Client credentials authenticated.");
57             chain.doFilter(request, response);
58             return;
59         }
60
61         filterLogger.warn(EELFLoggerDelegate.debugLogger,"Client did not provide the expected credentials.");
62         ((HttpServletResponse) response).sendError(401);
63     }
64
65     public boolean verifyClientCredentials(String actualAuthorization, String expectedAuthorization)
66     {
67         if (StringUtils.isEmpty(expectedAuthorization))
68         {
69             filterLogger.warn(EELFLoggerDelegate.debugLogger,String.format("Expected Authorization is not configured (key: %s)", SchedulerProperties.SCHEDULER_BASIC_AUTH));
70             return true;
71         }
72
73         if (StringUtils.isEmpty(actualAuthorization))
74         {
75             filterLogger.warn(EELFLoggerDelegate.debugLogger,"Authorization header is missing.");
76             return false;
77         }
78
79         return actualAuthorization.equals(expectedAuthorization);
80     }
81
82 }