Collection syntax change because of Sonar
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / CadiWrap.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 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  * 
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
22 package org.onap.aaf.cadi;
23
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletRequestWrapper;
30
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.filter.NullPermConverter;
33 import org.onap.aaf.cadi.filter.PermConverter;
34 import org.onap.aaf.cadi.lur.EpiLur;
35 import org.onap.aaf.cadi.principal.TaggedPrincipal;
36 import org.onap.aaf.cadi.taf.TafResp;
37
38
39
40 /**
41  * Inherit the HttpServletRequestWrapper, which calls methods of delegate it's created with, but
42  * overload the key security mechanisms with CADI mechanisms
43  * 
44  * This works with mechanisms working strictly with HttpServletRequest (i.e. Servlet Filters)
45  * 
46  * Specialty cases, i.e. Tomcat, which for their containers utilize their own mechanisms and Wrappers, you may
47  * need something similar.  See AppServer specific code (i.e. tomcat) for these.
48  * 
49  * @author Jonathan
50  *
51  */
52 public class CadiWrap extends HttpServletRequestWrapper implements HttpServletRequest, BasicCred {
53         private TaggedPrincipal principal;
54         private Lur lur;
55         private String user; // used to set user/pass from brain-dead protocols like WSSE 
56         private byte[] password;
57         private PermConverter pconv;
58         private Access access; 
59         
60         /**
61          * Standard Wrapper constructor for Delegate pattern
62          * @param request
63          */
64         public CadiWrap(HttpServletRequest request, TafResp tafResp, Lur lur) {
65                 super(request);
66                 principal = tafResp.getPrincipal();
67                 access = tafResp.getAccess();
68                 this.lur = lur;
69                 pconv = NullPermConverter.singleton();
70         }
71
72         /**
73          * Standard Wrapper constructor for Delegate pattern, with PermConverter
74          * @param request
75          */
76         public CadiWrap(HttpServletRequest request, TafResp tafResp, Lur lur, PermConverter pc) {
77                 super(request);
78                 principal = tafResp.getPrincipal();
79                 access = tafResp.getAccess();
80                 this.lur = lur;
81                 pconv = pc;
82         }
83
84
85         /**
86          * Part of the HTTP Security API.  Declare the User associated with this HTTP Transaction.
87          * CADI does this by reporting the name associated with the Principal obtained, if any.
88          */
89         @Override
90         public String getRemoteUser() {
91                 return principal==null?null:principal.getName();
92         }
93
94         /**
95          * Part of the HTTP Security API.  Return the User Principal associated with this HTTP 
96          * Transaction.
97          */
98         @Override
99         public Principal getUserPrincipal() {
100                 return principal;
101         }
102         
103         /**
104          * This is the key API call for AUTHZ in J2EE.  Given a Role (String passed in), is the user
105          * associated with this HTTP Transaction allowed to function in this Role?
106          * 
107          * For CADI, we pass the responsibility for determining this to the "LUR", which may be
108          * determined by the Enterprise.
109          * 
110          * Note: Role check is also done in "CadiRealm" in certain cases...
111          * 
112          *
113          */
114         @Override
115         public boolean isUserInRole(String perm) {
116                 return perm==null?false:checkPerm(access,"(HttpRequest)",principal,pconv,lur,perm);
117         }
118         
119         public static boolean checkPerm(Access access, String caller, Principal principal, PermConverter pconv, Lur lur, String perm) {
120                 if(principal== null) {
121                         access.log(Level.AUDIT,caller, "No Principal in Transaction");
122                         return false;
123                 } else { 
124                         perm = pconv.convert(perm);
125                         if(lur.fish(principal,lur.createPerm(perm))) {
126                                 access.log(Level.DEBUG,caller, principal.getName(), "has", perm);
127                                 return true;
128                         } else {
129                                 access.log(Level.DEBUG,caller, principal.getName(), "does not have", perm);
130                                 return false;
131                         }
132                 }
133
134         }
135
136         /** 
137          * CADI Function (Non J2EE standard). GetPermissions will read the Permissions from AAF (if configured) and Roles from Local Lur, etc
138          *  as implemented with lur.fishAll
139          *  
140          *  To utilize, the Request must be a "CadiWrap" object, then call.
141          */
142         public List<Permission> getPermissions(Principal p) {
143                 List<Permission> perms = new ArrayList<>();
144                 lur.fishAll(p, perms);
145                 return perms;
146         }
147         /**
148          * Allow setting of tafResp and lur after construction
149          * 
150          * This can happen if the CadiWrap is constructed in a Valve other than CadiValve
151          */
152         public void set(TafResp tafResp, Lur lur) {
153                 principal = tafResp.getPrincipal();
154                 access = tafResp.getAccess();
155                 this.lur = lur;
156         }
157
158         public String getUser() {
159                 if(user==null && principal!=null) {
160                         user = principal.getName();
161                 }
162                 return user;
163         }
164
165         public byte[] getCred() {
166                 return password;
167         }
168
169         public void setUser(String user) {
170                 this.user = user;
171         }
172
173         public void setCred(byte[] passwd) {
174                 password = passwd;
175         }
176         
177         public CadiWrap setPermConverter(PermConverter pc) {
178                 pconv = pc;
179                 return this;
180         }
181         
182         // Add a feature
183         public void invalidate(String id) {
184                 if(lur instanceof EpiLur) {
185                         ((EpiLur)lur).remove(id);
186                 } else if(lur instanceof CachingLur) {
187                         ((CachingLur<?>)lur).remove(id);
188                 }
189         }
190         
191         public Lur getLur() {
192                 return lur;
193         }
194         
195         public Access access() {
196                 return access;
197         }
198 }