mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 15:26:40 +00:00
39 lines
732 B
Go
39 lines
732 B
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type requestContextKey string
|
|
|
|
const (
|
|
authMethodKey requestContextKey = "authMethod"
|
|
authUserKey requestContextKey = "authUser"
|
|
)
|
|
|
|
func withAuthMethod(ctx context.Context, method Method) context.Context {
|
|
return context.WithValue(ctx, authMethodKey, method)
|
|
}
|
|
|
|
func MethodFromContext(ctx context.Context) Method {
|
|
v := ctx.Value(authMethodKey)
|
|
method, ok := v.(Method)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return method
|
|
}
|
|
|
|
func withAuthUser(ctx context.Context, userId string) context.Context {
|
|
return context.WithValue(ctx, authUserKey, userId)
|
|
}
|
|
|
|
func UserFromContext(ctx context.Context) string {
|
|
v := ctx.Value(authUserKey)
|
|
userId, ok := v.(string)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return userId
|
|
}
|