-
-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add phone number validation to user APIs #5882
Open
darcyYe
wants to merge
3
commits into
master
Choose a base branch
from
yemq-log-8880-add-phone-number-validation-to-user-APIs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+286
−141
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
/* eslint-disable max-lines */ | ||
import type { BindMfa, CreateUser, MfaVerification, Scope, User } from '@logto/schemas'; | ||
import { MfaFactor, RoleType, Users, UsersPasswordEncryptionMethod } from '@logto/schemas'; | ||
import { generateStandardId, generateStandardShortId } from '@logto/shared'; | ||
import { deduplicateByKey, type Nullable } from '@silverhand/essentials'; | ||
import { generateStandardShortId, generateStandardId } from '@logto/shared'; | ||
import type { Nullable } from '@silverhand/essentials'; | ||
import { deduplicateByKey, conditional } from '@silverhand/essentials'; | ||
import { argon2Verify, bcryptVerify, md5, sha1, sha256 } from 'hash-wasm'; | ||
import pRetry from 'p-retry'; | ||
|
||
|
@@ -14,6 +16,7 @@ | |
import assertThat from '#src/utils/assert-that.js'; | ||
import { encryptPassword } from '#src/utils/password.js'; | ||
import type { OmitAutoSetFields } from '#src/utils/sql.js'; | ||
import { getValidPhoneNumber } from '#src/utils/user.js'; | ||
|
||
export const encryptUserPassword = async ( | ||
password: string | ||
|
@@ -90,7 +93,7 @@ | |
hasUserWithId, | ||
hasUserWithPhone, | ||
findUsersByIds, | ||
updateUserById, | ||
updateUserById: updateUserByIdQuery, | ||
findUserById, | ||
}, | ||
usersRoles: { findUsersRolesByRoleId, findUsersRolesByUserId }, | ||
|
@@ -115,6 +118,29 @@ | |
{ retries, factor: 0 } // No need for exponential backoff | ||
); | ||
|
||
const updateUserById = async ( | ||
id: string, | ||
set: Partial<OmitAutoSetFields<CreateUser>>, | ||
jsonbMode?: 'replace' | 'merge' | ||
) => { | ||
const validPhoneNumber = conditional( | ||
typeof set.primaryPhone === 'string' && getValidPhoneNumber(set.primaryPhone) | ||
); | ||
|
||
return updateUserByIdQuery( | ||
id, | ||
{ | ||
...set, | ||
...conditional( | ||
validPhoneNumber && { | ||
primaryPhone: validPhoneNumber, | ||
} | ||
), | ||
}, | ||
jsonbMode | ||
); | ||
}; | ||
|
||
const insertUser = async ( | ||
data: OmitAutoSetFields<CreateUser>, | ||
additionalRoleNames: string[] | ||
|
@@ -127,12 +153,19 @@ | |
|
||
assertThat(parameterRoles.length === roleNames.length, 'role.default_role_missing'); | ||
|
||
const validPhoneNumber = conditional( | ||
typeof data.primaryPhone === 'string' && getValidPhoneNumber(data.primaryPhone) | ||
); | ||
|
||
return pool.transaction(async (connection) => { | ||
const insertUserQuery = buildInsertIntoWithPool(connection)(Users, { | ||
returning: true, | ||
}); | ||
|
||
const user = await insertUserQuery(data); | ||
const user = await insertUserQuery({ | ||
...data, | ||
...conditional(validPhoneNumber && { primaryPhone: validPhoneNumber }), | ||
}); | ||
const roles = deduplicateByKey([...parameterRoles, ...defaultRoles], 'id'); | ||
|
||
if (roles.length > 0) { | ||
|
@@ -145,7 +178,7 @@ | |
const provisionOrganizations = async (): Promise<readonly string[]> => { | ||
// Just-in-time organization provisioning | ||
const userEmailDomain = data.primaryEmail?.split('@')[1]; | ||
// TODO: Remove this check when launching | ||
if (EnvSet.values.isDevFeaturesEnabled && userEmailDomain) { | ||
const organizationQueries = new OrganizationQueries(connection); | ||
const organizationIds = await organizationQueries.emailDomains.getOrganizationIdsByDomain( | ||
|
@@ -245,7 +278,7 @@ | |
}; | ||
|
||
const addUserMfaVerification = async (userId: string, payload: BindMfa) => { | ||
// TODO @sijie use jsonb array append | ||
Check warning on line 281 in packages/core/src/libraries/user.ts GitHub Actions / ESLint Report Analysispackages/core/src/libraries/user.ts#L281
|
||
const { mfaVerifications } = await findUserById(userId); | ||
await updateUserById(userId, { | ||
mfaVerifications: [...mfaVerifications, converBindMfaToMfaVerification(payload)], | ||
|
@@ -336,5 +369,7 @@ | |
verifyUserPassword, | ||
signOutUser, | ||
findUserSsoIdentities, | ||
updateUserById, | ||
}; | ||
}; | ||
/* eslint-enable max-lines */ |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should put the validPhoneNumber validation logic at the API koaGuard level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering to make this phone number guard at API level. But since the DB insert and update operation could bypass the API-level check since some of them were used in scenarios other than in API handlers.
Make this change on the DB queries/libraries methods IMO should be the safest and the most easy-to-maintain way of doing this.