代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。