From 85946bfbcac443f5ebe80256544d4b8b5e38f5b9 Mon Sep 17 00:00:00 2001 From: KynixInHK Date: Tue, 9 Jul 2024 16:51:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=87jwt=E4=B8=AD=E6=AC=8A=E9=99=90?= =?UTF-8?q?=E6=95=B8=E5=AD=97=E6=AA=A2=E6=9F=A5=E5=B7=A5=E5=85=B7=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=AF=84=E5=9C=8D=E6=94=BE=E9=96=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- am_jwt/jwt.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/am_jwt/jwt.go b/am_jwt/jwt.go index 8f61707..5827eef 100644 --- a/am_jwt/jwt.go +++ b/am_jwt/jwt.go @@ -46,7 +46,7 @@ const ( ) // 身份字段转换权限等级数字 -func claimToRole(claim string) int { +func ClaimToRole(claim string) int { switch claim { case "root": return ROOTROLE @@ -192,12 +192,12 @@ func ParseToken(token string, roleRequired int, secret string) (*TokenClaims, er } // 验证权限合法性 - if claimToRole(claims.Role) >= roleRequired { // 如果提供的token权限验证大于所需权限,初步判断通过 - if roleRequired == 1 && claimToRole(claims.Role) == 2 { // user权限无权新增用户 + if ClaimToRole(claims.Role) >= roleRequired { // 如果提供的token权限验证大于所需权限,初步判断通过 + if roleRequired == 1 && ClaimToRole(claims.Role) == 2 { // user权限无权新增用户 return nil, InvalidRoleError } - if claimToRole(claims.Role) == 1 { // temp权限仅用于注册和重设密码临时使用,一经使用立即灭活 + if ClaimToRole(claims.Role) == 1 { // temp权限仅用于注册和重设密码临时使用,一经使用立即灭活 _, err := Kickoff(token) if err != nil { return nil, err @@ -252,7 +252,7 @@ func JWTAuthMiddleware(role string, secret string) func(c *gin.Context) { } // 开始认证 - claims, err := ParseToken(parts[1], claimToRole(role), secret) + claims, err := ParseToken(parts[1], ClaimToRole(role), secret) if err != nil { if errors.Is(err, ExpiredButCanSaveError) { // 如果还可以救一救 tokenClaims := TokenClaims{