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

[Question] middleware set context not work in stream rpc #750

Closed
solarhell opened this issue Jun 5, 2024 · 2 comments
Closed

[Question] middleware set context not work in stream rpc #750

solarhell opened this issue Jun 5, 2024 · 2 comments

Comments

@solarhell
Copy link

solarhell commented Jun 5, 2024

my middleware

package middleware

import (
	"context"
	"fmt"

	"connectrpc.com/connect"
	"pkg/ctxuser"
)

func NewAuthInterceptor() connect.UnaryInterceptorFunc {
	interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
		return func(
			ctx context.Context,
			req connect.AnyRequest,
		) (connect.AnyResponse, error) {
			tokenString := req.Header().Get("token")
			if tokenString == "" {
				return nil, connect.NewError(connect.CodeUnauthenticated, fmt.Errorf("token is empty"))
			}

			userID, err := getUserIDFromToken(tokenString)
			...

			ctx = ctxuser.SetInContext(ctx, ctxuser.RequestInfo{
				UserID: userID,
			})

			return next(ctx, req)
		}
	}
	return interceptor
}
package ctxuser

import (
	"context"
)

type ctxMarker struct{}

var (
	ctxMarkerKey = &ctxMarker{}
)

type RequestInfo struct {
	RequestID string

	UserID string
}

func Extract(ctx context.Context) (RequestInfo, bool) {
	info, ok := ctx.Value(ctxMarkerKey).(RequestInfo)
	return info, ok
}

func SetInContext(ctx context.Context, info RequestInfo) context.Context {
	return context.WithValue(ctx, ctxMarkerKey, info)
}

in my api, i use token in http header to check if user is logged in.
and i use middleware to set the context, so i can get user_id easily in handler.

But in such rpc handler i can't get the user_id.

my proto

  rpc AskGPT(AskGPTRequest) returns(stream AskGPTResponse);

Do i need to change my middleware code? Thanks.

@solarhell solarhell changed the title [Question] How to use middleware check in stream rpc [Question] middleware set context not work in stream rpc Jun 5, 2024
@emcfarlane
Copy link
Contributor

emcfarlane commented Jun 5, 2024

The context code looks correct, however (stream AskGPTResponse); requires you implement the full Interceptor interface. The current interceptor will only work on unary, it doesn't cover streaming use cases.

Would recommend to look at the authn-go library: https://github.com/connectrpc/authn-go/tree/main
This will correctly handle setting the context value in the request and has some security benefits doing this as HTTP middleware.

@solarhell
Copy link
Author

After implemented the WrapStreamingHandler(StreamingHandlerFunc) StreamingHandlerFunc, it works fine now.

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

2 participants