Skip to content

Commit

Permalink
support get request_id in verify function
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Dec 2, 2023
1 parent 9d5aee1 commit 2393f09
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/gin/middleware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/gin/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pkg/gin/middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
Expand Down

0 comments on commit 2393f09

Please sign in to comment.