handler.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package api
  2. import (
  3. "context"
  4. "github.com/smallstep/certificates/acme"
  5. "github.com/smallstep/certificates/api"
  6. "github.com/smallstep/certificates/authority"
  7. "github.com/smallstep/certificates/authority/admin"
  8. )
  9. // Handler is the Admin API request handler.
  10. type Handler struct {
  11. acmeResponder acmeAdminResponderInterface
  12. }
  13. // Route traffic and implement the Router interface.
  14. //
  15. // Deprecated: use Route(r api.Router, acmeResponder acmeAdminResponderInterface)
  16. func (h *Handler) Route(r api.Router) {
  17. Route(r, h.acmeResponder)
  18. }
  19. // NewHandler returns a new Authority Config Handler.
  20. //
  21. // Deprecated: use Route(r api.Router, acmeResponder acmeAdminResponderInterface)
  22. func NewHandler(auth adminAuthority, adminDB admin.DB, acmeDB acme.DB, acmeResponder acmeAdminResponderInterface) api.RouterHandler {
  23. return &Handler{
  24. acmeResponder: acmeResponder,
  25. }
  26. }
  27. var mustAuthority = func(ctx context.Context) adminAuthority {
  28. return authority.MustFromContext(ctx)
  29. }
  30. // Route traffic and implement the Router interface.
  31. func Route(r api.Router, acmeResponder acmeAdminResponderInterface) {
  32. authnz := func(next nextHTTP) nextHTTP {
  33. return extractAuthorizeTokenAdmin(requireAPIEnabled(next))
  34. }
  35. requireEABEnabled := func(next nextHTTP) nextHTTP {
  36. return requireEABEnabled(next)
  37. }
  38. // Provisioners
  39. r.MethodFunc("GET", "/provisioners/{name}", authnz(GetProvisioner))
  40. r.MethodFunc("GET", "/provisioners", authnz(GetProvisioners))
  41. r.MethodFunc("POST", "/provisioners", authnz(CreateProvisioner))
  42. r.MethodFunc("PUT", "/provisioners/{name}", authnz(UpdateProvisioner))
  43. r.MethodFunc("DELETE", "/provisioners/{name}", authnz(DeleteProvisioner))
  44. // Admins
  45. r.MethodFunc("GET", "/admins/{id}", authnz(GetAdmin))
  46. r.MethodFunc("GET", "/admins", authnz(GetAdmins))
  47. r.MethodFunc("POST", "/admins", authnz(CreateAdmin))
  48. r.MethodFunc("PATCH", "/admins/{id}", authnz(UpdateAdmin))
  49. r.MethodFunc("DELETE", "/admins/{id}", authnz(DeleteAdmin))
  50. // ACME External Account Binding Keys
  51. r.MethodFunc("GET", "/acme/eab/{provisionerName}/{reference}", authnz(requireEABEnabled(acmeResponder.GetExternalAccountKeys)))
  52. r.MethodFunc("GET", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(acmeResponder.GetExternalAccountKeys)))
  53. r.MethodFunc("POST", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(acmeResponder.CreateExternalAccountKey)))
  54. r.MethodFunc("DELETE", "/acme/eab/{provisionerName}/{id}", authnz(requireEABEnabled(acmeResponder.DeleteExternalAccountKey)))
  55. }