diff --git a/app/controllers/users/mfa_selection_controller.rb b/app/controllers/users/mfa_selection_controller.rb
index 03cb90b25f7..0fc7013d2e5 100644
--- a/app/controllers/users/mfa_selection_controller.rb
+++ b/app/controllers/users/mfa_selection_controller.rb
@@ -22,12 +22,9 @@ def update
if result.success?
process_valid_form
else
- flash[:error] = t('errors.two_factor_auth_setup.must_select_additional_option')
- redirect_back(fallback_location: second_mfa_setup_path, allow_other_host: false)
+ flash[:error] = result.first_error_message
+ redirect_to second_mfa_setup_path
end
- rescue ActionController::ParameterMissing
- flash[:error] = t('errors.two_factor_auth_setup.must_select_option')
- redirect_back(fallback_location: two_factor_options_path, allow_other_host: false)
end
# @api private
@@ -67,6 +64,8 @@ def process_valid_form
def two_factor_options_form_params
params.require(:two_factor_options_form).permit(:selection, selection: [])
+ rescue ActionController::ParameterMissing
+ ActionController::Parameters.new(selection: [])
end
end
end
diff --git a/app/controllers/users/two_factor_authentication_setup_controller.rb b/app/controllers/users/two_factor_authentication_setup_controller.rb
index db1dd43a107..2f5fd7bab98 100644
--- a/app/controllers/users/two_factor_authentication_setup_controller.rb
+++ b/app/controllers/users/two_factor_authentication_setup_controller.rb
@@ -25,13 +25,10 @@ def create
if result.success?
process_valid_form
else
- flash[:error] = t('errors.two_factor_auth_setup.must_select_option')
+ flash.now[:error] = result.first_error_message
@presenter = two_factor_options_presenter
render :index
end
- rescue ActionController::ParameterMissing
- flash[:error] = t('errors.two_factor_auth_setup.must_select_option')
- redirect_back(fallback_location: authentication_methods_setup_path, allow_other_host: false)
end
# @api private
@@ -71,6 +68,8 @@ def confirm_user_needs_2fa_setup
def two_factor_options_form_params
params.require(:two_factor_options_form).permit(:selection, selection: [])
+ rescue ActionController::ParameterMissing
+ ActionController::Parameters.new(selection: [])
end
end
end
diff --git a/app/forms/two_factor_options_form.rb b/app/forms/two_factor_options_form.rb
index 491c2344ade..6328293f72f 100644
--- a/app/forms/two_factor_options_form.rb
+++ b/app/forms/two_factor_options_form.rb
@@ -1,5 +1,6 @@
class TwoFactorOptionsForm
include ActiveModel::Model
+ include ActionView::Helpers::TranslationHelper
attr_accessor :selection, :user, :phishing_resistant_required, :piv_cac_required
@@ -7,7 +8,7 @@ class TwoFactorOptionsForm
webauthn webauthn_platform
backup_code] }
- validates :selection, length: { minimum: 1 }, if: :has_no_mfa_or_in_required_flow?
+ validate :validate_selection_present
def initialize(user:, phishing_resistant_required:, piv_cac_required:)
self.user = user
@@ -16,7 +17,7 @@ def initialize(user:, phishing_resistant_required:, piv_cac_required:)
end
def submit(params)
- self.selection = Array(params[:selection]).filter(&:present?)
+ self.selection = params[:selection]
success = valid?
update_otp_delivery_preference_for_user if success && user_needs_updating?
@@ -25,6 +26,11 @@ def submit(params)
private
+ def validate_selection_present
+ return if !has_no_mfa_or_in_required_flow? || selection.present?
+ errors.add(:selection, missing_selection_error_message, type: :missing_selection)
+ end
+
def mfa_user
@mfa_user ||= MfaContext.new(user)
end
@@ -66,7 +72,16 @@ def platform_auth_only_option?
end
def has_no_mfa_or_in_required_flow?
- has_no_configured_mfa? || in_phishing_resistant_or_piv_cac_required_flow? ||
+ has_no_configured_mfa? ||
+ in_phishing_resistant_or_piv_cac_required_flow? ||
platform_auth_only_option?
end
+
+ def missing_selection_error_message
+ if has_no_configured_mfa? || in_phishing_resistant_or_piv_cac_required_flow?
+ t('errors.two_factor_auth_setup.must_select_option')
+ elsif platform_auth_only_option?
+ t('errors.two_factor_auth_setup.must_select_additional_option')
+ end
+ end
end
diff --git a/app/views/users/mfa_selection/index.html.erb b/app/views/users/mfa_selection/index.html.erb
index be32cbfd6fa..1a2bff4fdb4 100644
--- a/app/views/users/mfa_selection/index.html.erb
+++ b/app/views/users/mfa_selection/index.html.erb
@@ -11,7 +11,6 @@