Skip to content

Commit b5e1a03

Browse files
n-wenappleboy
authored andcommitted
support IdentityHandler. (#62)
1 parent 54836d3 commit b5e1a03

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

auth_jwt.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type GinJWTMiddleware struct {
5656
// User can define own Unauthorized func.
5757
Unauthorized func(*gin.Context, int, string)
5858

59+
// Set the identity handler function
60+
IdentityHandler func(jwt.MapClaims) string
61+
5962
// TokenLookup is a string in the form of "<source>:<name>" that is used
6063
// to extract token from the request.
6164
// Optional. Default value "header:Authorization".
@@ -110,6 +113,12 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
110113
}
111114
}
112115

116+
if mw.IdentityHandler == nil {
117+
mw.IdentityHandler = func (claims jwt.MapClaims) string {
118+
return claims["id"].(string)
119+
}
120+
}
121+
113122
if mw.Realm == "" {
114123
return errors.New("realm is required")
115124
}
@@ -150,7 +159,7 @@ func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {
150159

151160
claims := token.Claims.(jwt.MapClaims)
152161

153-
id := claims["id"].(string)
162+
id := mw.IdentityHandler(claims)
154163
c.Set("JWT_PAYLOAD", claims)
155164
c.Set("userID", id)
156165

0 commit comments

Comments
 (0)