-
Notifications
You must be signed in to change notification settings - Fork 166
Disable phone OTP option in select US area codes #1559
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| 'app/radio-btn', | ||
| 'app/print-personal-key', | ||
| 'app/utils/ms-formatter', | ||
| 'app/phone-internationalization', | ||
| ], | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { PhoneFormatter } from 'field-kit'; | ||
|
|
||
| const I18n = window.LoginGov.I18n; | ||
| const phoneFormatter = new PhoneFormatter(); | ||
|
|
||
| const getPhoneUnsupportedAreaCodeCountry = (areaCode) => { | ||
| const form = document.querySelector('#new_two_factor_setup_form'); | ||
| const phoneUnsupportedAreaCodes = JSON.parse(form.dataset.unsupportedAreaCodes); | ||
| return phoneUnsupportedAreaCodes[areaCode]; | ||
| }; | ||
|
|
||
| const areaCodeFromUSPhone = (phone) => { | ||
| const digits = phoneFormatter.digitsWithoutCountryCode(phone); | ||
| if (digits.length >= 10) { | ||
| return digits.slice(0, 3); | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| const unsupportedPhoneOTPDeliveryWarningMessage = (phone) => { | ||
| const areaCode = areaCodeFromUSPhone(phone); | ||
| const country = getPhoneUnsupportedAreaCodeCountry(areaCode); | ||
| if (country) { | ||
| const messageTemplate = I18n.t('devise.two_factor_authentication.otp_delivery_preference.phone_unsupported'); | ||
| return messageTemplate.replace('%{location}', country); | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| const updateOTPDeliveryMethods = () => { | ||
| const phoneInput = document.querySelector('#two_factor_setup_form_phone'); | ||
| const phoneRadio = document.querySelector('#two_factor_setup_form_otp_delivery_preference_voice'); | ||
| const smsRadio = document.querySelector('#two_factor_setup_form_otp_delivery_preference_sms'); | ||
| const phoneLabel = phoneRadio.parentNode.parentNode; | ||
| const deliveryMethodHint = document.querySelector('#otp_delivery_preference_instruction'); | ||
| const optPhoneLabelInfo = document.querySelector('#otp_phone_label_info'); | ||
|
|
||
| const phone = phoneInput.value; | ||
|
|
||
| const warningMessage = unsupportedPhoneOTPDeliveryWarningMessage(phone); | ||
| if (warningMessage) { | ||
| phoneRadio.disabled = true; | ||
| phoneLabel.classList.add('btn-disabled'); | ||
| smsRadio.click(); | ||
| deliveryMethodHint.innerText = warningMessage; | ||
| optPhoneLabelInfo.innerText = I18n.t('devise.two_factor_authentication.otp_phone_label_info_modile_only'); | ||
| } else { | ||
| phoneRadio.disabled = false; | ||
| phoneLabel.classList.remove('btn-disabled'); | ||
| deliveryMethodHint.innerText = I18n.t('devise.two_factor_authentication.otp_delivery_preference.instruction'); | ||
| optPhoneLabelInfo.innerText = I18n.t('devise.two_factor_authentication.otp_phone_label_info'); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', () => { | ||
| const input = document.querySelector('#two_factor_setup_form_phone'); | ||
| if (input) { | ||
| input.addEventListener('keyup', updateOTPDeliveryMethods); | ||
| updateOTPDeliveryMethods(); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,3 +72,9 @@ | |
| outline: none; | ||
| } | ||
| } | ||
|
|
||
| .btn-disabled { | ||
| background-color: $gray-light; | ||
| border-color: $gray; | ||
| color: $gray; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class PhoneNumberCapabilities | ||
| VOICE_UNSUPPORTED_US_AREA_CODES = { | ||
| '648' => 'American Samoa', | ||
| '671' => 'Guam', | ||
| '670' => 'Northern Mariana Islands', | ||
| '340' => 'United States Virgin Islands', | ||
| }.freeze | ||
|
|
||
| attr_reader :phone | ||
|
|
||
| def initialize(phone) | ||
| @phone = phone | ||
| end | ||
|
|
||
| def sms_only? | ||
| VOICE_UNSUPPORTED_US_AREA_CODES[area_code].present? | ||
| end | ||
|
|
||
| def unsupported_location | ||
| VOICE_UNSUPPORTED_US_AREA_CODES[area_code] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def area_code | ||
| @area_code ||= Phony.split(Phony.normalize(phone, cc: '1')).second | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I think using Phonelib.parse(phone).area_code
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fancy 👔. So many phone gems.....
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, so this started breaking the spec for the IdV flow. The problem is that at this point the phone number has been formatted for the vendor and no longer includes the country code. It appears to still work for US numbers, but gets confused for numbers with a US country code. Using Phony to normalize it to add the Related: We may want to make sure we only accept US phone numbers for IdV in #1544 |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| module OtpDeliveryPreferenceValidator | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| validate :otp_delivery_preference_supported | ||
| end | ||
|
|
||
| def otp_delivery_preference_supported | ||
| capabilities = PhoneNumberCapabilities.new(phone) | ||
| return unless otp_delivery_preference == 'voice' && capabilities.sms_only? | ||
|
|
||
| errors.add( | ||
| :phone, | ||
| I18n.t( | ||
| 'devise.two_factor_authentication.otp_delivery_preference.phone_unsupported', | ||
| location: capabilities.unsupported_location | ||
| ) | ||
| ) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Climate says this line is not tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, this presenter has basically no tests. I can stick some in there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@monfresh: Added tests for the parts I changed.