grpc/auth/context.go

25 lines
442 B
Go
Raw Normal View History

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
}