新增 ProtectBuilder 简化认证逻辑编写
This commit is contained in:
@@ -14,6 +14,30 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ProtectBuilder struct {
|
||||||
|
c *fiber.Ctx
|
||||||
|
types []PayloadType
|
||||||
|
scopes []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewProtect(c *fiber.Ctx) *ProtectBuilder {
|
||||||
|
return &ProtectBuilder{c, []PayloadType{}, []string{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProtectBuilder) Payload(types ...PayloadType) *ProtectBuilder {
|
||||||
|
p.types = types
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProtectBuilder) Scopes(scopes ...string) *ProtectBuilder {
|
||||||
|
p.scopes = scopes
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProtectBuilder) Do() (*Context, error) {
|
||||||
|
return Protect(p.c, p.types, p.scopes)
|
||||||
|
}
|
||||||
|
|
||||||
func Protect(c *fiber.Ctx, types []PayloadType, permissions []string) (*Context, error) {
|
func Protect(c *fiber.Ctx, types []PayloadType, permissions []string) (*Context, error) {
|
||||||
// 获取令牌
|
// 获取令牌
|
||||||
var header = c.Get("Authorization")
|
var header = c.Get("Authorization")
|
||||||
|
|||||||
Reference in New Issue
Block a user