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.
feat: forbid generating an access token without a session (supabase#1504
) ## What kind of change does this PR introduce? Enforces the precondition of a valid session before one can create an access token. This supports refactors around `generateAccessToken` and `updateMFASessionAndClaims`. Also allows for stronger guarantees within the function since one can always assume there is a valid session. There were a few test changes: - To mirror real world use, Access Tokens should now only exist where there is a valid session. We wrap `generateAccessToken` into a helper `generateAccessTokenAndSession` to replace previous occurrences where session was set to nil. - We split TestUpdatePassword into cases where reauthentication is required and reauthentication is not required. We also attach a session to two of the test cases as they were previously nil
- Loading branch information
Showing
8 changed files
with
126 additions
and
43 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
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
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 |
---|---|---|
|
@@ -52,10 +52,20 @@ func (ts *UserTestSuite) generateToken(user *models.User, sessionId *uuid.UUID) | |
return token | ||
} | ||
|
||
func (ts *UserTestSuite) generateAccessTokenAndSession(user *models.User) string { | ||
session, err := models.NewSession(user.ID, nil) | ||
require.NoError(ts.T(), err) | ||
require.NoError(ts.T(), ts.API.db.Create(session)) | ||
|
||
token, _, err := ts.API.generateAccessToken(context.Background(), ts.API.db, user, &session.ID, models.PasswordGrant) | ||
require.NoError(ts.T(), err, "Error generating access token") | ||
return token | ||
} | ||
|
||
func (ts *UserTestSuite) TestUserGet() { | ||
u, err := models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud) | ||
require.NoError(ts.T(), err, "Error finding user") | ||
token := ts.generateToken(u, nil) | ||
token := ts.generateAccessTokenAndSession(u) | ||
|
||
require.NoError(ts.T(), err, "Error generating access token") | ||
|
||
|
@@ -120,7 +130,7 @@ func (ts *UserTestSuite) TestUserUpdateEmail() { | |
require.NoError(ts.T(), u.SetPhone(ts.API.db, c.userData["phone"]), "Error setting user phone") | ||
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving test user") | ||
|
||
token := ts.generateToken(u, nil) | ||
token := ts.generateAccessTokenAndSession(u) | ||
|
||
require.NoError(ts.T(), err, "Error generating access token") | ||
|
||
|
@@ -183,7 +193,7 @@ func (ts *UserTestSuite) TestUserUpdatePhoneAutoconfirmEnabled() { | |
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
token := ts.generateToken(u, nil) | ||
token := ts.generateAccessTokenAndSession(u) | ||
require.NoError(ts.T(), err, "Error generating access token") | ||
|
||
var buffer bytes.Buffer | ||
|
@@ -244,28 +254,28 @@ func (ts *UserTestSuite) TestUserUpdatePassword() { | |
expected expected | ||
}{ | ||
{ | ||
desc: "Invalid password length", | ||
newPassword: "", | ||
desc: "Need reauthentication because outside of recently logged in window", | ||
newPassword: "newpassword123", | ||
nonce: "", | ||
requireReauthentication: false, | ||
sessionId: nil, | ||
expected: expected{code: http.StatusUnprocessableEntity, isAuthenticated: false}, | ||
requireReauthentication: true, | ||
sessionId: ¬RecentlyLoggedIn.ID, | ||
expected: expected{code: http.StatusBadRequest, isAuthenticated: false}, | ||
}, | ||
{ | ||
desc: "No nonce provided", | ||
newPassword: "newpassword123", | ||
nonce: "", | ||
sessionId: ¬RecentlyLoggedIn.ID, | ||
requireReauthentication: true, | ||
sessionId: nil, | ||
expected: expected{code: http.StatusBadRequest, isAuthenticated: false}, | ||
}, | ||
{ | ||
desc: "Need reauthentication because outside of recently logged in window", | ||
newPassword: "newpassword123", | ||
nonce: "", | ||
desc: "Invalid nonce", | ||
newPassword: "newpassword1234", | ||
nonce: "123456", | ||
sessionId: ¬RecentlyLoggedIn.ID, | ||
requireReauthentication: true, | ||
sessionId: r2.SessionId, | ||
expected: expected{code: http.StatusBadRequest, isAuthenticated: false}, | ||
expected: expected{code: http.StatusUnprocessableEntity, isAuthenticated: false}, | ||
}, | ||
{ | ||
desc: "No need reauthentication because recently logged in", | ||
|
@@ -275,20 +285,63 @@ func (ts *UserTestSuite) TestUserUpdatePassword() { | |
sessionId: r.SessionId, | ||
expected: expected{code: http.StatusOK, isAuthenticated: true}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
ts.Config.Security.UpdatePasswordRequireReauthentication = c.requireReauthentication | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]string{"password": c.newPassword, "nonce": c.nonce})) | ||
|
||
req := httptest.NewRequest(http.MethodPut, "http://localhost/user", &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
token := ts.generateToken(u, c.sessionId) | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
// Setup response recorder | ||
w := httptest.NewRecorder() | ||
ts.API.handler.ServeHTTP(w, req) | ||
require.Equal(ts.T(), c.expected.code, w.Code) | ||
|
||
// Request body | ||
u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud) | ||
require.NoError(ts.T(), err) | ||
|
||
require.Equal(ts.T(), c.expected.isAuthenticated, u.Authenticate(context.Background(), c.newPassword)) | ||
}) | ||
} | ||
} | ||
|
||
func (ts *UserTestSuite) TestUserUpdatePasswordNoReauthenticationRequired() { | ||
u, err := models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud) | ||
require.NoError(ts.T(), err) | ||
|
||
type expected struct { | ||
code int | ||
isAuthenticated bool | ||
} | ||
|
||
var cases = []struct { | ||
desc string | ||
newPassword string | ||
nonce string | ||
requireReauthentication bool | ||
expected expected | ||
}{ | ||
{ | ||
desc: "Invalid nonce", | ||
newPassword: "newpassword1234", | ||
nonce: "123456", | ||
requireReauthentication: true, | ||
sessionId: nil, | ||
desc: "Invalid password length", | ||
newPassword: "", | ||
nonce: "", | ||
requireReauthentication: false, | ||
expected: expected{code: http.StatusUnprocessableEntity, isAuthenticated: false}, | ||
}, | ||
|
||
{ | ||
desc: "Valid password length", | ||
newPassword: "newpassword", | ||
nonce: "", | ||
requireReauthentication: false, | ||
sessionId: nil, | ||
expected: expected{code: http.StatusOK, isAuthenticated: true}, | ||
}, | ||
} | ||
|
@@ -301,7 +354,7 @@ func (ts *UserTestSuite) TestUserUpdatePassword() { | |
|
||
req := httptest.NewRequest(http.MethodPut, "http://localhost/user", &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
token := ts.generateToken(u, c.sessionId) | ||
token := ts.generateAccessTokenAndSession(u) | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
|
@@ -330,7 +383,7 @@ func (ts *UserTestSuite) TestUserUpdatePasswordReauthentication() { | |
u.EmailConfirmedAt = &now | ||
require.NoError(ts.T(), ts.API.db.Update(u), "Error updating new test user") | ||
|
||
token := ts.generateToken(u, nil) | ||
token := ts.generateAccessTokenAndSession(u) | ||
|
||
// request for reauthentication nonce | ||
req := httptest.NewRequest(http.MethodGet, "http://localhost/reauthenticate", nil) | ||
|
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