Skip to content

Commit

Permalink
fix: email_verified field not being updated on signup confirmation (#…
Browse files Browse the repository at this point in the history
…1868)

## What kind of change does this PR introduce?
* Addresses #1620
  • Loading branch information
kangmingtay authored Dec 12, 2024
1 parent 40e0de1 commit 483463e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
19 changes: 19 additions & 0 deletions internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,25 @@ func (a *API) signupVerify(r *http.Request, ctx context.Context, conn *storage.C
if terr = user.Confirm(tx); terr != nil {
return internalServerError("Error confirming user").WithInternalError(terr)
}

// on signupVerify, the user will always only have an email identity
// so we can safely assume that the first identity is the email identity
//
// we still check for the length of the identities slice to be safe.
if len(user.Identities) != 0 {
if len(user.Identities) > 1 {
return internalServerError("User has more than one identity on signup")
}
emailIdentity := user.Identities[0]
if emailIdentity.Email != user.Email {
return internalServerError("User email identity does not match user email")
}
if terr = emailIdentity.UpdateIdentityData(tx, map[string]interface{}{
"email_verified": true,
}); terr != nil {
return internalServerError("Error updating email identity").WithInternalError(terr)
}
}
return nil
})
if err != nil {
Expand Down
35 changes: 23 additions & 12 deletions internal/api/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (ts *VerifyTestSuite) SetupTest() {
u, err := models.NewUser("12345678", "test@example.com", "password", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error creating test user model")
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new test user")

// Create identity
i, err := models.NewIdentity(u, "email", map[string]interface{}{
"sub": u.ID.String(),
"email": "test@example.com",
"email_verified": false,
})
require.NoError(ts.T(), err, "Error creating test identity model")
require.NoError(ts.T(), ts.API.db.Create(i), "Error saving new test identity")
}

func (ts *VerifyTestSuite) TestVerifyPasswordRecovery() {
Expand Down Expand Up @@ -673,6 +682,8 @@ func (ts *VerifyTestSuite) TestVerifySignupWithRedirectURLContainedPath() {
u, err = models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.UserMetaData["email_verified"].(bool))
assert.True(ts.T(), u.Identities[0].IdentityData["email_verified"].(bool))
})
}
}
Expand Down Expand Up @@ -875,6 +886,18 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Signup Token Hash",
sentTime: time.Now(),
body: map[string]interface{}{
"type": mail.SignupVerification,
"token_hash": crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
expected: expected{
code: http.StatusOK,
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Recovery OTP",
sentTime: time.Now(),
Expand Down Expand Up @@ -940,18 +963,6 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
tokenHash: crypto.GenerateTokenHash(u.PhoneChange, "123456"),
},
},
{
desc: "Valid Signup Token Hash",
sentTime: time.Now(),
body: map[string]interface{}{
"type": mail.SignupVerification,
"token_hash": crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
expected: expected{
code: http.StatusOK,
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Email Change Token Hash",
sentTime: time.Now(),
Expand Down
6 changes: 6 additions & 0 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ func (u *User) Confirm(tx *storage.Connection) error {
return err
}

if err := u.UpdateUserMetaData(tx, map[string]interface{}{
"email_verified": true,
}); err != nil {
return err
}

if err := ClearAllOneTimeTokensForUser(tx, u.ID); err != nil {
return err
}
Expand Down

0 comments on commit 483463e

Please sign in to comment.