Sfoglia il codice sorgente

Make step handler backward compatible

Mariano Cano 4 anni fa
parent
commit
d51c6b7d83
2 ha cambiato i file con 27 aggiunte e 8 eliminazioni
  1. 27 7
      scep/api/api.go
  2. 0 1
      scep/authority.go

+ 27 - 7
scep/api/api.go

@@ -48,28 +48,48 @@ type Response struct {
 }
 
 // handler is the SCEP request handler.
-type handler struct{}
+type handler struct {
+	auth *scep.Authority
+}
 
 // Route traffic and implement the Router interface.
 //
 // Deprecated: use scep.Route(r api.Router)
 func (h *handler) Route(r api.Router) {
-	Route(r)
+	route(r, func(next http.HandlerFunc) http.HandlerFunc {
+		return func(w http.ResponseWriter, r *http.Request) {
+			ctx := scep.NewContext(r.Context(), h.auth)
+			next(w, r.WithContext(ctx))
+		}
+	})
 }
 
 // New returns a new SCEP API router.
 //
 // Deprecated: use scep.Route(r api.Router)
 func New(auth *scep.Authority) api.RouterHandler {
-	return &handler{}
+	return &handler{auth: auth}
 }
 
 // Route traffic and implement the Router interface.
 func Route(r api.Router) {
-	r.MethodFunc(http.MethodGet, "/{provisionerName}/*", lookupProvisioner(Get))
-	r.MethodFunc(http.MethodGet, "/{provisionerName}", lookupProvisioner(Get))
-	r.MethodFunc(http.MethodPost, "/{provisionerName}/*", lookupProvisioner(Post))
-	r.MethodFunc(http.MethodPost, "/{provisionerName}", lookupProvisioner(Post))
+	route(r, nil)
+}
+
+func route(r api.Router, middleware func(next http.HandlerFunc) http.HandlerFunc) {
+	getHandler := lookupProvisioner(Get)
+	postHandler := lookupProvisioner(Post)
+
+	// For backward compatibility.
+	if middleware != nil {
+		getHandler = middleware(getHandler)
+		postHandler = middleware(postHandler)
+	}
+
+	r.MethodFunc(http.MethodGet, "/{provisionerName}/*", getHandler)
+	r.MethodFunc(http.MethodGet, "/{provisionerName}", getHandler)
+	r.MethodFunc(http.MethodPost, "/{provisionerName}/*", postHandler)
+	r.MethodFunc(http.MethodPost, "/{provisionerName}", postHandler)
 }
 
 // Get handles all SCEP GET requests

+ 0 - 1
scep/authority.go

@@ -453,7 +453,6 @@ func (a *Authority) CreateFailureResponse(ctx context.Context, csr *x509.Certifi
 
 // MatchChallengePassword verifies a SCEP challenge password
 func (a *Authority) MatchChallengePassword(ctx context.Context, password string) (bool, error) {
-
 	p, err := provisionerFromContext(ctx)
 	if err != nil {
 		return false, err