5c7170b0e6a220ae56041e76f91d66b071676421
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / util / DMaaPAuthFilter.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *
21  *******************************************************************************/
22 package org.onap.dmaap.util;
23
24 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
25 import java.io.IOException;
26
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
34 import org.onap.dmaap.dmf.mr.utils.Utils;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.springframework.stereotype.Component;
38
39 import org.onap.aaf.cadi.filter.CadiFilter;
40
41 /**
42  * This is a Servlet Filter class overriding the AjscCadiFilter
43  */
44 @Component
45 public class DMaaPAuthFilter extends CadiFilter {
46
47     private static final String FORCE_AAF_FLAG = "forceAAF";
48     static final String X509_ATTR = "javax.servlet.request.X509Certificate";
49     static final String AUTH_HEADER = "Authorization";
50     static final String APP_HEADER = "AppName";
51     static final String COOKIE_HEADER = "cookie";
52     private static final EELFLogger log = EELFManager.getInstance().getLogger(DMaaPAuthFilter.class);
53
54     public DMaaPAuthFilter() {
55         super();
56     }
57
58     /**
59      * This method will disable Cadi Authentication if cambria headers are present in the request else continue with
60      * Cadi Authentication
61      */
62     @Override
63     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
64         log.info("inside servlet filter Cambria Auth Headers checking before doing other Authentication");
65         if (shouldFilterWithCADI((HttpServletRequest) req)) {
66             super.doFilter(req, res, chain);
67         } else {
68             System.setProperty("CadiAuthN", "authentication-scheme-2");
69             chain.doFilter(req, res);
70         }
71     }
72
73     boolean shouldFilterWithCADI(HttpServletRequest request) {
74         return isCadiEnabled() &&
75             (isAAFforced() || isAuthDataProvided(request) || isInvenioApp(request));
76     }
77
78     private boolean isAuthDataProvided(HttpServletRequest request) {
79         return (null != request.getHeader(AUTH_HEADER)) || hasClientCertificate(request);
80     }
81
82     private boolean isInvenioApp(HttpServletRequest request) {
83         return (null != request.getHeader(APP_HEADER)) && request.getHeader(APP_HEADER).equalsIgnoreCase("invenio") &&
84             (null != request.getHeader(COOKIE_HEADER));
85     }
86
87     private boolean hasClientCertificate(HttpServletRequest request) {
88         return request.getAttribute(X509_ATTR) != null;
89     }
90
91     boolean isCadiEnabled() {
92         return Utils.isCadiEnabled();
93     }
94
95     boolean isAAFforced() {
96         return Boolean.valueOf(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, FORCE_AAF_FLAG));
97     }
98
99 }
100