Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / api / API_Cert.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.cm.api;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.onap.aaf.auth.cm.AAF_CM;
29 import org.onap.aaf.auth.cm.ca.CA;
30 import org.onap.aaf.auth.cm.mapper.Mapper.API;
31 import org.onap.aaf.auth.cm.service.Code;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.layer.Result;
34 import org.onap.aaf.auth.rserv.HttpMethods;
35 import org.onap.aaf.misc.env.Slot;
36
37 /**
38  * API Apis.. using Redirect for mechanism
39  *
40  * @author Jonathan
41  *
42  */
43 public class API_Cert {
44     public static final String CERT_AUTH = "CertAuthority";
45     private static Slot sCertAuth;
46
47     /**
48      * Normal Init level APIs
49      *
50      * @param aafCM
51      * @param facade
52      * @throws Exception
53      */
54     public static void init(final AAF_CM aafCM) throws Exception {
55         // Check for Created Certificate Authorities in TRANS
56         sCertAuth = aafCM.env.slot(CERT_AUTH);
57
58         ////////
59         // Overall APIs
60         ///////
61         aafCM.route(HttpMethods.PUT,"/cert/:ca",API.CERT_REQ,new Code(aafCM,"Request Certificate") {
62             @Override
63             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
64                 String key = pathParam(req, ":ca");
65                 CA ca;
66                 if ((ca = aafCM.getCA(key))==null) {
67                     context.error(trans,resp,Result.ERR_BadData,"CA %s is not supported",key);
68                 } else {
69                     trans.put(sCertAuth, ca);
70                     Result<Void> r = context.requestCert(trans, req, resp, ca);
71                     if (r.isOK()) {
72                         resp.setStatus(HttpStatus.OK_200);
73                     } else {
74                         context.error(trans,resp,r);
75                     }
76                 }
77             }
78         });
79
80         aafCM.route(HttpMethods.GET,"/cert/:ca/personal",API.CERT,new Code(aafCM,"Request Personal Certificate") {
81             @Override
82             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
83                 String key = pathParam(req, ":ca");
84                 CA ca;
85                 if ((ca = aafCM.getCA(key))==null) {
86                     context.error(trans,resp,Result.ERR_BadData,"CA %s is not supported",key);
87                 } else {
88                     trans.put(sCertAuth, ca);
89                     Result<Void> r = context.requestPersonalCert(trans, req, resp, ca);
90                     if (r.isOK()) {
91                         resp.setStatus(HttpStatus.OK_200);
92                     } else {
93                         context.error(trans,resp,r);
94                     }
95                 }
96             }
97         });
98
99
100         /**
101          *
102          */
103         aafCM.route(HttpMethods.GET, "/cert/may/:perm", API.VOID, new Code(aafCM,"Check Permission") {
104             @Override
105             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
106                 Result<Void> r = context.check(trans, resp, pathParam(req,"perm"));
107                 if (r.isOK()) {
108                     resp.setStatus(HttpStatus.OK_200);
109                 } else {
110                     trans.checkpoint(r.errorString());
111                     context.error(trans,resp,Result.err(Result.ERR_Denied,"%s does not have Permission.",trans.user()));
112                 }
113             }
114         });
115
116         /**
117          * Get Cert by ID and Machine
118          */
119
120
121         /**
122          * Get Certs by ID
123          */
124         aafCM.route(HttpMethods.GET, "/cert/id/:id", API.CERT, new Code(aafCM,"GetByID") {
125             @Override
126             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
127                 Result<Void> r = context.readCertsByMechID(trans, resp, pathParam(req,"id"));
128                 if (r.isOK()) {
129                     resp.setStatus(HttpStatus.OK_200);
130                 } else {
131                     context.error(trans,resp,r);
132                 }
133             }
134         });
135
136
137         /**
138          * Get Certs by Machine
139          */
140
141     }
142 }