2020-09-03 00:12:34 -04:00
|
|
|
package ucan
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CtxKey defines a distinct type for context keys used by the access
|
|
|
|
|
// package
|
|
|
|
|
type CtxKey string
|
|
|
|
|
|
2020-12-04 10:49:03 -05:00
|
|
|
// TokenCtxKey is the key for adding an access UCAN to a context.Context
|
|
|
|
|
const TokenCtxKey CtxKey = "UCAN"
|
2020-09-03 00:12:34 -04:00
|
|
|
|
2020-12-04 10:49:03 -05:00
|
|
|
// CtxWithToken adds a UCAN value to a context
|
|
|
|
|
func CtxWithToken(ctx context.Context, t Token) context.Context {
|
|
|
|
|
return context.WithValue(ctx, TokenCtxKey, t)
|
2020-09-03 00:12:34 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-04 10:49:03 -05:00
|
|
|
// FromCtx extracts a token from a given context if one is set, returning nil
|
|
|
|
|
// otherwise
|
|
|
|
|
func FromCtx(ctx context.Context) *Token {
|
|
|
|
|
iface := ctx.Value(TokenCtxKey)
|
|
|
|
|
if ref, ok := iface.(*Token); ok {
|
2020-09-03 00:12:34 -04:00
|
|
|
return ref
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|