46285396981099f2d0dffcbdd6e7a1060a34c02a
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / filters / AafAuthorizationFilter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.aai.aaf.filters;
22
23 import org.onap.aai.aaf.auth.ResponseFormatter;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.boot.web.filter.OrderedRequestContextFilter;
27 import org.springframework.context.annotation.Profile;
28 import org.springframework.context.annotation.PropertySource;
29 import org.springframework.stereotype.Component;
30
31 import javax.servlet.FilterChain;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.stream.Collectors;
40
41 /**
42  * AAF authorization filter
43  */
44
45 @Component
46 @Profile(AafProfiles.AAF_AUTHENTICATION)
47 @PropertySource(value = "file:${CONFIG_HOME}/aaf/permissions.properties", ignoreResourceNotFound = true)
48 @PropertySource(value = "file:${server.local.startpath}/aaf/permissions.properties", ignoreResourceNotFound = true)
49 public class AafAuthorizationFilter extends OrderedRequestContextFilter {
50
51     private static final String ADVANCED = "advanced";
52     private static final String BASIC = "basic";
53
54     private final String type;
55     private final String instance;
56
57     private GremlinFilter gremlinFilter;
58
59     private List<String> advancedKeywordsList;
60
61     @Autowired
62     public AafAuthorizationFilter(
63         GremlinFilter gremlinFilter,
64         @Value("${permission.type}") String type,
65         @Value("${permission.instance}") String instance,
66         @Value("${advanced.keywords.list:}") String advancedKeys
67     ) {
68         this.gremlinFilter = gremlinFilter;
69         this.type = type;
70         this.instance = instance;
71         if(advancedKeys == null || advancedKeys.isEmpty()){
72             this.advancedKeywordsList = new ArrayList<>();
73         } else {
74             this.advancedKeywordsList = Arrays.stream(advancedKeys.split(","))
75                 .collect(Collectors.toList());
76         }
77         this.setOrder(FilterPriority.AAF_AUTHORIZATION.getPriority());
78     }
79
80     @Override
81     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
82         if(request.getRequestURI().matches("^.*/util/echo$")){
83             filterChain.doFilter(request, response);
84         }
85         if(request.getRequestURI().endsWith("/query")){
86             gremlinFilter.doBasicAuthFilter(request, response, filterChain);
87         } else {
88
89             String permission = null;
90
91             if(advancedKeywordsList == null || advancedKeywordsList.size() == 0) {
92                 permission = String.format("%s|%s|%s", type, instance, request.getMethod().toLowerCase());
93             } else {
94
95                 boolean isAdvanced = this.containsAdvancedKeywords(request);
96
97                 //if the URI contains advanced.keywords it's an advanced query
98                 String queryType = isAdvanced ? ADVANCED : BASIC;
99                 permission = String.format("%s|%s|%s", type, instance, queryType);
100             }
101
102             boolean isAuthorized = request.isUserInRole(permission);
103
104             if(!isAuthorized){
105                 ResponseFormatter.errorResponse(request, response);
106             } else {
107                 filterChain.doFilter(request,response);
108             }
109
110         }
111     }
112
113     private boolean containsAdvancedKeywords(HttpServletRequest request) {
114         String uri = request.getRequestURI();
115         for (String keyword: advancedKeywordsList) {
116             if (uri.contains(keyword)) {
117                 return true;
118             }
119         }
120         return false;
121     }
122 }