forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add reuse interval for token refresh (supabase#466)
* add reuse interval to config * add test for refresh token reuse detection * fix: add reuse interval to refresh token grant * add tests for reuse interval * refactor token test * ignore reuse interval if revoked token is last token * add test case * refactor query to get child token * remove unnecessary check in refresh token grant * update readme * update example env file
- Loading branch information
1 parent
4b425f6
commit bc7323e
Showing
6 changed files
with
159 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,95 @@ func (ts *TokenTestSuite) TestTokenRefreshTokenGrantFailure() { | |
assert.Equal(ts.T(), http.StatusBadRequest, w.Code) | ||
} | ||
|
||
func (ts *TokenTestSuite) TestTokenRefreshTokenRotation() { | ||
u, err := models.NewUser(ts.instanceID, "[email protected]", "password", ts.Config.JWT.Aud, nil) | ||
require.NoError(ts.T(), err, "Error creating test user model") | ||
t := time.Now() | ||
u.EmailConfirmedAt = &t | ||
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving foo user") | ||
first, err := models.GrantAuthenticatedUser(ts.API.db, u) | ||
require.NoError(ts.T(), err) | ||
second, err := models.GrantRefreshTokenSwap(ts.API.db, u, first) | ||
require.NoError(ts.T(), err) | ||
third, err := models.GrantRefreshTokenSwap(ts.API.db, u, second) | ||
require.NoError(ts.T(), err) | ||
|
||
cases := []struct { | ||
desc string | ||
refreshTokenRotationEnabled bool | ||
reuseInterval int | ||
refreshToken string | ||
expectedCode int | ||
expectedBody map[string]interface{} | ||
}{ | ||
{ | ||
"Valid refresh within reuse interval", | ||
true, | ||
30, | ||
second.Token, | ||
http.StatusOK, | ||
map[string]interface{}{ | ||
"refresh_token": third.Token, | ||
}, | ||
}, | ||
{ | ||
"Invalid refresh, first token is not the previous revoked token", | ||
true, | ||
0, | ||
first.Token, | ||
http.StatusBadRequest, | ||
map[string]interface{}{ | ||
"error": "invalid_grant", | ||
"error_description": "Invalid Refresh Token", | ||
}, | ||
}, | ||
{ | ||
"Invalid refresh, revoked third token", | ||
true, | ||
0, | ||
second.Token, | ||
http.StatusBadRequest, | ||
map[string]interface{}{ | ||
"error": "invalid_grant", | ||
"error_description": "Invalid Refresh Token", | ||
}, | ||
}, | ||
{ | ||
"Invalid refresh, third token revoked by previous case", | ||
true, | ||
30, | ||
third.Token, | ||
http.StatusBadRequest, | ||
map[string]interface{}{ | ||
"error": "invalid_grant", | ||
"error_description": "Invalid Refresh Token", | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
ts.Config.Security.RefreshTokenRotationEnabled = c.refreshTokenRotationEnabled | ||
ts.Config.Security.RefreshTokenReuseInterval = c.reuseInterval | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{ | ||
"refresh_token": c.refreshToken, | ||
})) | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost/token?grant_type=refresh_token", &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
w := httptest.NewRecorder() | ||
ts.API.handler.ServeHTTP(w, req) | ||
assert.Equal(ts.T(), c.expectedCode, w.Code) | ||
|
||
data := make(map[string]interface{}) | ||
require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data)) | ||
for k, v := range c.expectedBody { | ||
require.Equal(ts.T(), v, data[k]) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (ts *TokenTestSuite) createBannedUser() *models.User { | ||
u, err := models.NewUser(ts.instanceID, "[email protected]", "password", ts.Config.JWT.Aud, nil) | ||
require.NoError(ts.T(), err, "Error creating test user model") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters