middleware.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/go-chi/chi"
  5. "go.step.sm/linkedca"
  6. "github.com/smallstep/certificates/api/render"
  7. "github.com/smallstep/certificates/authority/admin"
  8. "github.com/smallstep/certificates/authority/admin/db/nosql"
  9. "github.com/smallstep/certificates/authority/provisioner"
  10. )
  11. // requireAPIEnabled is a middleware that ensures the Administration API
  12. // is enabled before servicing requests.
  13. func (h *Handler) requireAPIEnabled(next http.HandlerFunc) http.HandlerFunc {
  14. return func(w http.ResponseWriter, r *http.Request) {
  15. if !h.auth.IsAdminAPIEnabled() {
  16. render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  17. "administration API not enabled"))
  18. return
  19. }
  20. next(w, r)
  21. }
  22. }
  23. // extractAuthorizeTokenAdmin is a middleware that extracts and caches the bearer token.
  24. func (h *Handler) extractAuthorizeTokenAdmin(next http.HandlerFunc) http.HandlerFunc {
  25. return func(w http.ResponseWriter, r *http.Request) {
  26. tok := r.Header.Get("Authorization")
  27. if tok == "" {
  28. render.Error(w, admin.NewError(admin.ErrorUnauthorizedType,
  29. "missing authorization header token"))
  30. return
  31. }
  32. adm, err := h.auth.AuthorizeAdminToken(r, tok)
  33. if err != nil {
  34. render.Error(w, err)
  35. return
  36. }
  37. ctx := linkedca.NewContextWithAdmin(r.Context(), adm)
  38. next(w, r.WithContext(ctx))
  39. }
  40. }
  41. // loadProvisioner is a middleware that searches for a provisioner
  42. // by name and stores it in the context.
  43. func (h *Handler) loadProvisionerByName(next http.HandlerFunc) http.HandlerFunc {
  44. return func(w http.ResponseWriter, r *http.Request) {
  45. ctx := r.Context()
  46. name := chi.URLParam(r, "provisionerName")
  47. var (
  48. p provisioner.Interface
  49. err error
  50. )
  51. if p, err = h.auth.LoadProvisionerByName(name); err != nil {
  52. render.Error(w, admin.WrapErrorISE(err, "error loading provisioner %s", name))
  53. return
  54. }
  55. prov, err := h.adminDB.GetProvisioner(ctx, p.GetID())
  56. if err != nil {
  57. render.Error(w, err)
  58. return
  59. }
  60. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  61. next(w, r.WithContext(ctx))
  62. }
  63. }
  64. // checkAction checks if an action is supported in standalone or not
  65. func (h *Handler) checkAction(next http.HandlerFunc, supportedInStandalone bool) http.HandlerFunc {
  66. return func(w http.ResponseWriter, r *http.Request) {
  67. // actions allowed in standalone mode are always supported
  68. if supportedInStandalone {
  69. next(w, r)
  70. return
  71. }
  72. // when an action is not supported in standalone mode and when
  73. // using a nosql.DB backend, actions are not supported
  74. if _, ok := h.adminDB.(*nosql.DB); ok {
  75. render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  76. "operation not supported in standalone mode"))
  77. return
  78. }
  79. // continue to next http handler
  80. next(w, r)
  81. }
  82. }