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] Auth middleware example #534

Open
hotrush opened this issue Feb 8, 2023 · 2 comments
Open

[Question] Auth middleware example #534

hotrush opened this issue Feb 8, 2023 · 2 comments

Comments

@hotrush
Copy link

hotrush commented Feb 8, 2023

Hello, checking auth middleware example (https://github.com/grpc-ecosystem/go-grpc-middleware/tree/master/auth#L36) and have several questions:

  • what does this line do? grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo))
  • also // WARNING: in production define your own type to avoid context collisions - do you suggest to define custom context type? but why? as i know it is a bad practice
@bwplotka bwplotka added the v1 label Mar 17, 2023
@bwplotka
Copy link
Collaborator

Hey,

  1. Adding token information to tags, so e.g. logger will use this info as field (if you add logging interceptor). Here is better example in upcoming v2 (https://github.com/grpc-ecosystem/go-grpc-middleware/blob/v2/interceptors/auth/examples_test.go#L41)
  2. This if you want further interceptors OR gRPC service code to use certain information about that token (e.g often it's needed to communicate with downstream services or so). From interceptor the only "request" state is through context, thus you need to create your own context key in some form. What exactly is bad practice? If you mean using context keys - I agree, but there is nothing better to put some state to the context.

@Multiply
Copy link

Multiply commented Mar 7, 2024

Using v2, if the auth interceptor is added after the logging interceptor, no auth.sub is available in the default grpc server logs.
When adding auth first, no logs will show if the auth function fails.

Wouldn't it make sense if PostCall somehow had access to the context of previous handlers/middlewares?

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

No branches or pull requests

3 participants