Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,20 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {

if mw.LoginResponse == nil {
mw.LoginResponse = func(c *gin.Context, code int, token string, expire time.Time) {
refreshToken, _, err := mw.RefreshToken(c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": mw.HTTPStatusMessageFunc(err, c),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
"access_token": token,
"token_type": "Bearer",
"expires_in": int(time.Until(expire).Seconds()),
"refresh_token": refreshToken,
"scope": "create",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The scope is hardcoded to "create". Since scopes are not configurable, it's better to omit this optional field.

})
}
}
Expand All @@ -374,10 +384,20 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {

if mw.RefreshResponse == nil {
mw.RefreshResponse = func(c *gin.Context, code int, token string, expire time.Time) {
refreshToken, _, err := mw.RefreshToken(c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": mw.HTTPStatusMessageFunc(err, c),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
"access_token": token,
"token_type": "Bearer",
"expires_in": int(time.Until(expire).Seconds()),
"refresh_token": refreshToken,
"scope": "create",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The scope is hardcoded to "create". It's better to omit this optional field since scopes are not a supported feature.

})
}
}
Expand Down
13 changes: 10 additions & 3 deletions auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,14 @@ func TestClaimsDuringAuthorization(t *testing.T) {
testkey = ""
}
// Set custom claim, to be checked in Authorizator method
return MapClaims{"identity": data.(string), "testkey": testkey, "exp": 0}
now := time.Now()
return MapClaims{
"identity": data.(string),
"testkey": testkey,
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"nbf": now.Unix(),
}
},
Authenticator: func(c *gin.Context) (interface{}, error) {
var loginVals Login
Expand Down Expand Up @@ -780,7 +787,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
"password": "admin",
}).
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
token := gjson.Get(r.Body.String(), "token")
token := gjson.Get(r.Body.String(), "access_token")
userToken = token.String()
assert.Equal(t, http.StatusOK, r.Code)
})
Expand All @@ -799,7 +806,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
"password": "test",
}).
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
token := gjson.Get(r.Body.String(), "token")
token := gjson.Get(r.Body.String(), "access_token")
userToken = token.String()
assert.Equal(t, http.StatusOK, r.Code)
})
Expand Down
Loading