1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
route.go 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-11-22 17:11 . jwt,支持原始数据,不需要base64
package auth
import (
"gitee.com/h79/goutils/auth/token"
"gitee.com/h79/goutils/common/result"
)
type authMap map[string]token.Authenticate
var (
_ token.Authenticate = (*route)(nil)
_ token.Route = (*route)(nil)
)
type route struct {
auths authMap
}
func (auth *route) HasAuth(method string) bool {
_, ok := auth.auths[method]
return ok
}
func (auth *route) Use(a token.Authenticate) token.Route {
auth.auths[a.Type()] = a
return auth
}
func (auth *route) Auth() token.Authenticate {
return auth
}
func (auth *route) Type() string {
return "engine"
}
func (auth *route) Create(source token.Source, expireSeconds int64, opts ...token.Option) (token.Token, error) {
item, err := auth.useAuth(source)
if err != nil {
return nil, err
}
return item.Create(source, expireSeconds, opts...)
}
func (auth *route) Decode(source token.Source, tok string, opts ...token.Option) (token.Token, error) {
item, err := auth.useAuth(source)
if err != nil {
return nil, err
}
return item.Decode(source, tok, opts...)
}
func (auth *route) Check(source token.Source, tok string, opts ...token.Option) (token.Token, error) {
item, err := auth.useAuth(source)
if err != nil {
return nil, err
}
return item.Check(source, tok, opts...)
}
func (auth *route) EnableFlag(source token.Source) int {
item, err := auth.useAuth(source)
if err != nil {
return 2
}
return item.EnableFlag(source)
}
func (auth *route) SetEnabled(source token.Source, enable bool) {
item, err := auth.useAuth(source)
if err != nil {
return
}
item.SetEnabled(source, enable)
}
func (auth *route) useAuth(source token.Source) (token.Authenticate, error) {
if len(auth.auths) <= 0 {
return nil, result.Error(result.ErrNil, "Not exist auth")
}
item, ok := auth.auths[source.Method]
if !ok {
return nil, result.Errorf(result.ErrNotFound, "NOT exist auth for source= '%+v'", source)
}
return item, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.3.6

搜索帮助

A270a887 8829481 3d7a4017 8829481