Skip to content

Commit

Permalink
support IdentityHandler. (appleboy#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
n-wen authored and appleboy committed Mar 22, 2017
1 parent 54836d3 commit b5e1a03
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type GinJWTMiddleware struct {
// User can define own Unauthorized func.
Unauthorized func(*gin.Context, int, string)

// Set the identity handler function
IdentityHandler func(jwt.MapClaims) string

// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
Expand Down Expand Up @@ -110,6 +113,12 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
}
}

if mw.IdentityHandler == nil {
mw.IdentityHandler = func (claims jwt.MapClaims) string {
return claims["id"].(string)
}
}

if mw.Realm == "" {
return errors.New("realm is required")
}
Expand Down Expand Up @@ -150,7 +159,7 @@ func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {

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

id := claims["id"].(string)
id := mw.IdentityHandler(claims)
c.Set("JWT_PAYLOAD", claims)
c.Set("userID", id)

Expand Down

0 comments on commit b5e1a03

Please sign in to comment.