@@ -58,7 +58,7 @@ type GinJWTMiddleware struct {
5858 // Callback function that should perform the authorization of the authenticated user. Called
5959 // only after an authentication success. Must return true on success, false on failure.
6060 // Optional, default to success.
61- Authorizator func (data any , c * gin.Context ) bool
61+ Authorizator func (c * gin.Context , data any ) bool
6262
6363 // Callback function that will be called during login.
6464 // Using this function it is possible to add additional payload data to the webtoken.
@@ -103,7 +103,7 @@ type GinJWTMiddleware struct {
103103
104104 // HTTP Status messages for when something in the JWT middleware fails.
105105 // Check error (e) to determine the appropriate error message.
106- HTTPStatusMessageFunc func (e error , c * gin.Context ) string
106+ HTTPStatusMessageFunc func (c * gin.Context , e error ) string
107107
108108 // Private key file for asymmetric algorithms
109109 PrivKeyFile string
@@ -383,7 +383,7 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
383383 }
384384
385385 if mw .Authorizator == nil {
386- mw .Authorizator = func (data any , c * gin.Context ) bool {
386+ mw .Authorizator = func (c * gin.Context , data any ) bool {
387387 return true
388388 }
389389 }
@@ -431,7 +431,7 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
431431 }
432432
433433 if mw .HTTPStatusMessageFunc == nil {
434- mw .HTTPStatusMessageFunc = func (e error , c * gin.Context ) string {
434+ mw .HTTPStatusMessageFunc = func (c * gin.Context , e error ) string {
435435 return e .Error ()
436436 }
437437 }
@@ -509,7 +509,7 @@ func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {
509509
510510 // For backwards compatibility since technically exp is not required in the spec but has been in gin-jwt
511511 if claims ["exp" ] == nil {
512- mw .unauthorized (c , http .StatusBadRequest , mw .HTTPStatusMessageFunc (ErrMissingExpField , c ))
512+ mw .unauthorized (c , http .StatusBadRequest , mw .HTTPStatusMessageFunc (c , ErrMissingExpField ))
513513 return
514514 }
515515
@@ -520,8 +520,8 @@ func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {
520520 c .Set (mw .IdentityKey , identity )
521521 }
522522
523- if ! mw .Authorizator (identity , c ) {
524- mw .unauthorized (c , http .StatusForbidden , mw .HTTPStatusMessageFunc (ErrForbidden , c ))
523+ if ! mw .Authorizator (c , identity ) {
524+ mw .unauthorized (c , http .StatusForbidden , mw .HTTPStatusMessageFunc (c , ErrForbidden ))
525525 return
526526 }
527527
@@ -557,20 +557,20 @@ func (mw *GinJWTMiddleware) GetClaimsFromJWT(c *gin.Context) (jwt.MapClaims, err
557557// Reply will be of the form {"token": "TOKEN"}.
558558func (mw * GinJWTMiddleware ) LoginHandler (c * gin.Context ) {
559559 if mw .Authenticator == nil {
560- mw .unauthorized (c , http .StatusInternalServerError , mw .HTTPStatusMessageFunc (ErrMissingAuthenticatorFunc , c ))
560+ mw .unauthorized (c , http .StatusInternalServerError , mw .HTTPStatusMessageFunc (c , ErrMissingAuthenticatorFunc ))
561561 return
562562 }
563563
564564 data , err := mw .Authenticator (c )
565565 if err != nil {
566- mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (err , c ))
566+ mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (c , err ))
567567 return
568568 }
569569
570570 // Generate complete token pair
571571 tokenPair , err := mw .GenerateTokenPair (data )
572572 if err != nil {
573- mw .unauthorized (c , http .StatusInternalServerError , mw .HTTPStatusMessageFunc (ErrFailedTokenCreation , c ))
573+ mw .unauthorized (c , http .StatusInternalServerError , mw .HTTPStatusMessageFunc (c , ErrFailedTokenCreation ))
574574 return
575575 }
576576
@@ -694,14 +694,14 @@ func (mw *GinJWTMiddleware) RefreshHandler(c *gin.Context) {
694694 // Validate refresh token
695695 userData , err := mw .validateRefreshToken (refreshToken )
696696 if err != nil {
697- mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (err , c ))
697+ mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (c , err ))
698698 return
699699 }
700700
701701 // Generate new token pair and revoke old refresh token
702702 tokenPair , err := mw .GenerateTokenPairWithRevocation (userData , refreshToken )
703703 if err != nil {
704- mw .unauthorized (c , http .StatusInternalServerError , mw .HTTPStatusMessageFunc (err , c ))
704+ mw .unauthorized (c , http .StatusInternalServerError , mw .HTTPStatusMessageFunc (c , err ))
705705 return
706706 }
707707
@@ -1022,13 +1022,13 @@ func (mw *GinJWTMiddleware) SetCookie(c *gin.Context, token string) {
10221022func (mw * GinJWTMiddleware ) handleTokenError (c * gin.Context , err error ) {
10231023 switch {
10241024 case errors .Is (err , jwt .ErrTokenExpired ):
1025- mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (ErrExpiredToken , c ))
1025+ mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (c , ErrExpiredToken ))
10261026 case errors .Is (err , jwt .ErrInvalidType ) && strings .Contains (err .Error (), "exp is invalid" ):
1027- mw .unauthorized (c , http .StatusBadRequest , mw .HTTPStatusMessageFunc (ErrWrongFormatOfExp , c ))
1027+ mw .unauthorized (c , http .StatusBadRequest , mw .HTTPStatusMessageFunc (c , ErrWrongFormatOfExp ))
10281028 case errors .Is (err , jwt .ErrTokenRequiredClaimMissing ) && strings .Contains (err .Error (), "exp claim is required" ):
1029- mw .unauthorized (c , http .StatusBadRequest , mw .HTTPStatusMessageFunc (ErrMissingExpField , c ))
1029+ mw .unauthorized (c , http .StatusBadRequest , mw .HTTPStatusMessageFunc (c , ErrMissingExpField ))
10301030 default :
1031- mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (err , c ))
1031+ mw .unauthorized (c , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (c , err ))
10321032 }
10331033}
10341034
0 commit comments