-
Notifications
You must be signed in to change notification settings - Fork 63
/
context.go
36 lines (30 loc) · 1.05 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package auth
import (
"context"
)
// MacContextKey 是用户的密钥信息
// context.Context中的键值不应该使用普通的字符串, 有可能导致命名冲突
type macContextKey struct{}
// tokenTypeKey 是签名算法类型key
type tokenTypeKey struct{}
// WithCredentials 返回一个包含密钥信息的context
func WithCredentials(ctx context.Context, cred *Credentials) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, macContextKey{}, cred)
}
// WithCredentialsType 返回一个context, 保存了密钥信息和token类型
func WithCredentialsType(ctx context.Context, cred *Credentials, t TokenType) context.Context {
ctx = WithCredentials(ctx, cred)
return context.WithValue(ctx, tokenTypeKey{}, t)
}
// CredentialsFromContext 从context获取密钥信息
func CredentialsFromContext(ctx context.Context) (cred *Credentials, t TokenType, ok bool) {
cred, ok = ctx.Value(macContextKey{}).(*Credentials)
t, yes := ctx.Value(tokenTypeKey{}).(TokenType)
if !yes {
t = TokenQBox
}
return
}