Skip to content

Commit bad9f43

Browse files
committed
refactor: unify function parameter order for context and data/error
- Change Authorizator function signature: swap parameter order to (c *gin.Context, data any) across all usages - Update HTTPStatusMessageFunc signature: swap parameter order to (c *gin.Context, e error) - Update middleware code and handler implementations to use new function signatures - Modify all relevant test cases to reflect the changed parameter order for Authorizator and HTTPStatusMessageFunc functions Signed-off-by: appleboy <[email protected]>
1 parent f8fef47 commit bad9f43

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

_example/basic/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ func authenticator() func(c *gin.Context) (any, error) {
133133
}
134134
}
135135

136-
func authorizator() func(data any, c *gin.Context) bool {
137-
return func(data any, c *gin.Context) bool {
136+
func authorizator() func(c *gin.Context, data any) bool {
137+
return func(c *gin.Context, data any) bool {
138138
if v, ok := data.(*User); ok && v.UserName == "admin" {
139139
return true
140140
}

_example/redis_simple/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func main() {
6565
return nil, jwt.ErrFailedAuthentication
6666
},
6767

68-
Authorizator: func(data any, c *gin.Context) bool {
68+
Authorizator: func(c *gin.Context, data any) bool {
6969
if v, ok := data.(*User); ok && v.UserName == "admin" {
7070
return true
7171
}

_example/redis_store/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161

6262
return nil, jwt.ErrFailedAuthentication
6363
},
64-
Authorizator: func(data any, c *gin.Context) bool {
64+
Authorizator: func(c *gin.Context, data any) bool {
6565
if v, ok := data.(*User); ok && v.UserName == "admin" {
6666
return true
6767
}

auth_jwt.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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"}.
558558
func (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) {
10221022
func (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

auth_jwt_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func TestLoginHandler(t *testing.T) {
273273
}
274274
return "", ErrFailedAuthentication
275275
},
276-
Authorizator: func(user any, c *gin.Context) bool {
276+
Authorizator: func(c *gin.Context, user any) bool {
277277
return true
278278
},
279279
LoginResponse: func(c *gin.Context, token *core.Token) {
@@ -675,7 +675,7 @@ func TestAuthorizator(t *testing.T) {
675675
Timeout: time.Hour,
676676
MaxRefresh: time.Hour * 24,
677677
Authenticator: defaultAuthenticator,
678-
Authorizator: func(data any, c *gin.Context) bool {
678+
Authorizator: func(c *gin.Context, data any) bool {
679679
return data.(string) == "admin"
680680
},
681681
})
@@ -782,7 +782,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
782782

783783
return "Guest", ErrFailedAuthentication
784784
},
785-
Authorizator: func(user any, c *gin.Context) bool {
785+
Authorizator: func(c *gin.Context, user any) bool {
786786
jwtClaims := ExtractClaims(c)
787787

788788
if jwtClaims["identity"] == "administrator" {
@@ -1125,7 +1125,7 @@ func TestHTTPStatusMessageFunc(t *testing.T) {
11251125
MaxRefresh: time.Hour * 24,
11261126
Authenticator: defaultAuthenticator,
11271127

1128-
HTTPStatusMessageFunc: func(e error, c *gin.Context) string {
1128+
HTTPStatusMessageFunc: func(c *gin.Context, e error) string {
11291129
if e == successError {
11301130
return successMessage
11311131
}
@@ -1134,8 +1134,8 @@ func TestHTTPStatusMessageFunc(t *testing.T) {
11341134
},
11351135
})
11361136

1137-
successString := authMiddleware.HTTPStatusMessageFunc(successError, nil)
1138-
failedString := authMiddleware.HTTPStatusMessageFunc(failedError, nil)
1137+
successString := authMiddleware.HTTPStatusMessageFunc(nil, successError)
1138+
failedString := authMiddleware.HTTPStatusMessageFunc(nil, failedError)
11391139

11401140
assert.Equal(t, successMessage, successString)
11411141
assert.NotEqual(t, successMessage, failedString)
@@ -1150,7 +1150,7 @@ func TestSendAuthorizationBool(t *testing.T) {
11501150
MaxRefresh: time.Hour * 24,
11511151
Authenticator: defaultAuthenticator,
11521152
SendAuthorization: true,
1153-
Authorizator: func(data any, c *gin.Context) bool {
1153+
Authorizator: func(c *gin.Context, data any) bool {
11541154
return data.(string) == "admin"
11551155
},
11561156
})
@@ -1188,7 +1188,7 @@ func TestExpiredTokenOnAuth(t *testing.T) {
11881188
MaxRefresh: time.Hour * 24,
11891189
Authenticator: defaultAuthenticator,
11901190
SendAuthorization: true,
1191-
Authorizator: func(data any, c *gin.Context) bool {
1191+
Authorizator: func(c *gin.Context, data any) bool {
11921192
return data.(string) == "admin"
11931193
},
11941194
TimeFunc: func() time.Time {
@@ -1456,7 +1456,7 @@ func TestGenerateTokenPair(t *testing.T) {
14561456
"identity": data,
14571457
}
14581458
},
1459-
Authorizator: func(data any, c *gin.Context) bool {
1459+
Authorizator: func(c *gin.Context, data any) bool {
14601460
return data == "admin"
14611461
},
14621462
Unauthorized: func(c *gin.Context, code int, message string) {

0 commit comments

Comments
 (0)