Update AAF Version 1.0.0
[aaf/authz.git] / authz-certman / src / main / java / com / att / authz / cm / api / API_Cert.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 com.att.authz.cm.api;\r
24 \r
25 import javax.servlet.http.HttpServletRequest;\r
26 import javax.servlet.http.HttpServletResponse;\r
27 \r
28 import com.att.aft.dme2.internal.jetty.http.HttpStatus;\r
29 import com.att.authz.cm.ca.CA;\r
30 import com.att.authz.cm.mapper.Mapper.API;\r
31 import com.att.authz.cm.service.CertManAPI;\r
32 import com.att.authz.cm.service.Code;\r
33 import com.att.authz.env.AuthzTrans;\r
34 import com.att.authz.layer.Result;\r
35 import com.att.cssa.rserv.HttpMethods;\r
36 import com.att.inno.env.Slot;\r
37 import com.att.inno.env.TransStore;\r
38 \r
39 /**\r
40  * API Apis.. using Redirect for mechanism\r
41  * \r
42  *\r
43  */\r
44 public class API_Cert {\r
45         public static final String CERT_AUTH = "CertAuthority";\r
46         private static Slot sCertAuth;\r
47 \r
48         /**\r
49          * Normal Init level APIs\r
50          * \r
51          * @param cmAPI\r
52          * @param facade\r
53          * @throws Exception\r
54          */\r
55         public static void init(final CertManAPI cmAPI) throws Exception {\r
56                 // Check for Created Certificate Authorities in TRANS\r
57                 sCertAuth = ((TransStore) cmAPI.env).slot(CERT_AUTH);\r
58                 \r
59                 ////////\r
60                 // Overall APIs\r
61                 ///////\r
62                 cmAPI.route(HttpMethods.PUT,"/cert/:ca",API.CERT_REQ,new Code(cmAPI,"Request Certificate") {\r
63                         @Override\r
64                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {\r
65                                 String key = pathParam(req, ":ca");\r
66                                 CA ca;\r
67                                 if((ca = cmAPI.getCA(key))==null) {\r
68                                         context.error(trans,resp,Result.ERR_BadData,"CA %s is not supported",key);\r
69                                 } else {\r
70                                         trans.put(sCertAuth, ca);\r
71                                         \r
72                                         Result<Void> r = context.requestCert(trans, req, resp, req.getParameter("withTrust")!=null);\r
73                                         if(r.isOK()) {\r
74                                                 resp.setStatus(HttpStatus.OK_200);\r
75                                         } else {\r
76                                                 context.error(trans,resp,r);\r
77                                         }\r
78                                 }\r
79                         }\r
80                 });\r
81                 \r
82                 /**\r
83                  * \r
84                  */\r
85                 cmAPI.route(HttpMethods.GET, "/cert/may/:perm", API.VOID, new Code(cmAPI,"Check Permission") {\r
86                         @Override\r
87                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {\r
88                                 Result<Void> r = context.check(trans, resp, pathParam(req,"perm"));\r
89                                 if(r.isOK()) {\r
90                                         resp.setStatus(HttpStatus.OK_200);\r
91                                 } else {\r
92                                         trans.checkpoint(r.errorString());\r
93                                         context.error(trans,resp,Result.err(Result.ERR_Denied,"%s does not have Permission.",trans.user()));\r
94                                 }\r
95                         }\r
96                 });\r
97 \r
98         }\r
99 }\r