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