Skip to content

Commit a20dca5

Browse files
committed
refactor: standardize middleware naming to Authorizer across the codebase
- Rename the middleware configuration field from Authorizator to Authorizer throughout the codebase and documentation - Update all references and usage examples to use Authorizer instead of Authorizator - Revise test functions and comments to reflect the Authorizer naming change fix #295 Signed-off-by: appleboy <[email protected]>
1 parent bad9f43 commit a20dca5

File tree

8 files changed

+24
-24
lines changed

8 files changed

+24
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func initParams() *jwt.GinJWTMiddleware {
182182

183183
IdentityHandler: identityHandler(),
184184
Authenticator: authenticator(),
185-
Authorizator: authorizator(),
185+
Authorizer: authorizator(),
186186
Unauthorized: unauthorized(),
187187
TokenLookup: "header: Authorization, query: token, cookie: jwt",
188188
// TokenLookup: "query:token",
@@ -662,13 +662,13 @@ Signature: `func(c *gin.Context, token *core.Token)`
662662

663663
PROVIDED: `MiddlewareFunc`
664664

665-
This is gin middleware that should be used within any endpoints that require the jwt token to be present. This middleware will parse the request headers for the token if it exists, and check that the jwt token is valid (not expired, correct signature). Then it will call `IdentityHandler` followed by `Authorizator`. If `Authorizator` passes and all of the previous token validity checks passed, the middleware will continue the request. If any of these checks fail, the `Unauthorized` function is used (explained below).
665+
This is gin middleware that should be used within any endpoints that require the jwt token to be present. This middleware will parse the request headers for the token if it exists, and check that the jwt token is valid (not expired, correct signature). Then it will call `IdentityHandler` followed by `Authorizer`. If `Authorizer` passes and all of the previous token validity checks passed, the middleware will continue the request. If any of these checks fail, the `Unauthorized` function is used (explained below).
666666

667667
OPTIONAL: `IdentityHandler`
668668

669-
The default of this function is likely sufficient for your needs. The purpose of this function is to fetch the user identity from claims embedded within the jwt token, and pass this identity value to `Authorizator`. This function assumes [`IdentityKey`: some_user_identity] is one of the attributes embedded within the claims of the jwt token (determined by `PayloadFunc`).
669+
The default of this function is likely sufficient for your needs. The purpose of this function is to fetch the user identity from claims embedded within the jwt token, and pass this identity value to `Authorizer`. This function assumes [`IdentityKey`: some_user_identity] is one of the attributes embedded within the claims of the jwt token (determined by `PayloadFunc`).
670670

671-
OPTIONAL: `Authorizator`
671+
OPTIONAL: `Authorizer`
672672

673673
Given the user identity value (`data` parameter) and the gin context, this function should check if the user is authorized to be reaching this endpoint (on the endpoints where the `MiddlewareFunc` applies). This function should likely use `ExtractClaims` to check if the user has the sufficient permissions to reach this endpoint, as opposed to hitting the database on every request. This function should return true if the user is authorized to continue through with the request, or false if they are not authorized (where `Unauthorized` will be called).
674674

README.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ CookieSameSite: http.SameSiteDefaultMode, // SameSiteDefaultMode, SameSiteLaxM
500500

501501
- 从 header/cookie/query 解析 Token
502502
- 验证 Token
503-
- 调用 `IdentityHandler``Authorizator`
503+
- 调用 `IdentityHandler``Authorizer`
504504
- 验证失败则调用 `Unauthorized`
505505

506506
- **可选:** `IdentityHandler`
507507
从 JWT Claims 获取用户身份。
508508

509-
- **可选:** `Authorizator`
509+
- **可选:** `Authorizer`
510510
检查用户是否有权限访问该端点。
511511

512512
---

README.zh-TW.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ CookieSameSite: http.SameSiteDefaultMode, // SameSiteDefaultMode, SameSiteLaxM
500500

501501
- 從 header/cookie/query 解析 Token
502502
- 驗證 Token
503-
- 呼叫 `IdentityHandler``Authorizator`
503+
- 呼叫 `IdentityHandler``Authorizer`
504504
- 驗證失敗則呼叫 `Unauthorized`
505505

506506
- **可選:** `IdentityHandler`
507507
從 JWT Claims 取得使用者身份。
508508

509-
- **可選:** `Authorizator`
509+
- **可選:** `Authorizer`
510510
檢查使用者是否有權限存取該端點。
511511

512512
---

_example/basic/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func initParams() *jwt.GinJWTMiddleware {
8282

8383
IdentityHandler: identityHandler(),
8484
Authenticator: authenticator(),
85-
Authorizator: authorizator(),
85+
Authorizer: authorizator(),
8686
Unauthorized: unauthorized(),
8787
LogoutResponse: logoutResponse(),
8888
TokenLookup: "header: Authorization, query: token, cookie: jwt",

_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(c *gin.Context, data any) bool {
68+
Authorizer: 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(c *gin.Context, data any) bool {
64+
Authorizer: 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: 4 additions & 4 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(c *gin.Context, data any) bool
61+
Authorizer 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.
@@ -382,8 +382,8 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
382382
mw.TokenHeadName = "Bearer"
383383
}
384384

385-
if mw.Authorizator == nil {
386-
mw.Authorizator = func(c *gin.Context, data any) bool {
385+
if mw.Authorizer == nil {
386+
mw.Authorizer = func(c *gin.Context, data any) bool {
387387
return true
388388
}
389389
}
@@ -520,7 +520,7 @@ func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {
520520
c.Set(mw.IdentityKey, identity)
521521
}
522522

523-
if !mw.Authorizator(c, identity) {
523+
if !mw.Authorizer(c, identity) {
524524
mw.unauthorized(c, http.StatusForbidden, mw.HTTPStatusMessageFunc(c,ErrForbidden))
525525
return
526526
}

auth_jwt_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestLoginHandler(t *testing.T) {
258258
Realm: "test zone",
259259
Key: key,
260260
PayloadFunc: func(data any) jwt.MapClaims {
261-
// Set custom claim, to be checked in Authorizator method
261+
// Set custom claim, to be checked in Authorizer method
262262
return jwt.MapClaims{"testkey": "testval", "exp": 0}
263263
},
264264
Authenticator: func(c *gin.Context) (any, error) {
@@ -273,7 +273,7 @@ func TestLoginHandler(t *testing.T) {
273273
}
274274
return "", ErrFailedAuthentication
275275
},
276-
Authorizator: func(c *gin.Context, user any) bool {
276+
Authorizer: func(c *gin.Context, user any) bool {
277277
return true
278278
},
279279
LoginResponse: func(c *gin.Context, token *core.Token) {
@@ -667,15 +667,15 @@ func TestExpiredTokenOnRefreshHandler(t *testing.T) {
667667
}
668668
}
669669

670-
func TestAuthorizator(t *testing.T) {
670+
func TestAuthorizer(t *testing.T) {
671671
// the middleware to test
672672
authMiddleware, _ := New(&GinJWTMiddleware{
673673
Realm: "test zone",
674674
Key: key,
675675
Timeout: time.Hour,
676676
MaxRefresh: time.Hour * 24,
677677
Authenticator: defaultAuthenticator,
678-
Authorizator: func(c *gin.Context, data any) bool {
678+
Authorizer: func(c *gin.Context, data any) bool {
679679
return data.(string) == "admin"
680680
},
681681
})
@@ -752,7 +752,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
752752
case "Guest":
753753
testkey = ""
754754
}
755-
// Set custom claim, to be checked in Authorizator method
755+
// Set custom claim, to be checked in Authorizer method
756756
now := time.Now()
757757
return jwt.MapClaims{
758758
"identity": data.(string),
@@ -782,7 +782,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
782782

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

788788
if jwtClaims["identity"] == "administrator" {
@@ -1150,7 +1150,7 @@ func TestSendAuthorizationBool(t *testing.T) {
11501150
MaxRefresh: time.Hour * 24,
11511151
Authenticator: defaultAuthenticator,
11521152
SendAuthorization: true,
1153-
Authorizator: func(c *gin.Context, data any) bool {
1153+
Authorizer: 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(c *gin.Context, data any) bool {
1191+
Authorizer: 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(c *gin.Context, data any) bool {
1459+
Authorizer: 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)