Skip to content

Commit 597476a

Browse files
authored
Merge pull request #18977 from mozilla/use-check-authenticated
task(auth): Use customs.checkAuthenticated where possible
2 parents 31a1577 + 5dd2a66 commit 597476a

19 files changed

Lines changed: 192 additions & 64 deletions

File tree

packages/fxa-auth-server/config/rate-limit-rules.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
accountCreate : email : 100 : 15 minutes : 15 minutes
77
accountLogin : ip : 100 : 15 minutes : 15 minutes
88
accountLogin : email : 100 : 15 minutes : 15 minutes
9-
accountDestroy : ip : 100 : 15 minutes : 15 minutes
10-
accountDestroy : email : 100 : 15 minutes : 15 minutes
9+
accountDestroy : uid : 100 : 15 minutes : 15 minutes
1110
passwordChange : ip : 100 : 15 minutes : 15 minutes
1211
passwordChange : email : 100 : 15 minutes : 15 minutes
1312
passwordForgotSendCode : ip : 100 : 15 minutes : 15 minutes
@@ -30,18 +29,15 @@
3029
# Email Send - These are limits on rate at which emails can be sent out. We limit both IP and email address
3130
# since many of these operations do not require authentication. Some operations, however, require sessions
3231
# and for these we should switch over to using UID. See FXA-11777 for more details.
33-
createEmail : email : 5 : 15 minutes : 15 minutes
34-
createEmail : ip : 5 : 10 minutes : 30 minutes
35-
recoveryEmailResendCode : email : 5 : 15 minutes : 15 minutes
36-
recoveryEmailResendCode : ip : 5 : 10 minutes : 30 minutes
32+
createEmail : uid : 5 : 15 minutes : 15 minutes
33+
recoveryEmailResendCode : uid : 5 : 15 minutes : 15 minutes
3734
recoveryEmailSecondaryResendCode : email : 5 : 15 minutes : 15 minutes
3835
recoveryEmailSecondaryResendCode : ip : 5 : 10 minutes : 30 minutes
3936
passwordForgotSendCode : email : 5 : 15 minutes : 15 minutes
4037
passwordForgotSendCode : ip : 5 : 10 minutes : 30 minutes
4138
passwordForgotResendCode : email : 5 : 15 minutes : 15 minutes
4239
passwordForgotResendCode : ip : 5 : 10 minutes : 30 minutes
43-
sendVerifyCode : email : 5 : 15 minutes : 15 minutes
44-
sendVerifyCode : ip : 5 : 10 minutes : 30 minutes
40+
sendVerifyCode : uid : 5 : 15 minutes : 15 minutes
4541
sendUnblockCode : email : 5 : 15 minutes : 15 minutes
4642
sendUnblockCode : ip : 5 : 10 minutes : 30 minutes
4743
unblockEmail : email : 5 : 15 minutes : 15 minutes

packages/fxa-auth-server/lib/customs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class CustomsClient {
120120
async checkAuthenticated(request, uid, email, action) {
121121
const checked = await this.checkV2(request, 'checkAuthenticated', action, {
122122
ip: request?.app?.clientAddress,
123+
email,
123124
uid,
124125
});
125126
if (checked) {

packages/fxa-auth-server/lib/routes/account.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ export class AccountHandler {
822822
const account = await this.db.account(uid as string);
823823
const email = account.primaryEmail?.email;
824824

825-
await this.customs.check(request, email, 'setPassword');
825+
await this.customs.checkAuthenticated(request, uid, email, 'setPassword');
826826

827827
const response: Record<string, any> = {};
828828
response.uid = uid;
@@ -1823,7 +1823,12 @@ export class AccountHandler {
18231823
authenticatorAssuranceLevel?: number;
18241824
}
18251825

1826-
await this.customs.check(request, emailAddress, 'accountDestroy');
1826+
await this.customs.checkAuthenticated(
1827+
request,
1828+
sessionToken.uid,
1829+
sessionToken.email,
1830+
'accountDestroy'
1831+
);
18271832

18281833
let accountRecord: Account;
18291834
try {

packages/fxa-auth-server/lib/routes/emails.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ module.exports = (
273273
return {};
274274
}
275275

276-
await customs.check(
276+
await customs.checkAuthenticated(
277277
request,
278+
sessionToken.uid,
278279
sessionToken.email,
279280
'recoveryEmailResendCode'
280281
);
@@ -577,7 +578,12 @@ module.exports = (
577578
uid: uid,
578579
};
579580

580-
await customs.check(request, primaryEmail, 'createEmail');
581+
await customs.checkAuthenticated(
582+
request,
583+
uid,
584+
primaryEmail,
585+
'createEmail'
586+
);
581587

582588
const account = await db.account(uid);
583589
const secondaryEmails = account.emails.filter(
@@ -724,7 +730,12 @@ module.exports = (
724730
const primaryEmail = sessionToken.email;
725731
const email = request.payload.email;
726732

727-
await customs.check(request, primaryEmail, 'deleteEmail');
733+
await customs.checkAuthenticated(
734+
request,
735+
uid,
736+
primaryEmail,
737+
'deleteEmail'
738+
);
728739
const account = await db.account(uid);
729740

730741
if (sessionToken.tokenVerificationId) {
@@ -789,7 +800,12 @@ module.exports = (
789800

790801
log.begin('Account.RecoveryEmailSetPrimary', request);
791802

792-
await customs.check(request, currentEmail, 'setPrimaryEmail');
803+
await customs.checkAuthenticated(
804+
request,
805+
uid,
806+
currentEmail,
807+
'setPrimaryEmail'
808+
);
793809

794810
if (sessionToken.tokenVerificationId) {
795811
throw error.unverifiedSession();
@@ -902,8 +918,9 @@ module.exports = (
902918
const geoData = request.app.geo;
903919
const { email } = request.payload;
904920

905-
await customs.check(
921+
await customs.checkAuthenticated(
906922
request,
923+
sessionToken.uid,
907924
sessionToken.email,
908925
'recoveryEmailSecondaryResendCode'
909926
);
@@ -988,8 +1005,9 @@ module.exports = (
9881005
const sessionToken = request.auth.credentials;
9891006
const { email, code } = request.payload;
9901007

991-
await customs.check(
1008+
await customs.checkAuthenticated(
9921009
request,
1010+
sessionToken.uid,
9931011
sessionToken.email,
9941012
'recoveryEmailSecondaryVerifyCode'
9951013
);

packages/fxa-auth-server/lib/routes/recovery-codes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ module.exports = (log, db, config, customs, mailer, glean) => {
207207
uid,
208208
} = request.auth.credentials;
209209

210-
await customs.check(request, email, 'verifyRecoveryCode');
210+
await customs.checkAuthenticated(
211+
request,
212+
uid,
213+
email,
214+
'verifyRecoveryCode'
215+
);
211216

212217
const { code } = request.payload;
213218
const { remaining } = await db.consumeRecoveryCode(uid, code);

packages/fxa-auth-server/lib/routes/recovery-phone.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ class RecoveryPhoneHandler {
130130
throw AppError.invalidToken();
131131
}
132132

133-
await this.customs.check(request, email, 'recoveryPhoneSendSigninCode');
133+
await this.customs.checkAuthenticated(
134+
request,
135+
uid,
136+
email,
137+
'recoveryPhoneSendSigninCode'
138+
);
134139

135140
const getFormattedMessage = async (code: string) => {
136141
const localizedMessage = await this.getLocalizedMessage(

packages/fxa-auth-server/lib/routes/session.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,9 @@ module.exports = function (
454454
const account = await db.account(sessionToken.uid);
455455
const secret = account.primaryEmail.emailCode;
456456

457-
await customs.check(
457+
await customs.checkAuthenticated(
458458
request,
459+
account.uid,
459460
account.primaryEmail.normalizedEmail,
460461
'sendVerifyCode'
461462
);

packages/fxa-auth-server/lib/routes/subscriptions/mozilla.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ export class MozillaSubscriptionHandler {
3838
);
3939

4040
const { uid, email } = await handleAuth(this.db, request.auth, true);
41-
await this.customs.check(
41+
await this.customs.checkAuthenticated(
4242
request,
43+
uid,
4344
email,
4445
'mozillaSubscriptionsCustomerBillingAndSubscriptions'
4546
);
@@ -84,8 +85,9 @@ export class MozillaSubscriptionHandler {
8485
this.log.begin('mozillaSubscriptions.validatePlanEligibility', request);
8586

8687
const { uid, email } = await handleAuth(this.db, request.auth, true);
87-
await this.customs.check(
88+
await this.customs.checkAuthenticated(
8889
request,
90+
uid,
8991
email,
9092
'mozillaSubscriptionsValidatePlanEligibility'
9193
);

packages/fxa-auth-server/lib/routes/subscriptions/paypal.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ export class PayPalHandler extends StripeWebhookHandler {
5959
*/
6060
async getCheckoutToken(request: AuthRequest) {
6161
this.log.begin('subscriptions.getCheckoutToken', request);
62-
const { email } = await handleAuth(this.db, request.auth, true);
63-
await this.customs.check(request, email, 'getCheckoutToken');
62+
const { uid, email } = await handleAuth(this.db, request.auth, true);
63+
await this.customs.checkAuthenticated(
64+
request,
65+
uid,
66+
email,
67+
'getCheckoutToken'
68+
);
6469

6570
const { currencyCode } = request.payload as Record<string, string>;
6671
const token = await this.paypalHelper.getCheckoutToken({ currencyCode });
@@ -87,7 +92,12 @@ export class PayPalHandler extends StripeWebhookHandler {
8792
);
8893

8994
try {
90-
await this.customs.check(request, email, 'createSubscriptionWithPaypal');
95+
await this.customs.checkAuthenticated(
96+
request,
97+
uid,
98+
email,
99+
'createSubscriptionWithPaypal'
100+
);
91101

92102
const taxAddress = buildTaxAddress(
93103
this.log,
@@ -370,7 +380,12 @@ export class PayPalHandler extends StripeWebhookHandler {
370380
async updatePaypalBillingAgreement(request: AuthRequest) {
371381
this.log.begin('subscriptions.updatePaypalBillingAgreement', request);
372382
const { uid, email } = await handleAuth(this.db, request.auth, true);
373-
await this.customs.check(request, email, 'updatePaypalBillingAgreement');
383+
await this.customs.checkAuthenticated(
384+
request,
385+
uid,
386+
email,
387+
'updatePaypalBillingAgreement'
388+
);
374389

375390
const customer = await this.stripeHelper.fetchCustomer(uid, [
376391
'subscriptions',

0 commit comments

Comments
 (0)