Skip to content

Commit

Permalink
add try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
vpomerleau committed Feb 1, 2025
1 parent 518f015 commit 255780d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 41 deletions.
60 changes: 40 additions & 20 deletions libs/accounts/recovery-phone/src/lib/recovery-phone.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,33 @@ export class RecoveryPhoneService {

const code = await this.otpCode.generateCode();

const smsBody = localizedMessageBody
? `${localizedMessageBody.part1} ${code} ${localizedMessageBody.part2}`
: `${code}`;
const msg = await this.smsManager.sendSMS({
to: phoneNumber,
body: smsBody,
});

if (!this.isSuccessfulSmsSend(msg)) {
return false;
}
await this.recoveryPhoneManager.storeUnconfirmed(
uid,
code,
phoneNumber,
true
);
return true;

const smsBody = localizedMessageBody
? `${localizedMessageBody.part1} ${code} ${localizedMessageBody.part2}`
: `${code}`;

let msg = {} as MessageInstance;
try {
msg = await this.smsManager.sendSMS({
to: phoneNumber,
body: smsBody,
});
} catch (e) {
console.log(e);
// retry sending without localized body
msg = await this.smsManager.sendSMS({
to: phoneNumber,
body: code,
});
}

return this.isSuccessfulSmsSend(msg);
}

/**
Expand Down Expand Up @@ -299,20 +308,31 @@ export class RecoveryPhoneService {
const { phoneNumber } =
await this.recoveryPhoneManager.getConfirmedPhoneNumber(uid);
const code = await this.otpCode.generateCode();
const smsBody = localizedMessageBody
? `${localizedMessageBody.part1} ${code} ${localizedMessageBody.part2}`
: `${code}`;

await this.recoveryPhoneManager.storeUnconfirmed(
uid,
code,
phoneNumber,
false
);
const msg = await this.smsManager.sendSMS({
to: phoneNumber,
body: smsBody,
});

const smsBody = localizedMessageBody
? `${localizedMessageBody.part1} ${code} ${localizedMessageBody.part2}`
: `${code}`;

let msg = {} as MessageInstance;
try {
msg = await this.smsManager.sendSMS({
to: phoneNumber,
body: smsBody,
});
} catch (e) {
console.log(e);
// retry sending without localized body
msg = await this.smsManager.sendSMS({
to: phoneNumber,
body: code,
});
}

return this.isSuccessfulSmsSend(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fxa-auth-server/test/client/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ module.exports = (config) => {
});
};

ClientApi.prototype.recoveryPhoneNumberCreate = async function (
ClientApi.prototype.recoveryPhoneCreate = async function (
sessionTokenHex,
phoneNumber
) {
Expand Down
7 changes: 4 additions & 3 deletions packages/fxa-auth-server/test/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,15 @@ module.exports = (config) => {
}
};

Client.prototype.recoveryPhoneNumberCreate = async function (phoneNumber) {
Client.prototype.recoveryPhoneCreate = async function (phoneNumber) {
if (this.sessionToken) {
const resp = await this.api.recoveryPhoneNumberCreate(
const resp = await this.api.recoveryPhoneCreate(
this.sessionToken,
phoneNumber
);
return resp;
}
return;
};

Client.prototype.recoveryPhoneAvailable = async function () {
Expand Down Expand Up @@ -391,7 +392,7 @@ module.exports = (config) => {

Client.prototype.recoveryPhoneCreate = async function (phoneNumber) {
if (this.sessionToken) {
const resp = await this.api.recoveryPhoneNumberCreate(
const resp = await this.api.recoveryPhoneCreate(
this.sessionToken,
phoneNumber
);
Expand Down
2 changes: 1 addition & 1 deletion packages/fxa-auth-server/test/mail_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function printMatchingKeys(startUp = false) {
}
}
} catch (error) {
console.error('Failed to retrieve keys:', error);
// do nothing
} finally {
// 1s delay seems reasonable
setTimeout(printMatchingKeys, 1000);
Expand Down
32 changes: 16 additions & 16 deletions packages/fxa-auth-server/test/remote/recovery_phone_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe(`#integration - recovery phone`, function () {
// Different magic numbers correspond to different scenarios. e.g. reassigned numbers, sim swapping,
// etc. See the following documentation for info in the event tests need to consider extra states.
// https://www.twilio.com/docs/lookup/magic-numbers-for-lookup
const phoneNumber = '+12345678900';
const phoneNumber = '+14159929960';
const password = 'password';

before(async function () {
Expand Down Expand Up @@ -116,7 +116,7 @@ describe(`#integration - recovery phone`, function () {
if (!isTwilioConfigured) {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}
const createResp = await client.recoveryPhoneNumberCreate(phoneNumber);
const createResp = await client.recoveryPhoneCreate(phoneNumber);
const codeSent = await redisUtil.recoveryPhone.getCode(client.uid);
const confirmResp = await client.recoveryPhoneConfirmSetup(codeSent);
const checkResp = await client.recoveryPhoneNumber();
Expand All @@ -134,7 +134,7 @@ describe(`#integration - recovery phone`, function () {
}

// Add recovery phone
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
await client.recoveryPhoneConfirmSetup(
await redisUtil.recoveryPhone.getCode(client.uid)
);
Expand Down Expand Up @@ -172,7 +172,7 @@ describe(`#integration - recovery phone`, function () {
if (!isTwilioConfigured) {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
await client.recoveryPhoneConfirmSetup(
await redisUtil.recoveryPhone.getCode(client.uid)
);
Expand All @@ -194,7 +194,7 @@ describe(`#integration - recovery phone`, function () {
let error;

try {
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
} catch (err) {
error = err;
}
Expand All @@ -206,8 +206,8 @@ describe(`#integration - recovery phone`, function () {
if (!isTwilioConfigured) {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}
await client.recoveryPhoneNumberCreate(phoneNumber);
const createResp = await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
const createResp = await client.recoveryPhoneCreate(phoneNumber);

assert.equal(createResp.status, 'success');
});
Expand All @@ -217,7 +217,7 @@ describe(`#integration - recovery phone`, function () {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}

await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);

let error;
try {
Expand All @@ -233,13 +233,13 @@ describe(`#integration - recovery phone`, function () {
if (!isTwilioConfigured) {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
const code = await redisUtil.recoveryPhone.getCode(client.uid);
await client.recoveryPhoneConfirmSetup(code);

let error;
try {
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
const code = await redisUtil.recoveryPhone.getCode(client.uid);
await client.recoveryPhoneConfirmSetup(code);
} catch (err) {
Expand All @@ -253,7 +253,7 @@ describe(`#integration - recovery phone`, function () {
if (!isTwilioConfigured) {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
const code = await redisUtil.recoveryPhone.getCode(client.uid);
await client.recoveryPhoneConfirmSetup(code);

Expand All @@ -277,7 +277,7 @@ describe(`#integration - recovery phone - customs checks`, function () {
customsUrl: config.customsUrl,
};
const password = 'password';
const phoneNumber = '+12345678900';
const phoneNumber = '+14159929960';

before(async function () {
config.securityHistory.ipProfiling.allowedRecency = 0;
Expand Down Expand Up @@ -314,12 +314,12 @@ describe(`#integration - recovery phone - customs checks`, function () {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}

await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);

let error;
try {
for (let i = 0; i < 9; i++) {
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
}
} catch (err) {
error = err;
Expand All @@ -335,7 +335,7 @@ describe(`#integration - recovery phone - customs checks`, function () {
}

// One code gets sent here
await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);

// Send 8 more codes, for a total of 9 codes.
for (let i = 0; i < 9; i++) {
Expand All @@ -360,7 +360,7 @@ describe(`#integration - recovery phone - customs checks`, function () {
this.skip('Invalid twilio accountSid or authToken. Check env / config!');
}

await client.recoveryPhoneNumberCreate(phoneNumber);
await client.recoveryPhoneCreate(phoneNumber);
const codeSent = await redisUtil.recoveryPhone.getCode(client.uid, 0);
await client.recoveryPhoneConfirmSetup(codeSent);

Expand Down

0 comments on commit 255780d

Please sign in to comment.