Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions internal/models/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ func NewIdentity(user *User, provider string, identityData map[string]interface{
Provider: provider,
LastSignInAt: &now,
}
if email, ok := identityData["email"]; ok {
identity.Email = storage.NullString(email.(string))
// identityData may originate from user-supplied metadata (e.g. the
// signup `data` field), so the email may be absent or a non-string
// value. Only set the email when it is actually a string to avoid a
// panic from an unchecked type assertion.
if email, ok := identityData["email"].(string); ok {
identity.Email = storage.NullString(email)
}

return identity, nil
Expand All @@ -71,8 +75,8 @@ func (i *Identity) BeforeCreate(tx *pop.Connection) error {
}

func (i *Identity) BeforeUpdate(tx *pop.Connection) error {
if _, ok := i.IdentityData["email"]; ok {
i.IdentityData["email"] = strings.ToLower(i.IdentityData["email"].(string))
if email, ok := i.IdentityData["email"].(string); ok {
i.IdentityData["email"] = strings.ToLower(email)
}
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions internal/models/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ func (ts *IdentityTestSuite) TestNewIdentity() {
require.NoError(ts.T(), err)
require.Equal(ts.T(), u.ID, identity.UserID)
})

ts.Run("Test create identity with string email", func() {
identityData := map[string]interface{}{"sub": uuid.Nil.String(), "email": "test@supabase.io"}
identity, err := NewIdentity(u, "email", identityData)
require.NoError(ts.T(), err)
require.Equal(ts.T(), "test@supabase.io", identity.GetEmail())
})

// A phone signup omits the (empty) email claim, so a user-supplied
// `data.email` can reach identityData as a non-string value. NewIdentity
// must not panic on it. Regression test for supabase/auth#2268.
ts.Run("Test create identity with non-string email does not panic", func() {
for _, email := range []interface{}{nil, 123, true, map[string]interface{}{}} {
identityData := map[string]interface{}{"sub": uuid.Nil.String(), "email": email}
identity, err := NewIdentity(u, "phone", identityData)
require.NoError(ts.T(), err)
require.Empty(ts.T(), identity.GetEmail(), "email should be unset for non-string value %#v", email)
}
})
}

func (ts *IdentityTestSuite) TestFindUserIdentities() {
Expand Down