56371c7d931c301250fa4f24e13d5fc5b6fbd974
[music.git] / src / main / java / org / onap / music / authentication / CadiAuthFilter.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.authentication;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.http.HttpServletRequest;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import org.onap.aaf.cadi.CadiWrap;
36 import org.onap.aaf.cadi.Permission;
37 import org.onap.aaf.cadi.PropAccess;
38 import org.onap.aaf.cadi.aaf.AAFPermission;
39 import org.onap.aaf.cadi.filter.CadiFilter;
40 import org.onap.music.eelf.logging.EELFLoggerDelegate;
41 import org.onap.music.main.MusicCore;
42
43 public class CadiAuthFilter extends CadiFilter {
44
45     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CadiAuthFilter.class);
46
47     public CadiAuthFilter(PropAccess access) throws ServletException {
48         super(true, access);
49     }
50     
51     public void init(FilterConfig filterConfig) throws ServletException {
52         super.init(filterConfig);
53     }
54
55
56     private boolean matchPattern(String requestedPath, String includeUrl) {
57         includeUrl = includeUrl.substring(1);
58         String[] path = requestedPath.split("/");
59         if (path.length > 1) {
60             String[] roleFunctionArray = includeUrl.split("/");
61             boolean match = true;
62             for (int i = 0; i < roleFunctionArray.length; i++) {
63                 if (match) {
64                     if (!"*".equals(roleFunctionArray[i])) {
65                         Pattern p = Pattern.compile(Pattern.quote(path[i]), Pattern.CASE_INSENSITIVE);
66                         Matcher m = p.matcher(roleFunctionArray[i]);
67                         match = m.matches();
68                     } else if (roleFunctionArray[i].equals("*")) {
69                         match = true;
70                     }
71
72                 }
73             }
74             if (match)
75                 return match;
76         } else {
77             if (requestedPath.matches(includeUrl))
78                 return true;
79             else if ("*".equals(includeUrl))
80                 return true;
81         }
82         return false;
83     }
84     
85
86     public static List<AAFPermission> getAAFPermissions(HttpServletRequest request) { 
87         CadiWrap wrapReq = (CadiWrap) request; 
88         List<Permission> perms = wrapReq.getPermissions(wrapReq.getUserPrincipal()); 
89         List<AAFPermission> aafPermsList = new ArrayList<>(); 
90         for (Permission perm : perms) { 
91             AAFPermission aafPerm = (AAFPermission) perm; 
92             aafPermsList.add(aafPerm); 
93             logger.info(aafPerm.toString());
94             logger.info(aafPerm.getType());
95         } 
96         return aafPermsList; 
97     } 
98     
99     public static List<AAFPermission> getAAFPermissions(ServletRequest request) { 
100         CadiWrap wrapReq = (CadiWrap) request; 
101         List<Permission> perms = wrapReq.getPermissions(wrapReq.getUserPrincipal()); 
102         List<AAFPermission> aafPermsList = new ArrayList<>(); 
103         for (Permission perm : perms) { 
104             AAFPermission aafPerm = (AAFPermission) perm; 
105             aafPermsList.add(aafPerm); 
106         } 
107         return aafPermsList; 
108     }
109
110 }