Skip to content

Commit c6a2ae3

Browse files
committed
reworked revocation of certificates
1 parent 339c574 commit c6a2ae3

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

cmd/certmaker/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func setupRoutes(cfg *configuration.AppConfig, logger *logrus.Entry, dbSvc *dbse
196196
apiRouter.HandleFunc("/root-certificate/obtain", bh.APIRootCertificateDownloadHandler).Methods(http.MethodGet)
197197
apiRouter.HandleFunc("/certificate/request-with-simplerequest", bh.APIRequestCertificateWithSimpleRequestHandler).Methods(http.MethodPost)
198198
apiRouter.HandleFunc("/certificate/request-with-csr", bh.APIRequestCertificateWithCSRHandler).Methods(http.MethodPost)
199-
apiRouter.HandleFunc("/certificate/{sn}/revoke", bh.APIRevokeCertificateHandler).Methods(http.MethodGet)
199+
apiRouter.HandleFunc("/certificate/revoke", bh.APIRevokeCertificateHandler).Methods(http.MethodPost)
200200
// apiRouter.HandleFunc("/certificate/{id}/obtain", bh.APIObtainCertificateHandler).Methods(http.MethodGet)
201201
// apiRouter.HandleFunc("/privatekey/{id}/obtain", bh.APIObtainPrivateKeyHandler).Methods(http.MethodGet)
202202
apiRouter.HandleFunc("/http-01/{challengeID}/solve", bh.APISolveHTTP01ChallengeHandler).Methods(http.MethodPost)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package entity
2+
3+
type RevocationRequest struct {
4+
SerialNumber uint64
5+
Reason string
6+
}

internal/handler/api.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,18 +853,25 @@ func (bh *BaseHandler) APIRevokeCertificateHandler(w http.ResponseWriter, r *htt
853853
var (
854854
logger = bh.ContextLogger("api")
855855
u = r.Context().Value("user").(*entity.User)
856-
vars = mux.Vars(r)
857856
)
858857

859-
ci, err := bh.DBSvc.FindCertInfo("serial_number = ?", vars["sn"])
858+
var revocationRequest entity.RevocationRequest
859+
err := json.NewDecoder(r.Body).Decode(&revocationRequest)
860+
if err != nil {
861+
logger.Debugf("could not decode request body: %s", err.Error())
862+
w.WriteHeader(http.StatusBadRequest)
863+
return
864+
}
865+
866+
ci, err := bh.DBSvc.FindCertInfo("serial_number = ?", revocationRequest.SerialNumber)
860867
if err != nil {
861868
logger.Debugf("could not find certinfo: %s", err.Error())
862869
w.WriteHeader(http.StatusBadRequest)
863870
return
864871
}
865872

866873
if ci.CreatedForUser != u.ID && !u.Admin {
867-
logger.Debugf("this is not your certificate")
874+
logger.Debugf("This certificate was not issued to the requesting user %d (%s)", u.ID, u.Username)
868875
w.WriteHeader(http.StatusForbidden)
869876
return
870877
}
@@ -881,7 +888,7 @@ func (bh *BaseHandler) APIRevokeCertificateHandler(w http.ResponseWriter, r *htt
881888
err = bh.DBSvc.UpdateCertInfo(&ci)
882889
if err != nil {
883890
logger.Debugf("could not update certinfo: %s", err.Error())
884-
w.WriteHeader(http.StatusBadRequest)
891+
w.WriteHeader(http.StatusInternalServerError)
885892
return
886893
}
887894
}

0 commit comments

Comments
 (0)