1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
auth.go 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-11-22 16:57 . jwt,支持原始数据,不需要base64
package jwt
import (
"gitee.com/h79/goutils/auth/token"
"gitee.com/h79/goutils/common/secret"
)
// 保证 Authenticate struct implement token.Authenticate
var _ token.Authenticate = (*Authenticate)(nil)
type Authenticate struct {
secret token.Secret
}
func New(secret token.Secret) *Authenticate {
return &Authenticate{secret: secret}
}
const Type = "jwt"
func (t *Authenticate) Type() string {
return Type
}
// Create implement token.Authenticate interface
func (t *Authenticate) Create(source token.Source, expireSeconds int64, opts ...token.Option) (token.Token, error) {
sec := t.secret.GetSecret(source)
if expireSeconds <= 0 {
expireSeconds = sec.Expire
}
st := secret.Secret{
Value: sec.Value,
Expire: expireSeconds,
}
return NewToken(&st, base64Enabled(opts...)).WithSource(source), nil
}
// Decode implement token.Authenticate interface
func (t *Authenticate) Decode(source token.Source, tok string, opts ...token.Option) (token.Token, error) {
if base64Enabled(opts...) {
return DecodeToken(tok)
}
return DecodeTokenNoBase64(tok)
}
// Check implement token.Authenticate interface
func (t *Authenticate) Check(source token.Source, tok string, opts ...token.Option) (token.Token, error) {
sec := t.secret.GetSecret(source)
if base64Enabled(opts...) {
return VerifyToken(tok, &sec)
}
return VerifyTokenNoBase64(tok, &sec)
}
// SetEnabled implement token.Authenticate interface
func (t *Authenticate) SetEnabled(source token.Source, enable bool) {
t.secret.SetEnabled(source, enable)
}
// EnableFlag implement token.Authenticate interface
func (t *Authenticate) EnableFlag(source token.Source) int {
return t.secret.EnableFlag(source)
}
func base64Enabled(opts ...token.Option) bool {
if len(opts) == 0 {
// 默认base64
return true
}
for i := range opts {
if opts[i].Base64 {
return true
}
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.2.21

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385