feat(login): additionally use email/phone for authentication (#4563)

* feat: add ability to disable login by email and phone

* feat: check login by email and phone

* fix: set verified email / phone correctly on notify users

* update projection version

* fix merge

* fix email/phone verified reduce tests

* fix user tests

* loginname check

* cleanup

* fix: update user projection version to handle fixed statement
This commit is contained in:
Livio Spring
2022-10-17 21:19:15 +02:00
committed by GitHub
parent 9ae58b62fd
commit b0b1e94090
54 changed files with 1245 additions and 768 deletions

View File

@@ -17,43 +17,44 @@ import (
)
var (
userQuery = `SELECT projections.users3.id,` +
` projections.users3.creation_date,` +
` projections.users3.change_date,` +
` projections.users3.resource_owner,` +
` projections.users3.sequence,` +
` projections.users3.state,` +
` projections.users3.type,` +
` projections.users3.username,` +
userQuery = `SELECT projections.users4.id,` +
` projections.users4.creation_date,` +
` projections.users4.change_date,` +
` projections.users4.resource_owner,` +
` projections.users4.sequence,` +
` projections.users4.state,` +
` projections.users4.type,` +
` projections.users4.username,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.first_name,` +
` projections.users3_humans.last_name,` +
` projections.users3_humans.nick_name,` +
` projections.users3_humans.display_name,` +
` projections.users3_humans.preferred_language,` +
` projections.users3_humans.gender,` +
` projections.users3_humans.avatar_key,` +
` projections.users3_humans.email,` +
` projections.users3_humans.is_email_verified,` +
` projections.users3_humans.phone,` +
` projections.users3_humans.is_phone_verified,` +
` projections.users3_machines.user_id,` +
` projections.users3_machines.name,` +
` projections.users3_machines.description` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id` +
` LEFT JOIN projections.users3_machines ON projections.users3.id = projections.users3_machines.user_id` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.first_name,` +
` projections.users4_humans.last_name,` +
` projections.users4_humans.nick_name,` +
` projections.users4_humans.display_name,` +
` projections.users4_humans.preferred_language,` +
` projections.users4_humans.gender,` +
` projections.users4_humans.avatar_key,` +
` projections.users4_humans.email,` +
` projections.users4_humans.is_email_verified,` +
` projections.users4_humans.phone,` +
` projections.users4_humans.is_phone_verified,` +
` projections.users4_machines.user_id,` +
` projections.users4_machines.name,` +
` projections.users4_machines.description,` +
` COUNT(*) OVER ()` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id` +
` LEFT JOIN projections.users4_machines ON projections.users4.id = projections.users4_machines.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name)::TEXT[] AS loginnames` +
` FROM projections.login_names AS login_names` +
` WHERE login_names.instance_id = $1` +
` GROUP BY login_names.user_id) AS login_names` +
` ON login_names.user_id = projections.users3.id` +
` ON login_names.user_id = projections.users4.id` +
` LEFT JOIN` +
` (SELECT preferred_login_name.user_id, preferred_login_name.login_name FROM projections.login_names AS preferred_login_name WHERE preferred_login_name.instance_id = $2 AND preferred_login_name.is_primary = $3) AS preferred_login_name` +
` ON preferred_login_name.user_id = projections.users3.id`
` ON preferred_login_name.user_id = projections.users4.id`
userCols = []string{
"id",
"creation_date",
@@ -82,22 +83,23 @@ var (
"user_id",
"name",
"description",
"count",
}
profileQuery = `SELECT projections.users3.id,` +
` projections.users3.creation_date,` +
` projections.users3.change_date,` +
` projections.users3.resource_owner,` +
` projections.users3.sequence,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.first_name,` +
` projections.users3_humans.last_name,` +
` projections.users3_humans.nick_name,` +
` projections.users3_humans.display_name,` +
` projections.users3_humans.preferred_language,` +
` projections.users3_humans.gender,` +
` projections.users3_humans.avatar_key` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id`
profileQuery = `SELECT projections.users4.id,` +
` projections.users4.creation_date,` +
` projections.users4.change_date,` +
` projections.users4.resource_owner,` +
` projections.users4.sequence,` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.first_name,` +
` projections.users4_humans.last_name,` +
` projections.users4_humans.nick_name,` +
` projections.users4_humans.display_name,` +
` projections.users4_humans.preferred_language,` +
` projections.users4_humans.gender,` +
` projections.users4_humans.avatar_key` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id`
profileCols = []string{
"id",
"creation_date",
@@ -113,16 +115,16 @@ var (
"gender",
"avatar_key",
}
emailQuery = `SELECT projections.users3.id,` +
` projections.users3.creation_date,` +
` projections.users3.change_date,` +
` projections.users3.resource_owner,` +
` projections.users3.sequence,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.email,` +
` projections.users3_humans.is_email_verified` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id`
emailQuery = `SELECT projections.users4.id,` +
` projections.users4.creation_date,` +
` projections.users4.change_date,` +
` projections.users4.resource_owner,` +
` projections.users4.sequence,` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.email,` +
` projections.users4_humans.is_email_verified` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id`
emailCols = []string{
"id",
"creation_date",
@@ -133,16 +135,16 @@ var (
"email",
"is_email_verified",
}
phoneQuery = `SELECT projections.users3.id,` +
` projections.users3.creation_date,` +
` projections.users3.change_date,` +
` projections.users3.resource_owner,` +
` projections.users3.sequence,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.phone,` +
` projections.users3_humans.is_phone_verified` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id`
phoneQuery = `SELECT projections.users4.id,` +
` projections.users4.creation_date,` +
` projections.users4.change_date,` +
` projections.users4.resource_owner,` +
` projections.users4.sequence,` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.phone,` +
` projections.users4_humans.is_phone_verified` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id`
phoneCols = []string{
"id",
"creation_date",
@@ -153,14 +155,14 @@ var (
"phone",
"is_phone_verified",
}
userUniqueQuery = `SELECT projections.users3.id,` +
` projections.users3.state,` +
` projections.users3.username,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.email,` +
` projections.users3_humans.is_email_verified` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id`
userUniqueQuery = `SELECT projections.users4.id,` +
` projections.users4.state,` +
` projections.users4.username,` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.email,` +
` projections.users4_humans.is_email_verified` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id`
userUniqueCols = []string{
"id",
"state",
@@ -169,42 +171,43 @@ var (
"email",
"is_email_verified",
}
notifyUserQuery = `SELECT projections.users3.id,` +
` projections.users3.creation_date,` +
` projections.users3.change_date,` +
` projections.users3.resource_owner,` +
` projections.users3.sequence,` +
` projections.users3.state,` +
` projections.users3.type,` +
` projections.users3.username,` +
notifyUserQuery = `SELECT projections.users4.id,` +
` projections.users4.creation_date,` +
` projections.users4.change_date,` +
` projections.users4.resource_owner,` +
` projections.users4.sequence,` +
` projections.users4.state,` +
` projections.users4.type,` +
` projections.users4.username,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.first_name,` +
` projections.users3_humans.last_name,` +
` projections.users3_humans.nick_name,` +
` projections.users3_humans.display_name,` +
` projections.users3_humans.preferred_language,` +
` projections.users3_humans.gender,` +
` projections.users3_humans.avatar_key,` +
` projections.users3_notifications.user_id,` +
` projections.users3_notifications.last_email,` +
` projections.users3_notifications.verified_email,` +
` projections.users3_notifications.last_phone,` +
` projections.users3_notifications.verified_phone,` +
` projections.users3_notifications.password_set` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id` +
` LEFT JOIN projections.users3_notifications ON projections.users3.id = projections.users3_notifications.user_id` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.first_name,` +
` projections.users4_humans.last_name,` +
` projections.users4_humans.nick_name,` +
` projections.users4_humans.display_name,` +
` projections.users4_humans.preferred_language,` +
` projections.users4_humans.gender,` +
` projections.users4_humans.avatar_key,` +
` projections.users4_notifications.user_id,` +
` projections.users4_notifications.last_email,` +
` projections.users4_notifications.verified_email,` +
` projections.users4_notifications.last_phone,` +
` projections.users4_notifications.verified_phone,` +
` projections.users4_notifications.password_set,` +
` COUNT(*) OVER ()` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id` +
` LEFT JOIN projections.users4_notifications ON projections.users4.id = projections.users4_notifications.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) AS loginnames` +
` FROM projections.login_names AS login_names` +
` WHERE login_names.instance_id = $1` +
` GROUP BY login_names.user_id) AS login_names` +
` ON login_names.user_id = projections.users3.id` +
` ON login_names.user_id = projections.users4.id` +
` LEFT JOIN` +
` (SELECT preferred_login_name.user_id, preferred_login_name.login_name FROM projections.login_names AS preferred_login_name WHERE preferred_login_name.instance_id = $2 AND preferred_login_name.is_primary = $3) AS preferred_login_name` +
` ON preferred_login_name.user_id = projections.users3.id`
` ON preferred_login_name.user_id = projections.users4.id`
notifyUserCols = []string{
"id",
"creation_date",
@@ -232,44 +235,45 @@ var (
"last_phone",
"verified_phone",
"password_set",
"count",
}
usersQuery = `SELECT projections.users3.id,` +
` projections.users3.creation_date,` +
` projections.users3.change_date,` +
` projections.users3.resource_owner,` +
` projections.users3.sequence,` +
` projections.users3.state,` +
` projections.users3.type,` +
` projections.users3.username,` +
usersQuery = `SELECT projections.users4.id,` +
` projections.users4.creation_date,` +
` projections.users4.change_date,` +
` projections.users4.resource_owner,` +
` projections.users4.sequence,` +
` projections.users4.state,` +
` projections.users4.type,` +
` projections.users4.username,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` projections.users3_humans.user_id,` +
` projections.users3_humans.first_name,` +
` projections.users3_humans.last_name,` +
` projections.users3_humans.nick_name,` +
` projections.users3_humans.display_name,` +
` projections.users3_humans.preferred_language,` +
` projections.users3_humans.gender,` +
` projections.users3_humans.avatar_key,` +
` projections.users3_humans.email,` +
` projections.users3_humans.is_email_verified,` +
` projections.users3_humans.phone,` +
` projections.users3_humans.is_phone_verified,` +
` projections.users3_machines.user_id,` +
` projections.users3_machines.name,` +
` projections.users3_machines.description,` +
` projections.users4_humans.user_id,` +
` projections.users4_humans.first_name,` +
` projections.users4_humans.last_name,` +
` projections.users4_humans.nick_name,` +
` projections.users4_humans.display_name,` +
` projections.users4_humans.preferred_language,` +
` projections.users4_humans.gender,` +
` projections.users4_humans.avatar_key,` +
` projections.users4_humans.email,` +
` projections.users4_humans.is_email_verified,` +
` projections.users4_humans.phone,` +
` projections.users4_humans.is_phone_verified,` +
` projections.users4_machines.user_id,` +
` projections.users4_machines.name,` +
` projections.users4_machines.description,` +
` COUNT(*) OVER ()` +
` FROM projections.users3` +
` LEFT JOIN projections.users3_humans ON projections.users3.id = projections.users3_humans.user_id` +
` LEFT JOIN projections.users3_machines ON projections.users3.id = projections.users3_machines.user_id` +
` FROM projections.users4` +
` LEFT JOIN projections.users4_humans ON projections.users4.id = projections.users4_humans.user_id` +
` LEFT JOIN projections.users4_machines ON projections.users4.id = projections.users4_machines.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) AS loginnames` +
` FROM projections.login_names AS login_names` +
` GROUP BY login_names.user_id) AS login_names` +
` ON login_names.user_id = projections.users3.id` +
` ON login_names.user_id = projections.users4.id` +
` LEFT JOIN` +
` (SELECT preferred_login_name.user_id, preferred_login_name.login_name FROM projections.login_names AS preferred_login_name WHERE preferred_login_name.is_primary = $1) AS preferred_login_name` +
` ON preferred_login_name.user_id = projections.users3.id`
` ON preferred_login_name.user_id = projections.users4.id`
usersCols = []string{
"id",
"creation_date",
@@ -370,6 +374,7 @@ func Test_UserPrepares(t *testing.T) {
nil,
nil,
nil,
1,
},
),
},
@@ -436,6 +441,7 @@ func Test_UserPrepares(t *testing.T) {
"id",
"name",
"description",
1,
},
),
},
@@ -879,6 +885,7 @@ func Test_UserPrepares(t *testing.T) {
"lastPhone",
"verifiedPhone",
true,
1,
},
),
},
@@ -942,6 +949,7 @@ func Test_UserPrepares(t *testing.T) {
nil,
nil,
nil,
1,
},
),
err: func(err error) (error, bool) {