Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to support request.WithContext() within the AuthenticationFunc? #1023

Open
ottaviohartman opened this issue Oct 18, 2024 · 0 comments

Comments

@ottaviohartman
Copy link

Within the AuthenticationFunc, after I verify a JWT, I want to add claims to the request context.Context for downstream middleware and functions to use.

Is this possible with openapi3filter? I must be doing something wrong, because this is the only way I can get it to work:

// this does not work
input.RequestValidationInput.Request = input.RequestValidationInput.Request.WithContext(newCtx)

// this does work
*input.RequestValidationInput.Request = *input.RequestValidationInput.Request.WithContext(newCtx)

Relevant code:

func (v *Validator) Middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
route, pathParams, err := v.router.FindRoute(r)
if err != nil {
v.logFunc(ctx, "validation error: failed to find route for "+r.URL.String(), err)
v.errFunc(ctx, w, http.StatusNotFound, ErrCodeCannotFindRoute, err)
return
}
requestValidationInput := &RequestValidationInput{
Request: r,
PathParams: pathParams,
Route: route,
Options: &v.options,
}
if err = ValidateRequest(ctx, requestValidationInput); err != nil {
v.logFunc(ctx, "invalid request", err)
v.errFunc(ctx, w, http.StatusBadRequest, ErrCodeRequestInvalid, err)
return
}
var wr responseWrapper
if v.strict {
wr = &strictResponseWrapper{w: w}
} else {
wr = newWarnResponseWrapper(w)
}
h.ServeHTTP(wr, r)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant