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

Ensure that only SMS devices can be set up via the admin user list

Klaus Zanders8af0b2a5a0f2
diff --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.rb
index 01065a27e72..7fc5effda9d 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
@@ -8,6 +8,8 @@ module ::TwoFactorAuthentication
before_action :find_device, only: %i[confirm make_default destroy]
+ helper_method :optional_webauthn_challenge_url
+
layout 'no_menu'
def new
@@ -261,5 +263,11 @@ module ::TwoFactorAuthentication
target_user.update(webauthn_id: WebAuthn.generate_user_id)
end
+
+ def optional_webauthn_challenge_url
+ if @device_type == :webauthn
+ helpers.url_for(action: :webauthn_challenge, format: :json)
+ end
+ end
end
end
diff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb
index 244867bf878..f41a771062f 100644
--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb
+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb
@@ -7,6 +7,9 @@ module ::TwoFactorAuthentication
# Ensure where not the user under edit
before_action :require_not_self
+ # Ensure that only mobile devices are added for other users
+ before_action :ensure_only_sms_type, only: :new
+
# Password confirmation helpers and actions
include PasswordConfirmation
before_action :check_password_confirmation,
@@ -77,6 +80,12 @@ module ::TwoFactorAuthentication
private
+ def ensure_only_sms_type
+ return if params[:type] == 'sms'
+
+ render_400(message: I18n.t('two_factor_authentication.admin.only_sms_allowed'))
+ end
+
def new_device_params
# Overrides the base controller to active the device
# without prior confirmation.
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb
index 2a4ac4a4796..af4dbe010d0 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb
@@ -7,7 +7,7 @@
html: { class: nil, autocomplete: 'off', data: {
controller: "two-factor-authentication",
application_target: "dynamic",
- challenge_url: url_for(action: :webauthn_challenge, format: :json),
+ challenge_url: optional_webauthn_challenge_url,
device_type: @device_type,
action: "submit->two-factor-authentication#onCreateDevice"
} },
diff --git a/modules/two_factor_authentication/config/locales/en.yml b/modules/two_factor_authentication/config/locales/en.yml
index 57c94b5db0d..f1874d48fc7 100644
--- a/modules/two_factor_authentication/config/locales/en.yml
+++ b/modules/two_factor_authentication/config/locales/en.yml
@@ -80,6 +80,7 @@ en:
button_register_mobile_phone_for_user: "Register mobile phone"
text_2fa_enabled: "Upon every login, this user will be requested to enter a OTP token from their default 2FA device."
text_2fa_disabled: "The user did not set up a 2FA device through their 'My account page'"
+ only_sms_allowed: "Only SMS delivery can be set up for other users."
upsale:
title: "Two-factor authentication"
description: "Strenghten the security of your OpenProject instance by offering (or enforcing) two-factor authentification to all project members."