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

Forwarding headers through the middleware not working #398

Open
spa5k opened this issue Apr 21, 2024 · 5 comments
Open

Forwarding headers through the middleware not working #398

spa5k opened this issue Apr 21, 2024 · 5 comments
Labels
question Further information is requested

Comments

@spa5k
Copy link

spa5k commented Apr 21, 2024

So I have a small middleware that reads the cookie and writes a "User-Id" header through the context, but when I try to read the same header in the handler or in the next middleware, it remains empty

func UserAuthenticationMiddleware(tokenService *token.Manager) func(ctx huma.Context, next func(huma.Context)) {
	return func(ctx huma.Context, next func(huma.Context)) {
		tokenCookie, err := huma.ReadCookie(ctx, "review_hq_token")
		...
		userId, err := tokenService.VerifyToken(tokenCookie.Value)
		if err != nil {
			ctx.SetStatus(401)
			ctx.BodyWriter().Write([]byte(err.Error()))
			return
		}

		ctx.SetHeader("User-Id", userId)

		ctx = huma.WithValue(ctx, "User-Id", userId)

		next(ctx)
	}
}
huma.Register(api, huma.Operation{
		Path:        path,
		Method:      "POST",
		Tags:        []string{"Review"},
		OperationID: "create-review",
		Middlewares: huma.Middlewares{
			middleware.UserAuthenticationMiddleware(tokenService),
			func(ctx huma.Context, next func(huma.Context)) {
				// This is empty
				println("userid from middleware", ctx.Header("User-Id"))
			},
		},
	}, func(ctx context.Context, i *CreateReviewRequest) (*createReviewResponse, error) {
		// first is empty, second works
		println("reviewUserID", i.UserId)
		println("reviewUserID", ctx.Value("User-Id").(string))

Passing values through context is working as expected.

@x-user
Copy link
Contributor

x-user commented Apr 21, 2024

It's because huma.Context.Header reads request headers, but huma.Context.SetHeader sets response headers.

@spa5k
Copy link
Author

spa5k commented Apr 21, 2024

So this set header is only for response? I can't update the headers of the request when it reaches Middleware?

@x-user
Copy link
Contributor

x-user commented Apr 21, 2024

Yes. At least for now you can only do this in router own middleware.

@danielgtaylor
Copy link
Owner

@spa5k if you must override headers in router-agnostic middleware, you can create your own struct which wraps huma.Context and override yourStruct.Header(name string) string to return the values you want. Otherwise, like @x-user says you can do this in the underlying router's own middlewares.

@danielgtaylor danielgtaylor added the question Further information is requested label Apr 22, 2024
@spa5k
Copy link
Author

spa5k commented Apr 30, 2024

imo, it should forward the headers naturally, how do other framework/languages handle it?

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

No branches or pull requests

3 participants