Skip to content

Commit

Permalink
feat: remove legacy lookup in users for one_time_tokens (phase II) (s…
Browse files Browse the repository at this point in the history
…upabase#1569)

Removes legacy lookups in `auth.users` for when a corresponding entry in
`one_time_tokens` is not found.

Phase II of the refactor, based on supabase#1558, to be released after it's
deployed for a few days.

---------

Co-authored-by: Kang Ming <kang.ming1996@gmail.com>
  • Loading branch information
hf and kangmingtay authored May 17, 2024
1 parent c64ae3d commit 39ca026
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 340 deletions.
5 changes: 5 additions & 0 deletions internal/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,11 @@ func (ts *AdminTestSuite) TestAdminUserSoftDeletion() {
"provider": "email",
}
require.NoError(ts.T(), ts.API.db.Create(u))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.ConfirmationToken, models.ConfirmationToken))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.RecoveryToken, models.RecoveryToken))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.EmailChangeTokenCurrent, models.EmailChangeTokenCurrent))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.EmailChangeTokenNew, models.EmailChangeTokenNew))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetPhone(), u.PhoneChangeToken, models.PhoneChangeToken))

// create user identities
_, err = ts.API.createNewIdentity(ts.API.db, u, "email", map[string]interface{}{
Expand Down
4 changes: 4 additions & 0 deletions internal/api/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (ts *ExternalTestSuite) createUser(providerId string, email string, name st
ts.Require().NoError(err, "Error making new user")
ts.Require().NoError(ts.API.db.Create(u), "Error creating user")

if confirmationToken != "" {
ts.Require().NoError(models.CreateOneTimeToken(ts.API.db, u.ID, email, u.ConfirmationToken, models.ConfirmationToken), "Error creating one-time confirmation/invite token")
}

i, err := models.NewIdentity(u, "email", map[string]interface{}{
"sub": u.ID.String(),
"email": email,
Expand Down
1 change: 1 addition & 0 deletions internal/api/invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (ts *InviteTestSuite) TestVerifyInvite() {
user.ConfirmationToken = crypto.GenerateTokenHash(c.email, c.requestBody["token"].(string))
require.NoError(ts.T(), err)
require.NoError(ts.T(), ts.API.db.Create(user))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, user.ID, user.GetEmail(), user.ConfirmationToken, models.ConfirmationToken))

// Find test user
_, err = models.FindUserByEmailAndAudience(ts.API.db, c.email, ts.Config.JWT.Aud)
Expand Down
4 changes: 4 additions & 0 deletions internal/api/resend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,24 @@ func (ts *ResendTestSuite) TestResendSuccess() {
u.EmailChangeSentAt = &now
u.EmailChangeTokenNew = "123456"
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new test user")
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.ConfirmationToken, models.ConfirmationToken))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.EmailChange, u.EmailChangeTokenNew, models.EmailChangeTokenNew))

phoneUser, err := models.NewUser("1234567890", "", "password", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error creating test user model")
phoneUser.EmailChange = "bar@example.com"
phoneUser.EmailChangeSentAt = &now
phoneUser.EmailChangeTokenNew = "123456"
require.NoError(ts.T(), ts.API.db.Create(phoneUser), "Error saving new test user")
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, phoneUser.ID, phoneUser.EmailChange, phoneUser.EmailChangeTokenNew, models.EmailChangeTokenNew))

emailUser, err := models.NewUser("", "bar@example.com", "password", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error creating test user model")
phoneUser.PhoneChange = "1234567890"
phoneUser.PhoneChangeSentAt = &now
phoneUser.PhoneChangeToken = "123456"
require.NoError(ts.T(), ts.API.db.Create(emailUser), "Error saving new test user")
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, phoneUser.ID, phoneUser.PhoneChange, phoneUser.PhoneChangeToken, models.PhoneChangeToken))

cases := []struct {
desc string
Expand Down
4 changes: 3 additions & 1 deletion internal/api/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
mail "github.com/supabase/auth/internal/mailer"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

mail "github.com/supabase/auth/internal/mailer"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -127,6 +128,7 @@ func (ts *SignupTestSuite) TestVerifySignup() {
user.ConfirmationSentAt = &now
require.NoError(ts.T(), err)
require.NoError(ts.T(), ts.API.db.Create(user))
require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, user.ID, user.GetEmail(), user.ConfirmationToken, models.ConfirmationToken))

// Find test user
u, err := models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
Expand Down
Loading

0 comments on commit 39ca026

Please sign in to comment.