Skip to content
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

Phone verification bypass number #2119

Merged
merged 2 commits into from
Feb 1, 2024
Merged
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
68 changes: 41 additions & 27 deletions packages/pds/src/api/com/atproto/server/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,11 @@ export default function (server: Server, ctx: AppContext) {
const now = new Date().toISOString()
const passwordScrypt = await scrypt.genSaltAndHash(password)

let verificationPhone: string | undefined = undefined
if (ctx.cfg.phoneVerification.required && ctx.twilio) {
if (!input.body.verificationPhone) {
throw new InvalidRequestError(
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
'InvalidPhoneVerification',
)
} else if (!input.body.verificationCode) {
throw new InvalidRequestError(
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
'InvalidPhoneVerification',
)
}
verificationPhone = ctx.twilio.normalizePhoneNumber(
input.body.verificationPhone,
)
const verified = await ctx.twilio.verifyCode(
verificationPhone,
input.body.verificationCode.trim(),
)
if (!verified) {
throw new InvalidRequestError(
'Could not verify phone number. Please try again.',
'InvalidPhoneVerification',
)
}
}
const verificationPhone = await ensurePhoneVerification(
ctx,
input.body.verificationPhone,
input.body.verificationCode?.trim(),
)

const result = await ctx.db.transaction(async (dbTxn) => {
const actorTxn = ctx.services.account(dbTxn)
Expand Down Expand Up @@ -486,6 +464,42 @@ const ensureUnusedHandleAndEmail = async (
}
}

const ensurePhoneVerification = async (
ctx: AppContext,
phone?: string,
code?: string,
): Promise<string | undefined> => {
if (!ctx.cfg.phoneVerification.required || !ctx.twilio) {
return
}

if (!phone) {
throw new InvalidRequestError(
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
'InvalidPhoneVerification',
)
}
if (ctx.cfg.phoneVerification.bypassPhoneNumber === phone) {
return undefined
}

if (!code) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might consider trimming the code before this check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice yup 👍

throw new InvalidRequestError(
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
'InvalidPhoneVerification',
)
}
const normalizedPhone = ctx.twilio.normalizePhoneNumber(phone)
const verified = await ctx.twilio.verifyCode(normalizedPhone, code)
if (!verified) {
throw new InvalidRequestError(
'Could not verify phone number. Please try again.',
'InvalidPhoneVerification',
)
}
return normalizedPhone
}

const randomIndexByWeight = (weights) => {
let sum = 0
const cumulative = weights.map((weight) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/pds/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const envToCfg = (env: ServerEnvironment): ServerConfig => {
twilioAccountSid: env.twilioAccountSid,
twilioServiceSid: env.twilioServiceSid,
accountsPerPhoneNumber: env.accountsPerPhoneNumber ?? 3,
bypassPhoneNumber: env.bypassPhoneNumber,
}
}

Expand Down Expand Up @@ -322,6 +323,7 @@ export type PhoneVerificationConfig =
twilioAccountSid: string
twilioServiceSid: string
accountsPerPhoneNumber: number
bypassPhoneNumber?: string
}
| {
required: false
Expand Down
2 changes: 2 additions & 0 deletions packages/pds/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const readEnv = (): ServerEnvironment => {
// phone verification
phoneVerificationRequired: envBool('PDS_PHONE_VERIFICATION_REQUIRED'),
accountsPerPhoneNumber: envInt('PDS_ACCOUNTS_PER_PHONE_NUMBER'),
bypassPhoneNumber: envStr('PDS_BYPASS_PHONE_NUMBER'),
twilioAccountSid: envStr('PDS_TWILIO_ACCOUNT_SID'),
twilioAuthToken: envStr('PDS_TWILIO_AUTH_TOKEN'),
twilioServiceSid: envStr('PDS_TWILIO_SERVICE_SID'),
Expand Down Expand Up @@ -166,6 +167,7 @@ export type ServerEnvironment = {
// phone verification
phoneVerificationRequired?: boolean
accountsPerPhoneNumber?: number
bypassPhoneNumber?: string
twilioAccountSid?: string
twilioAuthToken?: string
twilioServiceSid?: string
Expand Down
Loading