/
api_certificate.go
47 lines (36 loc) · 1.07 KB
/
api_certificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package api
import (
"net/http"
"github.com/fernandezvara/certsfor/pkg/client"
"github.com/fernandezvara/rest"
"github.com/julienschmidt/httprouter"
)
// putCertificate PUT /v1/ca/:caid/certificates/%s
func (a *API) putCertificate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var (
request client.APICertificateRequest
response client.Certificate
caID string = ps.ByName("caid")
cn string = ps.ByName("cn") // certificate common name
err error
)
err = rest.GetFromBody(r, &request)
if err != nil {
rest.BadRequest(w, r, "")
return
}
if cn == "ca" {
rest.ErrorResponse(w, http.StatusConflict, "CA Certificate cannot be overwritten")
return
}
if request.DN.CN == "" {
request.DN.CN = cn
}
if request.DN.CN != cn {
rest.ErrorResponse(w, http.StatusConflict, "Common Name on certificate does not match")
return
}
response.Request = request
response.CACertificate, response.Certificate, response.Key, err = a.srv.CertificateSet(r.Context(), caID, request)
rest.Response(w, response, err, http.StatusOK, "")
}