add AAF based on a flag
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / 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 com.att.nsa.dmaap.util;
23
24 import java.io.IOException;
25
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31
32 import com.att.dmf.mr.utils.Utils;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import org.springframework.stereotype.Component;
36
37 import org.onap.aaf.cadi.filter.CadiFilter;
38 //import ajsc.external.plugins.cadi.AjscCadiFilter;
39 import javax.servlet.FilterConfig;
40
41 /**
42          * This is a Servlet Filter class
43          * overriding the AjscCadiFilter
44          */
45 @Component      
46 public class DMaaPAuthFilter extends CadiFilter {
47         
48                 //private Logger log = Logger.getLogger(DMaaPAuthFilter.class.toString());
49
50                 private static final EELFLogger log = EELFManager.getInstance().getLogger(DMaaPAuthFilter.class);
51                                 
52                 public DMaaPAuthFilter() throws Exception {
53                         super();
54                 }
55                 
56         /*      public void init(FilterConfig filterConfig) throws ServletException {
57               
58                         super.init(filterConfig);
59               System.out.println("---------------------------- in init method");
60                 }*/
61                 
62                 /**
63                  * This method will disable Cadi Authentication 
64                  * if cambria headers are present in the request
65                  * else continue with Cadi Authentication
66                  */
67                 @Override
68                 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
69                                 ServletException {
70                 log.info("inside servlet filter Cambria Auth Headers checking before doing other Authentication");
71                         HttpServletRequest request = (HttpServletRequest) req;
72                                 boolean forceAAF = Boolean.valueOf(System.getProperty("forceAAF"));
73                                 
74                                 //if (forceAAF || null != request.getHeader("Authorization") ){
75                                         if (Utils.isCadiEnabled()&&(forceAAF || null != request.getHeader("Authorization") || 
76                                                         (null != request.getHeader("AppName") &&  request.getHeader("AppName").equalsIgnoreCase("invenio") &&
77                                                          null != request.getHeader("cookie")))){
78                                                 super.doFilter(req, res, chain);
79                                                 
80                                 } else { 
81                                         System.setProperty("CadiAuthN", "authentication-scheme-2");
82                                                 chain.doFilter(req, res);
83                                         
84                                         
85                                 } 
86
87                 }
88
89         }
90