Skip to content

Commit d31681b

Browse files
authored
fix: IdentityHandler arg (#159)
fix #158
1 parent df10efd commit d31681b

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ type login struct {
4343
Password string `form:"password" json:"password" binding:"required"`
4444
}
4545

46+
var identityKey = "id"
47+
4648
func helloHandler(c *gin.Context) {
4749
claims := jwt.ExtractClaims(c)
50+
user, _ := c.Get(identityKey)
4851
c.JSON(200, gin.H{
49-
"userID": claims["id"],
50-
"text": "Hello World.",
52+
"userID": claims["id"],
53+
"userName": user.(*User).UserName,
54+
"text": "Hello World.",
5155
})
5256
}
5357

@@ -74,15 +78,21 @@ func main() {
7478
Key: []byte("secret key"),
7579
Timeout: time.Hour,
7680
MaxRefresh: time.Hour,
77-
IdentityKey: "id",
81+
IdentityKey: identityKey,
7882
PayloadFunc: func(data interface{}) jwt.MapClaims {
7983
if v, ok := data.(*User); ok {
8084
return jwt.MapClaims{
81-
"id": v.UserName,
85+
identityKey: v.UserName,
8286
}
8387
}
8488
return jwt.MapClaims{}
8589
},
90+
IdentityHandler: func(c *gin.Context) interface{} {
91+
claims := jwt.ExtractClaims(c)
92+
return &User{
93+
UserName: claims["id"].(string),
94+
}
95+
},
8696
Authenticator: func(c *gin.Context) (interface{}, error) {
8797
var loginVals login
8898
if err := c.ShouldBind(&loginVals); err != nil {
@@ -102,7 +112,7 @@ func main() {
102112
return nil, jwt.ErrFailedAuthentication
103113
},
104114
Authorizator: func(data interface{}, c *gin.Context) bool {
105-
if v, ok := data.(string); ok && v == "admin" {
115+
if v, ok := data.(*User); ok && v.UserName == "admin" {
106116
return true
107117
}
108118

auth_jwt.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type GinJWTMiddleware struct {
6969
RefreshResponse func(*gin.Context, int, string, time.Time)
7070

7171
// Set the identity handler function
72-
IdentityHandler func(jwt.MapClaims) interface{}
72+
IdentityHandler func(*gin.Context) interface{}
7373

7474
// Set the identity key
7575
IdentityKey string
@@ -166,6 +166,9 @@ var (
166166

167167
// ErrInvalidPubKey indicates the the given public key is invalid
168168
ErrInvalidPubKey = errors.New("public key invalid")
169+
170+
// IdentityKey default identity key
171+
IdentityKey = "identity"
169172
)
170173

171174
// New for check error with GinJWTMiddleware
@@ -283,11 +286,12 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
283286
}
284287

285288
if mw.IdentityKey == "" {
286-
mw.IdentityKey = "identity"
289+
mw.IdentityKey = IdentityKey
287290
}
288291

289292
if mw.IdentityHandler == nil {
290-
mw.IdentityHandler = func(claims jwt.MapClaims) interface{} {
293+
mw.IdentityHandler = func(c *gin.Context) interface{} {
294+
claims := ExtractClaims(c)
291295
return claims[mw.IdentityKey]
292296
}
293297
}
@@ -334,9 +338,9 @@ func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {
334338
}
335339

336340
claims := token.Claims.(jwt.MapClaims)
337-
338-
identity := mw.IdentityHandler(claims)
339341
c.Set("JWT_PAYLOAD", claims)
342+
identity := mw.IdentityHandler(c)
343+
340344
if identity != nil {
341345
c.Set(mw.IdentityKey, identity)
342346
}

example/basic/server.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ type login struct {
1515
Password string `form:"password" json:"password" binding:"required"`
1616
}
1717

18+
var identityKey = "id"
19+
1820
func helloHandler(c *gin.Context) {
1921
claims := jwt.ExtractClaims(c)
22+
user, _ := c.Get(identityKey)
2023
c.JSON(200, gin.H{
21-
"userID": claims["id"],
22-
"text": "Hello World.",
24+
"userID": claims["id"],
25+
"userName": user.(*User).UserName,
26+
"text": "Hello World.",
2327
})
2428
}
2529

@@ -46,15 +50,21 @@ func main() {
4650
Key: []byte("secret key"),
4751
Timeout: time.Hour,
4852
MaxRefresh: time.Hour,
49-
IdentityKey: "id",
53+
IdentityKey: identityKey,
5054
PayloadFunc: func(data interface{}) jwt.MapClaims {
5155
if v, ok := data.(*User); ok {
5256
return jwt.MapClaims{
53-
"id": v.UserName,
57+
identityKey: v.UserName,
5458
}
5559
}
5660
return jwt.MapClaims{}
5761
},
62+
IdentityHandler: func(c *gin.Context) interface{} {
63+
claims := jwt.ExtractClaims(c)
64+
return &User{
65+
UserName: claims["id"].(string),
66+
}
67+
},
5868
Authenticator: func(c *gin.Context) (interface{}, error) {
5969
var loginVals login
6070
if err := c.ShouldBind(&loginVals); err != nil {
@@ -74,7 +84,7 @@ func main() {
7484
return nil, jwt.ErrFailedAuthentication
7585
},
7686
Authorizator: func(data interface{}, c *gin.Context) bool {
77-
if v, ok := data.(string); ok && v == "admin" {
87+
if v, ok := data.(*User); ok && v.UserName == "admin" {
7888
return true
7989
}
8090

0 commit comments

Comments
 (0)