Skip to content

Commit bb81564

Browse files
authored
feat: adopt OAuth2-style token fields in authentication responses (#347)
* feat: adopt OAuth2-style token fields in authentication responses - Replace token response fields with OAuth2-style fields: access_token, token_type, expires_in, refresh_token, and scope - Add a test assertion to verify that refresh_token is present and not empty in the response fix #346 Signed-off-by: Bo-Yi Wu <[email protected]> * fix: improve refresh token handling and update related tests - Add error handling and response for refresh token generation failures during middleware initialization - Return the actual refresh token in the response instead of an empty string - Update test claims to include standard JWT fields with current timestamps - Change test code to extract access_token instead of token from responses - Remove redundant test assertions for refresh_token presence Signed-off-by: Bo-Yi Wu <[email protected]> --------- Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 892b627 commit bb81564

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

auth_jwt.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,20 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
356356

357357
if mw.LoginResponse == nil {
358358
mw.LoginResponse = func(c *gin.Context, code int, token string, expire time.Time) {
359+
refreshToken, _, err := mw.RefreshToken(c)
360+
if err != nil {
361+
c.JSON(http.StatusUnauthorized, gin.H{
362+
"code": http.StatusUnauthorized,
363+
"message": mw.HTTPStatusMessageFunc(err, c),
364+
})
365+
return
366+
}
359367
c.JSON(http.StatusOK, gin.H{
360-
"code": http.StatusOK,
361-
"token": token,
362-
"expire": expire.Format(time.RFC3339),
368+
"access_token": token,
369+
"token_type": "Bearer",
370+
"expires_in": int(time.Until(expire).Seconds()),
371+
"refresh_token": refreshToken,
372+
"scope": "create",
363373
})
364374
}
365375
}
@@ -374,10 +384,20 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
374384

375385
if mw.RefreshResponse == nil {
376386
mw.RefreshResponse = func(c *gin.Context, code int, token string, expire time.Time) {
387+
refreshToken, _, err := mw.RefreshToken(c)
388+
if err != nil {
389+
c.JSON(http.StatusUnauthorized, gin.H{
390+
"code": http.StatusUnauthorized,
391+
"message": mw.HTTPStatusMessageFunc(err, c),
392+
})
393+
return
394+
}
377395
c.JSON(http.StatusOK, gin.H{
378-
"code": http.StatusOK,
379-
"token": token,
380-
"expire": expire.Format(time.RFC3339),
396+
"access_token": token,
397+
"token_type": "Bearer",
398+
"expires_in": int(time.Until(expire).Seconds()),
399+
"refresh_token": refreshToken,
400+
"scope": "create",
381401
})
382402
}
383403
}

auth_jwt_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,14 @@ func TestClaimsDuringAuthorization(t *testing.T) {
718718
testkey = ""
719719
}
720720
// Set custom claim, to be checked in Authorizator method
721-
return MapClaims{"identity": data.(string), "testkey": testkey, "exp": 0}
721+
now := time.Now()
722+
return MapClaims{
723+
"identity": data.(string),
724+
"testkey": testkey,
725+
"exp": now.Add(time.Hour).Unix(),
726+
"iat": now.Unix(),
727+
"nbf": now.Unix(),
728+
}
722729
},
723730
Authenticator: func(c *gin.Context) (interface{}, error) {
724731
var loginVals Login
@@ -780,7 +787,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
780787
"password": "admin",
781788
}).
782789
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
783-
token := gjson.Get(r.Body.String(), "token")
790+
token := gjson.Get(r.Body.String(), "access_token")
784791
userToken = token.String()
785792
assert.Equal(t, http.StatusOK, r.Code)
786793
})
@@ -799,7 +806,7 @@ func TestClaimsDuringAuthorization(t *testing.T) {
799806
"password": "test",
800807
}).
801808
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
802-
token := gjson.Get(r.Body.String(), "token")
809+
token := gjson.Get(r.Body.String(), "access_token")
803810
userToken = token.String()
804811
assert.Equal(t, http.StatusOK, r.Code)
805812
})

0 commit comments

Comments
 (0)