Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/concerns/phone_confirmation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def prompt_to_confirm_phone(id:, phone:, selected_delivery_method: nil,
private

def otp_delivery_method(_id, phone, selected_delivery_method)
return :sms if PhoneNumberCapabilities.new(phone).sms_only?
capabilities = PhoneNumberCapabilities.new(phone)
return :sms if capabilities.sms_only?
return selected_delivery_method if selected_delivery_method.present?
current_user.otp_delivery_preference
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def voice_otp_delivery_unsupported?
else
user_session[:unconfirmed_phone]
end
PhoneNumberCapabilities.new(phone_number).sms_only?
!PhoneNumberCapabilities.new(phone_number).supports_voice?
end

def decorated_user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def confirm_voice_capability

capabilities = PhoneNumberCapabilities.new(phone)

return unless capabilities.sms_only?
return if capabilities.supports_voice?

flash[:error] = t(
'two_factor_authentication.otp_delivery_preference.phone_unsupported',
Expand Down
11 changes: 3 additions & 8 deletions app/forms/otp_delivery_selection_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def submit(params)

@success = valid?

change_otp_delivery_preference_to_sms if unsupported_phone?
if !otp_delivery_preference_supported? && phone_number_capabilities.supports_sms?
change_otp_delivery_preference_to_sms
end

FormResponse.new(success: success, errors: errors.messages, extra: extra_analytics_attributes)
end
Expand All @@ -35,13 +37,6 @@ def change_otp_delivery_preference_to_sms
UpdateUser.new(user: user, attributes: user_attributes).call
end

def unsupported_phone?
error_messages = errors.messages
return false unless error_messages.key?(:phone)

error_messages[:phone].first != I18n.t('errors.messages.missing_field')
end

def extra_analytics_attributes
{
otp_delivery_preference: otp_delivery_preference,
Expand Down
12 changes: 10 additions & 2 deletions app/models/phone_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ def formatted_phone
end

def selection_presenters
options = [TwoFactorAuthentication::SmsSelectionPresenter.new(self)]
unless PhoneNumberCapabilities.new(phone).sms_only?
options = []

capabilities = PhoneNumberCapabilities.new(phone)

if capabilities.supports_sms?
options << TwoFactorAuthentication::SmsSelectionPresenter.new(self)
end

if capabilities.supports_voice?
options << TwoFactorAuthentication::VoiceSelectionPresenter.new(self)
end

options
end

Expand Down
17 changes: 12 additions & 5 deletions app/services/phone_number_capabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ def initialize(phone)
end

def sms_only?
return true if country_code_data.nil?
country_code_data['sms_only']
supports_sms? && !supports_voice?
end

def supports_sms?
return false if country_code_data.nil?
country_code_data['supports_sms']
end

def supports_voice?
return false if country_code_data.nil?
country_code_data['supports_voice']
end

def unsupported_location
Expand All @@ -21,9 +30,7 @@ def unsupported_location
private

def country_code_data
@country_code_data ||= INTERNATIONAL_CODES.select do |key, _|
key == two_letter_country_code
end.values.first
INTERNATIONAL_CODES[two_letter_country_code]
end

def two_letter_country_code
Expand Down
13 changes: 11 additions & 2 deletions app/validators/otp_delivery_preference_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ module OtpDeliveryPreferenceValidator
end

def otp_delivery_preference_supported?
return true unless otp_delivery_preference == 'voice'
!phone_number_capabilities.sms_only?
case otp_delivery_preference
when 'voice'
phone_number_capabilities.supports_voice?
when 'sms'
phone_number_capabilities.supports_sms?
end
end

def invalid_otp_delivery_preference?
!%w[voice sms].include?(otp_delivery_preference)
end

def otp_delivery_preference_supported
return if invalid_otp_delivery_preference?
return if otp_delivery_preference_supported?

errors.add(
Expand Down
Loading