handler.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // Provisioners
  36. r.MethodFunc("GET", "/provisioners/{name}", authnz(GetProvisioner))
  37. r.MethodFunc("GET", "/provisioners", authnz(GetProvisioners))
  38. r.MethodFunc("POST", "/provisioners", authnz(CreateProvisioner))
  39. r.MethodFunc("PUT", "/provisioners/{name}", authnz(UpdateProvisioner))
  40. r.MethodFunc("DELETE", "/provisioners/{name}", authnz(DeleteProvisioner))
  41. // Admins
  42. r.MethodFunc("GET", "/admins/{id}", authnz(GetAdmin))
  43. r.MethodFunc("GET", "/admins", authnz(GetAdmins))
  44. r.MethodFunc("POST", "/admins", authnz(CreateAdmin))
  45. r.MethodFunc("PATCH", "/admins/{id}", authnz(UpdateAdmin))
  46. r.MethodFunc("DELETE", "/admins/{id}", authnz(DeleteAdmin))
  47. // ACME External Account Binding Keys
  48. r.MethodFunc("GET", "/acme/eab/{provisionerName}/{reference}", authnz(requireEABEnabled(acmeResponder.GetExternalAccountKeys)))
  49. r.MethodFunc("GET", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(acmeResponder.GetExternalAccountKeys)))
  50. r.MethodFunc("POST", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(acmeResponder.CreateExternalAccountKey)))
  51. r.MethodFunc("DELETE", "/acme/eab/{provisionerName}/{id}", authnz(requireEABEnabled(acmeResponder.DeleteExternalAccountKey)))
  52. }