Skip to content

Commit 86655ea

Browse files
authored
Phone verification bypass number (#2119)
* add bypass phone number * trim earlier
1 parent 5f8f094 commit 86655ea

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

packages/pds/src/api/com/atproto/server/createAccount.ts

+41-27
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,11 @@ export default function (server: Server, ctx: AppContext) {
4444
const now = new Date().toISOString()
4545
const passwordScrypt = await scrypt.genSaltAndHash(password)
4646

47-
let verificationPhone: string | undefined = undefined
48-
if (ctx.cfg.phoneVerification.required && ctx.twilio) {
49-
if (!input.body.verificationPhone) {
50-
throw new InvalidRequestError(
51-
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
52-
'InvalidPhoneVerification',
53-
)
54-
} else if (!input.body.verificationCode) {
55-
throw new InvalidRequestError(
56-
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
57-
'InvalidPhoneVerification',
58-
)
59-
}
60-
verificationPhone = ctx.twilio.normalizePhoneNumber(
61-
input.body.verificationPhone,
62-
)
63-
const verified = await ctx.twilio.verifyCode(
64-
verificationPhone,
65-
input.body.verificationCode.trim(),
66-
)
67-
if (!verified) {
68-
throw new InvalidRequestError(
69-
'Could not verify phone number. Please try again.',
70-
'InvalidPhoneVerification',
71-
)
72-
}
73-
}
47+
const verificationPhone = await ensurePhoneVerification(
48+
ctx,
49+
input.body.verificationPhone,
50+
input.body.verificationCode?.trim(),
51+
)
7452

7553
const result = await ctx.db.transaction(async (dbTxn) => {
7654
const actorTxn = ctx.services.account(dbTxn)
@@ -491,6 +469,42 @@ const ensureUnusedHandleAndEmail = async (
491469
}
492470
}
493471

472+
const ensurePhoneVerification = async (
473+
ctx: AppContext,
474+
phone?: string,
475+
code?: string,
476+
): Promise<string | undefined> => {
477+
if (!ctx.cfg.phoneVerification.required || !ctx.twilio) {
478+
return
479+
}
480+
481+
if (!phone) {
482+
throw new InvalidRequestError(
483+
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
484+
'InvalidPhoneVerification',
485+
)
486+
}
487+
if (ctx.cfg.phoneVerification.bypassPhoneNumber === phone) {
488+
return undefined
489+
}
490+
491+
if (!code) {
492+
throw new InvalidRequestError(
493+
`Text verification is now required on this server. Please make sure you're using the latest version of the Bluesky app.`,
494+
'InvalidPhoneVerification',
495+
)
496+
}
497+
const normalizedPhone = ctx.twilio.normalizePhoneNumber(phone)
498+
const verified = await ctx.twilio.verifyCode(normalizedPhone, code)
499+
if (!verified) {
500+
throw new InvalidRequestError(
501+
'Could not verify phone number. Please try again.',
502+
'InvalidPhoneVerification',
503+
)
504+
}
505+
return normalizedPhone
506+
}
507+
494508
const randomIndexByWeight = (weights) => {
495509
let sum = 0
496510
const cumulative = weights.map((weight) => {

packages/pds/src/config/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const envToCfg = (env: ServerEnvironment): ServerConfig => {
137137
twilioAccountSid: env.twilioAccountSid,
138138
twilioServiceSid: env.twilioServiceSid,
139139
accountsPerPhoneNumber: env.accountsPerPhoneNumber ?? 3,
140+
bypassPhoneNumber: env.bypassPhoneNumber,
140141
}
141142
}
142143

@@ -322,6 +323,7 @@ export type PhoneVerificationConfig =
322323
twilioAccountSid: string
323324
twilioServiceSid: string
324325
accountsPerPhoneNumber: number
326+
bypassPhoneNumber?: string
325327
}
326328
| {
327329
required: false

packages/pds/src/config/env.ts

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const readEnv = (): ServerEnvironment => {
5252
// phone verification
5353
phoneVerificationRequired: envBool('PDS_PHONE_VERIFICATION_REQUIRED'),
5454
accountsPerPhoneNumber: envInt('PDS_ACCOUNTS_PER_PHONE_NUMBER'),
55+
bypassPhoneNumber: envStr('PDS_BYPASS_PHONE_NUMBER'),
5556
twilioAccountSid: envStr('PDS_TWILIO_ACCOUNT_SID'),
5657
twilioAuthToken: envStr('PDS_TWILIO_AUTH_TOKEN'),
5758
twilioServiceSid: envStr('PDS_TWILIO_SERVICE_SID'),
@@ -166,6 +167,7 @@ export type ServerEnvironment = {
166167
// phone verification
167168
phoneVerificationRequired?: boolean
168169
accountsPerPhoneNumber?: number
170+
bypassPhoneNumber?: string
169171
twilioAccountSid?: string
170172
twilioAuthToken?: string
171173
twilioServiceSid?: string

0 commit comments

Comments
 (0)