mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-22 10:56:26 +00:00
25 lines
442 B
Go
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
|
|
}
|