grpc/auth/context.go
Adphi e5d84975a2
wip: add token & session forwarding
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2024-10-23 14:00:17 +02:00

25 lines
442 B
Go

package auth
import (
"context"
)
type authKey struct{}
func Context[T any](ctx context.Context, auth T) context.Context {
return context.WithValue(ctx, authKey{}, auth)
}
func FromContext[T any](ctx context.Context) (T, bool) {
auth, ok := ctx.Value(authKey{}).(T)
return auth, ok
}
func MustTokenFromContext[T any](ctx context.Context) T {
auth, ok := FromContext[T](ctx)
if !ok {
panic("no auth in context")
}
return auth
}