blob: d4c7e3c5fdc22252a3e5db14f2dd7211971a9d8f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package org.onap.vid.controller.filter;
import org.apache.commons.lang3.StringUtils;
import org.onap.vid.scheduler.SchedulerProperties;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.util.SystemProperties;
import org.springframework.web.filter.GenericFilterBean;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by amichai on 13/05/2018.
*/
@WebFilter(urlPatterns = "/change-management/workflow/*")
public class ClientCredentialsFilter extends GenericFilterBean {
private final static EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ClientCredentialsFilter.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse))
return;
String expectedAuthorization = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_BASIC_AUTH);
String actualAuthorization = ((HttpServletRequest)request).getHeader("Authorization");
if (verifyClientCredentials(actualAuthorization, expectedAuthorization)) {
LOGGER.warn(EELFLoggerDelegate.debugLogger,"Client credentials authenticated.");
chain.doFilter(request, response);
return;
}
LOGGER.warn(EELFLoggerDelegate.debugLogger,"Client did not provide the expected credentials.");
((HttpServletResponse) response).sendError(401);
}
public boolean verifyClientCredentials(String actualAuthorization, String expectedAuthorization)
{
if (StringUtils.isEmpty(expectedAuthorization))
{
LOGGER.warn(EELFLoggerDelegate.debugLogger,String.format("Expected Authorization is not configured (key: %s)", SchedulerProperties.SCHEDULER_BASIC_AUTH));
return true;
}
if (StringUtils.isEmpty(actualAuthorization))
{
LOGGER.warn(EELFLoggerDelegate.debugLogger,"Authorization header is missing.");
return false;
}
return actualAuthorization.equals(expectedAuthorization);
}
}
|