Update Fixes from testing
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / api / API_Perms.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.auth.service.api;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25 import static org.onap.aaf.auth.rserv.HttpMethods.DELETE;
26 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
27 import static org.onap.aaf.auth.rserv.HttpMethods.POST;
28 import static org.onap.aaf.auth.rserv.HttpMethods.PUT;
29
30 import java.net.URLDecoder;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.eclipse.jetty.http.HttpStatus;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.layer.Result;
38 import org.onap.aaf.auth.service.AAF_Service;
39 import org.onap.aaf.auth.service.Code;
40 import org.onap.aaf.auth.service.facade.AuthzFacade;
41 import org.onap.aaf.auth.service.mapper.Mapper.API;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.misc.env.util.Split;
44
45 public class API_Perms {
46     public static void timeSensitiveInit(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
47         /** 
48          *  gets all permissions by user name
49          */
50         authzAPI.route(GET, "/authz/perms/user/:user", API.PERMS, new Code(facade,"Get Permissions by User",true) {
51             public void handle(
52                     AuthzTrans trans, 
53                     HttpServletRequest req,
54                     HttpServletResponse resp) throws Exception {
55                 
56                 String scopes = req.getParameter("scopes");
57                 Result<Void> r;
58                 if (scopes==null) {
59                     r = context.getPermsByUser(trans, resp, pathParam(req, "user"));
60                 } else {
61                     r = context.getPermsByUserScope(trans, resp, pathParam(req, "user"),Split.split(':', scopes));
62                 }
63                 switch(r.status) {
64                     case OK: 
65                         resp.setStatus(HttpStatus.OK_200); 
66                         break;
67                     default:
68                         context.error(trans,resp,r);
69                 }
70             }
71
72         });
73         
74         /** 
75          *  gets all permissions by user name
76          */
77         authzAPI.route(POST, "/authz/perms/user/:user", API.PERMS, new Code(facade,"Get Permissions by User, Query AAF Perms",true) {
78             public void handle(
79                     AuthzTrans trans, 
80                     HttpServletRequest req,
81                     HttpServletResponse resp) throws Exception {
82                 
83                 Result<Void> r = context.getPermsByUserWithAAFQuery(trans, req, resp, pathParam(req, "user"));
84                 switch(r.status) {
85                     case OK: 
86                         resp.setStatus(HttpStatus.OK_200); 
87                         break;
88                     default:
89                         context.error(trans,resp,r);
90                 }
91             }
92
93         });
94
95
96     } // end timeSensitiveInit
97
98     public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
99         /**
100          * Create a Permission
101          */
102         authzAPI.route(POST,"/authz/perm",API.PERM_REQ,new Code(facade,"Create a Permission",true) {
103             public void handle(
104                     AuthzTrans trans, 
105                     HttpServletRequest req,
106                     HttpServletResponse resp) throws Exception {
107                 
108                 Result<Void> r = context.createPerm(trans, req, resp);
109                 switch(r.status) {
110                     case OK: 
111                         resp.setStatus(HttpStatus.CREATED_201); 
112                         break;
113                     default:
114                         context.error(trans,resp,r);
115                 }
116             }
117         });
118
119         /** 
120          *  get details of Permission
121          */
122         authzAPI.route(GET, "/authz/perms/:type/:instance/:action", API.PERMS, new Code(facade,"Get Permissions by Key",true) {
123             public void handle(
124                     AuthzTrans trans, 
125                     HttpServletRequest req,
126                     HttpServletResponse resp) throws Exception {
127                 
128                 Result<Void> r = context.getPermsByName(trans, resp, 
129                         pathParam(req, "type"),
130                         URLDecoder.decode(pathParam(req, "instance"),Config.UTF_8),
131                         pathParam(req, "action"));
132                 switch(r.status) {
133                     case OK: 
134                         resp.setStatus(HttpStatus.OK_200); 
135                         break;
136                     default:
137                         context.error(trans,resp,r);
138                 }
139             }
140
141         });
142         
143         /** 
144          *  get children of Permission
145          */
146         authzAPI.route(GET, "/authz/perms/:type", API.PERMS, new Code(facade,"Get Permissions by Type",true) {
147             public void handle(
148                     AuthzTrans trans, 
149                     HttpServletRequest req,
150                     HttpServletResponse resp) throws Exception {
151                 
152                 Result<Void> r = context.getPermsByType(trans, resp, pathParam(req, "type"));
153                 switch(r.status) {
154                     case OK: 
155                         resp.setStatus(HttpStatus.OK_200); 
156                         break;
157                     default:
158                         context.error(trans,resp,r);
159                 }
160             }
161
162         });
163
164         
165         /**
166          * gets all permissions by role name
167          */
168         authzAPI.route(GET,"/authz/perms/role/:role",API.PERMS,new Code(facade,"Get Permissions by Role",true) {
169             public void handle(
170                     AuthzTrans trans, 
171                     HttpServletRequest req,
172                     HttpServletResponse resp) throws Exception {
173                 
174                 Result<Void> r = context.getPermsForRole(trans, resp, pathParam(req, "role"));
175                 switch(r.status) {
176                     case OK: 
177                         resp.setStatus(HttpStatus.OK_200); 
178                         break;
179                     default:
180                         context.error(trans,resp,r);
181                 }
182             }
183         });
184
185         /**
186          * gets all permissions by Namespace
187          */
188         authzAPI.route(GET,"/authz/perms/ns/:ns",API.PERMS,new Code(facade,"Get PermsByNS",true) {
189             public void handle(
190                     AuthzTrans trans, 
191                     HttpServletRequest req,
192                     HttpServletResponse resp) throws Exception {
193                 
194                 Result<Void> r = context.getPermsByNS(trans, resp, pathParam(req, "ns"));
195                 switch(r.status) {
196                     case OK: 
197                         resp.setStatus(HttpStatus.OK_200); 
198                         break;
199                     default:
200                         context.error(trans,resp,r);
201                 }
202             }
203         });
204         
205         /**
206          * Set a perm's description
207          */
208         authzAPI.route(PUT,"/authz/perm",API.PERM_REQ,new Code(facade,"Set Description for Permission",true) {
209             @Override
210             public void handle(
211                     AuthzTrans trans, 
212                     HttpServletRequest req,
213                     HttpServletResponse resp) throws Exception {
214                 
215                 Result<Void> r = context.updatePermDescription(trans, req, resp);
216                 switch(r.status) {
217                     case OK: 
218                         resp.setStatus(HttpStatus.OK_200); 
219                         break;
220                     default:
221                         context.error(trans,resp,r);
222                 }
223             }
224         });    
225         
226         /**
227          * Update a permission with a rename
228          */
229         authzAPI.route(PUT,"/authz/perm/:type/:instance/:action",API.PERM_REQ,new Code(facade,"Update a Permission",true) {
230             public void handle(
231                     AuthzTrans trans, 
232                     HttpServletRequest req,
233                     HttpServletResponse resp) throws Exception {
234                 
235                 Result<Void> r = context.renamePerm(trans, req, resp, 
236                         pathParam(req, "type"), 
237                         URLDecoder.decode(pathParam(req, "instance"),Config.UTF_8), 
238                         pathParam(req, "action"));
239                 switch(r.status) {
240                     case OK: 
241                         resp.setStatus(HttpStatus.OK_200); 
242                         break;
243                     default:
244                         context.error(trans,resp,r);
245                 }
246             }
247         });    
248         
249         /**
250          * Delete a Permission
251          */
252         authzAPI.route(DELETE,"/authz/perm",API.PERM_REQ,new Code(facade,"Delete a Permission",true) {
253             public void handle(
254                     AuthzTrans trans, 
255                     HttpServletRequest req,
256                     HttpServletResponse resp) throws Exception {
257                 
258                 Result<Void> r = context.deletePerm(trans,req, resp);
259                 switch(r.status) {
260                     case OK: 
261                         resp.setStatus(HttpStatus.OK_200); 
262                         break;
263                     default:
264                         context.error(trans,resp,r);
265                 }
266             }
267         });
268
269         
270         
271
272         /**
273          * Delete a Permission
274          */
275         authzAPI.route(DELETE,"/authz/perm/:name/:type/:action",API.PERM_KEY,new Code(facade,"Delete a Permission",true) {
276             public void handle(
277                     AuthzTrans trans, 
278                     HttpServletRequest req,
279                     HttpServletResponse resp) throws Exception {
280                 
281                 Result<Void> r = context.deletePerm(trans, resp,
282                         pathParam(req, ":name"),
283                         pathParam(req, ":type"),
284                         pathParam(req, ":action"));
285                 switch(r.status) {
286                     case OK: 
287                         resp.setStatus(HttpStatus.OK_200); 
288                         break;
289                     default:
290                         context.error(trans,resp,r);
291                 }
292             }
293         });
294
295     } // end init
296 }
297
298
299