Skip to content

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

Enable WebAuthn in the forced registration controller

Klaus Zanders3d1a49720fff
diff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller.rb
index 7d025617294..bedf5f1dcf6 100644
--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller.rb
+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller.rb
@@ -21,14 +21,30 @@ module ::TwoFactorAuthentication
##
# Register the device and let the user confirm
- def register
+ def register # rubocop:disable Metrics/AbcSize
@device_type = params[:key].to_sym
@device = new_device_type! @device_type
- @device.attributes = new_device_params
+ needs_confirmation = true
+
+ if @device_type == :webauthn
+ if verify_webauthn_credential
+ @device.attributes = new_webauthn_device_params
+ needs_confirmation = false
+ end
+ else
+ @device.attributes = new_device_params
+ end
+
if @device.save
Rails.logger.info "User ##{target_user.id} forced to register a new (unconfirmed) device #{@device_type}."
- redirect_to action: :confirm, device_id: @device.id
+ if needs_confirmation
+ redirect_to action: :confirm, device_id: @device.id
+ else
+ flash[:notice] = t('two_factor_authentication.devices.registration_complete')
+ @device.confirm_registration_and_save
+ redirect_to registration_success_path
+ end
else
Rails.logger.warn { "User ##{target_user.id} forced to register failed for #{@device_type}." }
render 'two_factor_authentication/two_factor_devices/new'
diff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb
index fa9b5fce63c..f5506c3ab0d 100644
--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb
+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb
@@ -30,7 +30,7 @@ module ::TwoFactorAuthentication
##
# Register the device and let the user confirm
- def register
+ def register # rubocop:disable Metrics/AbcSize
@device_type = params[:key].to_sym
@device = new_device_type! @device_type
diff --git a/modules/two_factor_authentication/config/routes.rb b/modules/two_factor_authentication/config/routes.rb
index d28379b9bd8..1182534b0ee 100644
--- a/modules/two_factor_authentication/config/routes.rb
+++ b/modules/two_factor_authentication/config/routes.rb
@@ -14,11 +14,14 @@ Rails.application.routes.draw do
end
scope 'two_factor_authentication' do # Avoids adding the namespace prefix
- scope 'device_registration',
- controller: 'two_factor_authentication/forced_registration/two_factor_devices' do
+ # forced registration
+ scope 'device_registration', controller: 'two_factor_authentication/forced_registration/two_factor_devices' do
get :new, action: :new, as: 'new_forced_2fa_device'
post :register, action: :register, as: 'register_forced_2fa_device'
match '/:device_id/confirm', action: :confirm, via: %i[get post], as: 'confirm_forced_2fa_device'
+
+ # Request a challenge for WebAuthn
+ get :webauthn_challenge
end
end