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