mirror of
https://github.com/smallstep/certificates.git
synced 2024-10-31 03:20:16 +00:00
21 lines
580 B
Go
21 lines
580 B
Go
package userid
|
|
|
|
import "context"
|
|
|
|
type contextKey struct{}
|
|
|
|
// NewContext returns a new context with the given user ID added to the
|
|
// context.
|
|
// TODO(hs): this doesn't seem to be used / set currently; implement
|
|
// when/where it makes sense.
|
|
func NewContext(ctx context.Context, userID string) context.Context {
|
|
return context.WithValue(ctx, contextKey{}, userID)
|
|
}
|
|
|
|
// FromContext returns the user ID from the context if it exists
|
|
// and is not empty.
|
|
func FromContext(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(contextKey{}).(string)
|
|
return v, ok && v != ""
|
|
}
|