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

Fix/authenticator/phone validator #5518

Closed
wants to merge 26 commits into from

Conversation

ekjotmultani
Copy link
Member

@ekjotmultani ekjotmultani commented Oct 1, 2024

Issue #, if available: #4079

Description of changes:

  • Drop the country code prefix before performing validation

This is done in the exact same way as #4106 from November last year. This fix was correct for the main issue in the original issue however it didn't address being able to re-input a dial code and still have it pass through validation

  • Truncate a re-inputted dial code if it exceeds the maximum length of a phone number from that given country

Using a map of dial codes to their countries' maximum length of phone number (generated from this open source repo), we will check if the inputted phone number begins with the selected dial code, if it does, then check if it surpasses the length of number in that country (since it could be the case that the number naturally begins with the dial code), if this is also true then ignore the repeat dial code and take the leftover number as the phone number

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ekjotmultani ekjotmultani marked this pull request as ready for review October 1, 2024 20:23
@ekjotmultani ekjotmultani requested a review from a team as a code owner October 1, 2024 20:23
Comment on lines 59 to 64
if (phoneNumber.startsWith(prefix.substring(1))) {
if (countryPhoneNumberLengths.containsKey(prefix) &&
phoneNumber.length > countryPhoneNumberLengths[prefix]!) {
phoneNumber = phoneNumber.substring(prefix.length - 1);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

refactor: Can this be rewritten to avoid nested if statements in favor of guard clauses? Something like improves readability:

if (!phoneNumber.startsWith(prefix.substring(1))) {
  return phoneNumber;
}

final prefixLength = countryPhoneNumberLengths[prefix];
if (prefixLength == null || phoneNumber.length <= prefixLength) {
  return phoneNumber;
}

return phoneNumber.substring(prefix.length - 1);

Comment on lines +1 to +5
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/// A list of [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) dial codes mapped to the maximum [possible length](https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers) of the phone number from that country as an integer
///
///
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: I assume this is coming from libphonenumber... Can you ensure this is has the proper license & attribution for us to use?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this comes by scraping their metadata, it looks like they have an Apache 2.0 License. Is that ok for us to use?

Comment on lines 178 to 251
testWidgets(
'displays message when submitted with empty phone number if the field is required',
(tester) async {
await tester.pumpWidget(
MockAuthenticatorApp(
initialStep: AuthenticatorStep.signUp,
signUpForm: SignUpForm.custom(
fields: [
SignUpFormField.username(),
SignUpFormField.phoneNumber(required: true),
SignUpFormField.password(),
],
),
),
);
await tester.pumpAndSettle();

final signUpPage = SignUpPage(tester: tester);

await signUpPage.submitSignUp();

await tester.pumpAndSettle();

Finder findPhoneFieldError() => find.descendant(
of: signUpPage.phoneField,
matching: find.text('Phone Number field must not be blank.'),
);

expect(findPhoneFieldError(), findsOneWidget);

await signUpPage.enterPhoneNumber('1235556789');

await signUpPage.submitSignUp();

expect(findPhoneFieldError(), findsNothing);
},
);
testWidgets(
'displays message when submitted with empty phone number if the field is required',
(tester) async {
await tester.pumpWidget(
MockAuthenticatorApp(
initialStep: AuthenticatorStep.signUp,
signUpForm: SignUpForm.custom(
fields: [
SignUpFormField.username(),
SignUpFormField.phoneNumber(required: true),
SignUpFormField.password(),
],
),
),
);
await tester.pumpAndSettle();

final signUpPage = SignUpPage(tester: tester);

await signUpPage.submitSignUp();

await tester.pumpAndSettle();

Finder findPhoneFieldError() => find.descendant(
of: signUpPage.phoneField,
matching: find.text('Phone Number field must not be blank.'),
);

expect(findPhoneFieldError(), findsOneWidget);

await signUpPage.enterPhoneNumber('1235556789');

await signUpPage.submitSignUp();

expect(findPhoneFieldError(), findsNothing);
},
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: These tests appear to be identical, was this an accident? It would be nice to see the use case you're resolved tested here instead.

tyllark and others added 20 commits October 18, 2024 10:45
* chore(common,notifications): update os_detect to version 2.0.2
…5567)

* fix(api): Reconnect WebSocket when resuming app from a paused state
chore(version): Bump version

### Features
- feat(api): move App Sync subscription headers to protocol ([#5301](#5301))
- feat(authenticator): export unmet password requirements ([#5437](#5437))

### Fixes
- fix(api): Reconnect WebSocket when resuming app from a paused state ([#5567](#5567))

Updated-Components: amplify_lints, Amplify Flutter, Amplify Dart, Amplify UI, DB Common, Secure Storage, AWS Common, Smithy, Worker Bee
### Features
- feat(auth, authenticator): Add support for Email OTP MFA ([#5449](#5449)) (#5472)

### Fixes
- fix(common): db_common windows sqlite3 collision ([#5481](#5481))

Updated-Components: amplify_lints, Amplify Flutter, Amplify Dart, Amplify UI, DB Common, Secure Storage, AWS Common, Smithy, Worker Bee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants