acme.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/smallstep/certificates/api"
  5. "github.com/smallstep/certificates/authority/admin"
  6. "go.step.sm/linkedca"
  7. )
  8. // CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
  9. type CreateExternalAccountKeyRequest struct {
  10. ProvisionerName string `json:"provisioner"`
  11. Name string `json:"name"`
  12. }
  13. // Validate validates a new-admin request body.
  14. func (r *CreateExternalAccountKeyRequest) Validate() error {
  15. if r.ProvisionerName == "" {
  16. return admin.NewError(admin.ErrorBadRequestType, "provisioner name cannot be empty")
  17. }
  18. if r.Name == "" {
  19. return admin.NewError(admin.ErrorBadRequestType, "name / reference cannot be empty")
  20. }
  21. return nil
  22. }
  23. // GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
  24. type GetExternalAccountKeysResponse struct {
  25. EAKs []*linkedca.EABKey `json:"eaks"`
  26. NextCursor string `json:"nextCursor"`
  27. }
  28. // CreateExternalAccountKey creates a new External Account Binding key
  29. func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  30. var body CreateExternalAccountKeyRequest
  31. if err := api.ReadJSON(r.Body, &body); err != nil { // TODO: rewrite into protobuf json (likely)
  32. api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
  33. return
  34. }
  35. if err := body.Validate(); err != nil {
  36. api.WriteError(w, err)
  37. return
  38. }
  39. eak, err := h.acmeDB.CreateExternalAccountKey(r.Context(), body.ProvisionerName, body.Name)
  40. if err != nil {
  41. api.WriteError(w, admin.WrapErrorISE(err, "error creating external account key %s", body.Name))
  42. return
  43. }
  44. response := &linkedca.EABKey{
  45. EabKid: eak.ID,
  46. EabHmacKey: eak.KeyBytes,
  47. ProvisionerName: eak.ProvisionerName,
  48. Name: eak.Name,
  49. }
  50. api.ProtoJSONStatus(w, response, http.StatusCreated)
  51. }
  52. // GetExternalAccountKeys returns a segment of ACME EAB Keys.
  53. func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request) {
  54. // cursor, limit, err := api.ParseCursor(r)
  55. // if err != nil {
  56. // api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err,
  57. // "error parsing cursor and limit from query params"))
  58. // return
  59. // }
  60. // eaks, nextCursor, err := h.acmeDB.GetExternalAccountKeys(cursor, limit)
  61. // if err != nil {
  62. // api.WriteError(w, admin.WrapErrorISE(err, "error retrieving paginated admins"))
  63. // return
  64. // }
  65. // api.JSON(w, &GetExternalAccountKeysResponse{
  66. // EAKs: eaks,
  67. // NextCursor: nextCursor,
  68. // })
  69. }