You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/vendor/github.com/qiniu/go-sdk/v7/auth/context.go

37 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
}