CADI authentication and authorization filters
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / AAFAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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 package org.onap.dmaap.dbcapi.resources;
21
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.IOException;
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.FilterConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletResponse;
32 import org.apache.log4j.Logger;
33 import org.eclipse.jetty.http.HttpStatus;
34 import org.onap.aaf.cadi.PropAccess;
35 import org.onap.aaf.cadi.filter.CadiFilter;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.util.DmaapConfig;
38
39 public class AAFAuthenticationFilter implements Filter {
40
41     private static final Logger LOGGER = Logger.getLogger(AAFAuthenticationFilter.class.getName());
42     static final String CADI_PROPERTIES = "cadi.properties";
43     static final String AAF_AUTHN_FLAG = "UseAAF";
44
45     private boolean isAafEnabled;
46     private CadiFilter cadiFilter;
47
48     @Override
49     public void init(FilterConfig filterConfig) throws ServletException {
50         DmaapConfig dmaapConfig = getConfig();
51         String flag = dmaapConfig.getProperty(AAF_AUTHN_FLAG, "false");
52         isAafEnabled = "true".equalsIgnoreCase(flag);
53         initCadi(dmaapConfig);
54     }
55
56
57     @Override
58     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
59         throws IOException, ServletException {
60
61         if(isAafEnabled) {
62             cadiFilter.doFilter(servletRequest, servletResponse, filterChain);
63             updateResponseBody((HttpServletResponse)servletResponse);
64         } else {
65             filterChain.doFilter(servletRequest, servletResponse);
66         }
67     }
68
69     private void updateResponseBody(HttpServletResponse httpResponse)
70         throws IOException {
71         if(httpResponse.getStatus() == 401) {
72             String errorMsg = "invalid or no credentials provided";
73             LOGGER.error(errorMsg);
74             httpResponse.setContentType("application/json");
75             httpResponse.setCharacterEncoding("UTF-8");
76             httpResponse.getWriter().print(buildErrorResponse(errorMsg));
77             httpResponse.getWriter().flush();
78         }
79     }
80
81     private String buildErrorResponse(String msg) {
82         try {
83             return new ObjectMapper().writeValueAsString(new ApiError(HttpStatus.UNAUTHORIZED_401, msg, "Authentication"));
84         } catch (JsonProcessingException e) {
85             LOGGER.warn("Could not serialize response entity: " + e.getMessage());
86             return "";
87         }
88     }
89
90
91     @Override
92     public void destroy() {
93         //nothing to cleanup
94     }
95
96     private void initCadi(DmaapConfig dmaapConfig) throws ServletException {
97         if(isAafEnabled) {
98             try {
99                 String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES);
100                 if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) {
101                     cadiFilter = new CadiFilter(new PropAccess(cadiPropertiesFile));
102                 } else {
103                     throw new ServletException("Cannot initialize CADI filter.CADI properties not available.");
104                 }
105             } catch (ServletException e) {
106                 LOGGER.error("CADI init error :" + e.getMessage());
107                 throw e;
108             }
109         }
110     }
111
112     DmaapConfig getConfig() {
113         return (DmaapConfig) DmaapConfig.getConfig();
114     }
115
116     //tests only
117     CadiFilter getCadiFilter() {
118         return cadiFilter;
119     }
120
121     void setCadiFilter(CadiFilter cadiFilter) {
122         this.cadiFilter = cadiFilter;
123     }
124
125     boolean isAafEnabled() {
126         return isAafEnabled;
127     }
128 }