OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Extract relying party construction into a concern
Klaus Zanders3cb6c4d65e3a
diff --git a/modules/two_factor_authentication/app/controllers/concerns/two_factor_authentication/webauthn_relying_party.rb b/modules/two_factor_authentication/app/controllers/concerns/two_factor_authentication/webauthn_relying_party.rbnew file mode 100644index 00000000000..8fcde41b375--- /dev/null+++ b/modules/two_factor_authentication/app/controllers/concerns/two_factor_authentication/webauthn_relying_party.rb@@ -0,0 +1,19 @@+module ::TwoFactorAuthentication+ module WebauthnRelyingParty+ extend ActiveSupport::Concern++ protected++ def webauthn_relying_party+ @webauthn_relying_party ||= begin+ origin = "#{Setting.protocol}://#{Setting.host_name}"++ WebAuthn::RelyingParty.new(+ origin:,+ id: URI(origin).host,+ name: Setting.app_title+ )+ end+ end+ end+enddiff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/authentication_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/authentication_controller.rbindex d5d82c6266c..bb770fbe1ea 100644--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/authentication_controller.rb+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/authentication_controller.rb@@ -4,8 +4,10 @@ module ::TwoFactorAuthenticationinclude ::TwoFactorAuthentication::RememberToken# Backup tokens functionalityinclude ::TwoFactorAuthentication::BackupCodes+ # Webauthn relying party based on domain+ include ::TwoFactorAuthentication::WebauthnRelyingParty# Include global layout helper- layout 'no_menu'+ layout "no_menu"# User is not yet logged in, so skip login required checkskip_before_action :check_if_login_required@@ -29,7 +31,7 @@ module ::TwoFactorAuthenticationsession[:authenticated_user_force_2fa] = service.needs_registration?if service.needs_registration?- flash[:info] = I18n.t('two_factor_authentication.forced_registration.required_to_add_device')+ flash[:info] = I18n.t("two_factor_authentication.forced_registration.required_to_add_device")redirect_to new_forced_2fa_device_pathelsif !service.requires_token?complete_stage_redirect@@ -142,9 +144,9 @@ module ::TwoFactorAuthentication@active_devices = @user.otp_devices.get_activeif params["back_url"]- render action: 'request_otp', back_url: params["back_url"]+ render action: "request_otp", back_url: params["back_url"]else- render action: 'request_otp'+ render action: "request_otp"endend@@ -206,7 +208,7 @@ module ::TwoFactorAuthentication# In case of mis-configuration, block all loginsdef ensure_valid_configurationif manager.invalid_configuration?- render_500 message: I18n.t('two_factor_authentication.error_is_enforced_not_active')+ render_500 message: I18n.t("two_factor_authentication.error_is_enforced_not_active")falseendend@@ -221,12 +223,5 @@ module ::TwoFactorAuthenticationdef failure_stage_redirectredirect_to authentication_stage_failure_path :two_factor_authenticationend-- def webauthn_relying_party- @webauthn_relying_party ||= WebAuthn::RelyingParty.new(- origin: "#{Setting.protocol}://#{Setting.host_name}",- name: Setting.app_title- )- endendenddiff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rbindex 0f9bfb8bf99..bf5ca78a4af 100644--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb@@ -1,5 +1,7 @@module ::TwoFactorAuthenticationclass BaseController < ApplicationController+ include ::TwoFactorAuthentication::WebauthnRelyingParty+# Ensure 2FA authentication is enabledbefore_action :ensure_enabled_2fa@@ -10,17 +12,17 @@ module ::TwoFactorAuthenticationhelper_method :optional_webauthn_challenge_url- layout 'no_menu'+ layout "no_menu"def newif params[:type]@device_type = params[:type].to_sym@device = new_device_type! @device_type- render 'two_factor_authentication/two_factor_devices/new'+ render "two_factor_authentication/two_factor_devices/new"else@available_devices = available_devices- render 'two_factor_authentication/two_factor_devices/new_type'+ render "two_factor_authentication/two_factor_devices/new_type"endend@@ -32,7 +34,7 @@ module ::TwoFactorAuthenticationif @device.make_default!flash[:notice] = t(:notice_successful_update)else- flash[:error] = t('two_factor_authentication.devices.make_default_failed')+ flash[:error] = t("two_factor_authentication.devices.make_default_failed")endredirect_to index_path@@ -42,14 +44,14 @@ module ::TwoFactorAuthentication# Destroy the given device if its not the defaultdef destroyif @device.default && strategy_manager.enforced?- render_400 message: t('two_factor_authentication.devices.is_default_cannot_delete')+ render_400 message: t("two_factor_authentication.devices.is_default_cannot_delete")returnendif @device.destroyflash[:notice] = t(:notice_successful_delete)else- flash[:error] = t('two_factor_authentication.devices.failed_to_delete')+ flash[:error] = t("two_factor_authentication.devices.failed_to_delete")Rails.logger.error "Failed to delete #{@device.id} of user#{target_user.id}. Errors: #{@device.errors.full_messages.join(' ')}"end@@ -89,8 +91,8 @@ module ::TwoFactorAuthenticationrequest_token_for_device(@device,confirm_path: url_for(action: :confirm, device_id: @device.id),- title: I18n.t('two_factor_authentication.devices.confirm_device'),- message: I18n.t('two_factor_authentication.devices.text_confirm_to_complete_html', identifier: @device.identifier)+ title: I18n.t("two_factor_authentication.devices.confirm_device"),+ message: I18n.t("two_factor_authentication.devices.text_confirm_to_complete_html", identifier: @device.identifier))end@@ -112,15 +114,15 @@ module ::TwoFactorAuthentication# rubocop:disable Metrics/AbcSizedef confirm_and_save(result)if result.success? && @device.confirm_registration_and_save- flash[:notice] = t('two_factor_authentication.devices.registration_complete')+ flash[:notice] = t("two_factor_authentication.devices.registration_complete")trueelsif !result.success?flash[:notice] = nil- flash[:error] = t('two_factor_authentication.devices.registration_failed_token_invalid')+ flash[:error] = t("two_factor_authentication.devices.registration_failed_token_invalid")falseelseflash[:notice] = nil- flash[:error] = t('two_factor_authentication.devices.registration_failed_update')+ flash[:error] = t("two_factor_authentication.devices.registration_failed_update")falseendend@@ -133,10 +135,10 @@ module ::TwoFactorAuthenticationflash[:notice] = transmit.result if transmit.result.present?# Request confirmation from user as in the regular login flow- render 'two_factor_authentication/two_factor_devices/confirm', layout: 'base', locals:+ render "two_factor_authentication/two_factor_devices/confirm", layout: "base", locals:elseerror = transmit.errors.full_messages.join(". ")- default_message = t('two_factor_authentication.devices.confirm_send_failed')+ default_message = t("two_factor_authentication.devices.confirm_send_failed")flash[:error] = "#{default_message} #{error}"redirect_to registration_failure_path@@ -195,18 +197,6 @@ module ::TwoFactorAuthenticationfalseend- def webauthn_relying_party- @webauthn_relying_party ||= begin- origin = "#{Setting.protocol}://#{Setting.host_name}"-- WebAuthn::RelyingParty.new(- origin:,- id: URI(origin).host,- name: Setting.app_title- )- end- end-def logout_other_sessionsif current_user == target_userRails.logger.info { "First 2FA device registered for #{target_user}, terminating other logged in sessions." }@@ -257,7 +247,7 @@ module ::TwoFactorAuthenticationenddef default_breadcrumb- t('two_factor_authentication.label_devices')+ t("two_factor_authentication.label_devices")enddef available_devices