forked from TrueCloudLab/certificates
Move acme context middleware to deprecated handler
This commit is contained in:
parent
d51c6b7d83
commit
2ea0c70344
1 changed files with 26 additions and 31 deletions
|
@ -79,12 +79,29 @@ type handler struct {
|
||||||
opts *HandlerOptions
|
opts *HandlerOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
// Route traffic and implement the Router interface.
|
// Route traffic and implement the Router interface. For backward compatibility
|
||||||
|
// this route adds will add a new middleware that will set the ACME components
|
||||||
|
// on the context.
|
||||||
|
//
|
||||||
|
// Deprecated: use api.Route(r api.Router)
|
||||||
func (h *handler) Route(r api.Router) {
|
func (h *handler) Route(r api.Router) {
|
||||||
route(r, h.opts)
|
client := acme.NewClient()
|
||||||
|
linker := acme.NewLinker(h.opts.DNS, h.opts.Prefix)
|
||||||
|
route(r, func(next nextHTTP) nextHTTP {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
if ca, ok := h.opts.CA.(*authority.Authority); ok && ca != nil {
|
||||||
|
ctx = authority.NewContext(ctx, ca)
|
||||||
|
}
|
||||||
|
ctx = acme.NewContext(ctx, h.opts.DB, client, linker, h.opts.PrerequisitesChecker)
|
||||||
|
next(w, r.WithContext(ctx))
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler returns a new ACME API handler.
|
// NewHandler returns a new ACME API handler.
|
||||||
|
//
|
||||||
|
// Deprecated: use api.Route(r api.Router)
|
||||||
func NewHandler(opts HandlerOptions) api.RouterHandler {
|
func NewHandler(opts HandlerOptions) api.RouterHandler {
|
||||||
return &handler{
|
return &handler{
|
||||||
opts: &opts,
|
opts: &opts,
|
||||||
|
@ -98,40 +115,18 @@ func Route(r api.Router) {
|
||||||
route(r, nil)
|
route(r, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func route(r api.Router, opts *HandlerOptions) {
|
func route(r api.Router, middleware func(next nextHTTP) nextHTTP) {
|
||||||
var withContext func(next nextHTTP) nextHTTP
|
|
||||||
|
|
||||||
// For backward compatibility this block adds will add a new middleware that
|
|
||||||
// will set the ACME components to the context.
|
|
||||||
if opts != nil {
|
|
||||||
client := acme.NewClient()
|
|
||||||
linker := acme.NewLinker(opts.DNS, opts.Prefix)
|
|
||||||
|
|
||||||
withContext = func(next nextHTTP) nextHTTP {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
ctx := r.Context()
|
|
||||||
if ca, ok := opts.CA.(*authority.Authority); ok && ca != nil {
|
|
||||||
ctx = authority.NewContext(ctx, ca)
|
|
||||||
}
|
|
||||||
ctx = acme.NewContext(ctx, opts.DB, client, linker, opts.PrerequisitesChecker)
|
|
||||||
next(w, r.WithContext(ctx))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
withContext = func(next nextHTTP) nextHTTP {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
next(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
commonMiddleware := func(next nextHTTP) nextHTTP {
|
commonMiddleware := func(next nextHTTP) nextHTTP {
|
||||||
return withContext(func(w http.ResponseWriter, r *http.Request) {
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Linker middleware gets the provisioner and current url from the
|
// Linker middleware gets the provisioner and current url from the
|
||||||
// request and sets them in the context.
|
// request and sets them in the context.
|
||||||
linker := acme.MustLinkerFromContext(r.Context())
|
linker := acme.MustLinkerFromContext(r.Context())
|
||||||
linker.Middleware(http.HandlerFunc(checkPrerequisites(next))).ServeHTTP(w, r)
|
linker.Middleware(http.HandlerFunc(checkPrerequisites(next))).ServeHTTP(w, r)
|
||||||
})
|
}
|
||||||
|
if middleware != nil {
|
||||||
|
handler = middleware(handler)
|
||||||
|
}
|
||||||
|
return handler
|
||||||
}
|
}
|
||||||
validatingMiddleware := func(next nextHTTP) nextHTTP {
|
validatingMiddleware := func(next nextHTTP) nextHTTP {
|
||||||
return commonMiddleware(addNonce(addDirLink(verifyContentType(parseJWS(validateJWS(next))))))
|
return commonMiddleware(addNonce(addDirLink(verifyContentType(parseJWS(validateJWS(next))))))
|
||||||
|
|
Loading…
Reference in a new issue