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