From 2393f09c209b2d81c2945de8886375eb87cf6fb1 Mon Sep 17 00:00:00 2001 From: zhuyasen Date: Sat, 2 Dec 2023 17:02:05 +0800 Subject: [PATCH] support get request_id in verify function --- pkg/gin/middleware/README.md | 4 ++-- pkg/gin/middleware/auth.go | 8 ++++---- pkg/gin/middleware/auth_test.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/gin/middleware/README.md b/pkg/gin/middleware/README.md index 283f2775..1db54ef2 100644 --- a/pkg/gin/middleware/README.md +++ b/pkg/gin/middleware/README.md @@ -85,7 +85,7 @@ func main() { r.Run(serverAddr) } -func adminVerify(claims *jwt.Claims, token string) error { +func adminVerify(claims *jwt.Claims, tokenTail10 string, c *gin.Context) error { if claims.Role != "admin" { return errors.New("verify failed") } @@ -120,7 +120,7 @@ func main() { r.Run(serverAddr) } -func verify(claims *jwt.CustomClaims, tokenTail10 string) error { +func verify(claims *jwt.CustomClaims, tokenTail10 string, c *gin.Context) error { err := errors.New("verify failed") // token := getToken(id) diff --git a/pkg/gin/middleware/auth.go b/pkg/gin/middleware/auth.go index 5a26e222..e52add36 100644 --- a/pkg/gin/middleware/auth.go +++ b/pkg/gin/middleware/auth.go @@ -61,7 +61,7 @@ func responseUnauthorized(c *gin.Context, isSwitchHTTPCode bool) { // ------------------------------------------------------------------------------------------- // VerifyFn verify function, tokenTail10 is a string that intercepts the last 10 characters of the token. -type VerifyFn func(claims *jwt.Claims, tokenTail10 string) error +type VerifyFn func(claims *jwt.Claims, tokenTail10 string, c *gin.Context) error // Auth authorization func Auth(opts ...JwtOption) gin.HandlerFunc { @@ -88,7 +88,7 @@ func Auth(opts ...JwtOption) gin.HandlerFunc { if o.verify != nil { tokenTail10 := token[len(token)-10:] - if err = o.verify(claims, tokenTail10); err != nil { + if err = o.verify(claims, tokenTail10, c); err != nil { logger.Warn("verify error", logger.Err(err), logger.String("uid", claims.UID), logger.String("role", claims.Role)) responseUnauthorized(c, o.isSwitchHTTPCode) c.Abort() @@ -106,7 +106,7 @@ func Auth(opts ...JwtOption) gin.HandlerFunc { // ------------------------------------------------------------------------------------------- // VerifyCustomFn verify custom function, tokenTail10 is a string that intercepts the last 10 characters of the token. -type VerifyCustomFn func(claims *jwt.CustomClaims, tokenTail10 string) error +type VerifyCustomFn func(claims *jwt.CustomClaims, tokenTail10 string, c *gin.Context) error // AuthCustom custom authentication func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc { @@ -132,7 +132,7 @@ func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc { } tokenTail10 := token[len(token)-10:] - if err = verify(claims, tokenTail10); err != nil { + if err = verify(claims, tokenTail10, c); err != nil { logger.Warn("verify error", logger.Err(err), logger.Any("fields", claims.Fields)) responseUnauthorized(c, o.isSwitchHTTPCode) c.Abort() diff --git a/pkg/gin/middleware/auth_test.go b/pkg/gin/middleware/auth_test.go index 0a43ba23..95ebbaca 100644 --- a/pkg/gin/middleware/auth_test.go +++ b/pkg/gin/middleware/auth_test.go @@ -22,7 +22,7 @@ var ( fields = jwt.KV{"id": 1, "foo": "bar"} ) -func verify(claims *jwt.Claims, tokenTail10 string) error { +func verify(claims *jwt.Claims, tokenTail10 string, c *gin.Context) error { if claims.UID != uid || claims.Role != role { return errors.New("verify failed") } @@ -33,7 +33,7 @@ func verify(claims *jwt.Claims, tokenTail10 string) error { return nil } -func verifyCustom(claims *jwt.CustomClaims, tokenTail10 string) error { +func verifyCustom(claims *jwt.CustomClaims, tokenTail10 string, c *gin.Context) error { err := errors.New("verify failed") id, exist := claims.Get("id")