CADI authentication and authorization filters
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / AuthorizationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 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  * 
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 package org.onap.dmaap.dbcapi.resources;
22
23 import javax.ws.rs.container.ContainerRequestContext;
24 import javax.ws.rs.container.ContainerRequestFilter;
25
26 import org.apache.log4j.Logger;
27 import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException;
28 import org.onap.dmaap.dbcapi.service.ApiService;
29 import org.onap.dmaap.dbcapi.util.DmaapConfig;
30
31
32 @Authorization
33 public class AuthorizationFilter implements ContainerRequestFilter   {
34
35         private static final String AAF_FLAG = "UseAAF";
36         private final Logger logger = Logger.getLogger(AuthorizationFilter.class.getName());
37         private final ResponseBuilder responseBuilder = new ResponseBuilder();
38         private final boolean isAafEnabled;
39
40
41         public AuthorizationFilter() {
42                 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
43                 String flag = dmaapConfig.getProperty(AAF_FLAG, "false");
44                 isAafEnabled = "true".equalsIgnoreCase(flag);
45         }
46
47         @Override
48         public void filter(ContainerRequestContext requestContext) {
49
50                 if(!isAafEnabled) {
51                         ApiService apiResp = new ApiService()
52                                 .setAuth(requestContext.getHeaderString("Authorization"))
53                                 .setUriPath(requestContext.getUriInfo().getPath())
54                                 .setHttpMethod(requestContext.getMethod())
55                                 .setRequestId(requestContext.getHeaderString("X-ECOMP-RequestID"));
56
57                         try {
58                                 apiResp.checkAuthorization();
59                         } catch (AuthenticationErrorException ae) {
60                                 logger.error("Error", ae);
61                                 requestContext.abortWith(responseBuilder.unauthorized(apiResp.getErr().getMessage()));
62                         } catch (Exception e) {
63                                 logger.error("Error", e);
64                                 requestContext.abortWith(responseBuilder.unavailable());
65                         }
66                 }
67         }
68
69 }