From 957d9ef4438590c77b64b8522ed38ceec2910cc9 Mon Sep 17 00:00:00 2001 From: luorijun Date: Tue, 13 May 2025 09:28:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20ProtectBuilder=20=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E8=AE=A4=E8=AF=81=E9=80=BB=E8=BE=91=E7=BC=96=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/auth/authenticate.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/web/auth/authenticate.go b/web/auth/authenticate.go index 2df1cd8..1a9613f 100644 --- a/web/auth/authenticate.go +++ b/web/auth/authenticate.go @@ -14,6 +14,30 @@ import ( "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) { // 获取令牌 var header = c.Get("Authorization")