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

Build RelyingParty on the fly to support our hosted environment

Klaus Zanders7d92d20a921a
diff --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.rb
index 7a33d80c34b..d5d82c6266c 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
@@ -58,7 +58,7 @@ module ::TwoFactorAuthentication
def webauthn_challenge
device = otp_service(@authenticated_user).device
- webauthn_options = device.options_for_get
+ webauthn_options = device.options_for_get(webauthn_relying_party)
session[:webauthn_challenge] = webauthn_options.challenge
render json: webauthn_options
@@ -154,7 +154,11 @@ module ::TwoFactorAuthentication
service = otp_service_for_verification(user)
result = if service.device.class.device_type == :webauthn
- service.verify(webauthn_credential, webauthn_challenge: session[:webauthn_challenge])
+ service.verify(
+ JSON.parse(webauthn_credential),
+ webauthn_challenge: session[:webauthn_challenge],
+ webauthn_relying_party:
+ )
else
service.verify(otp_token)
end
@@ -217,5 +221,12 @@ module ::TwoFactorAuthentication
def failure_stage_redirect
redirect_to authentication_stage_failure_path :two_factor_authentication
end
+
+ def webauthn_relying_party
+ @webauthn_relying_party ||= WebAuthn::RelyingParty.new(
+ origin: "#{Setting.protocol}://#{Setting.host_name}",
+ name: Setting.app_title
+ )
+ end
end
end
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 7fc5effda9d..20f973229db 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
@@ -75,7 +75,7 @@ module ::TwoFactorAuthentication
ensure_user_has_webauthn_id!
- webauthn_options = device.options_for_create
+ webauthn_options = device.options_for_create(webauthn_relying_party)
session[:webauthn_challenge] = webauthn_options.challenge
render json: webauthn_options
@@ -177,18 +177,31 @@ module ::TwoFactorAuthentication
end
def webauthn_credential
- @webauthn_credential ||= WebAuthn::Credential.from_create(JSON.parse(params[:device][:webauthn_credential]))
+ @webauthn_credential ||= webauthn_relying_party.verify_registration(
+ JSON.parse(params[:device][:webauthn_credential]),
+ session[:webauthn_challenge]
+ )
end
def verify_webauthn_credential
- webauthn_credential.verify(session[:webauthn_challenge])
- session.delete(:webauthn_challenge)
- true
+ if webauthn_credential
+ session.delete(:webauthn_challenge)
+ true
+ else
+ false
+ end
rescue WebAuthn::Error => e
Rails.logger.error "Failed to verify WebAuthn credential for registration. #{e}"
false
end
+ def webauthn_relying_party
+ @webauthn_relying_party ||= WebAuthn::RelyingParty.new(
+ origin: "#{Setting.protocol}://#{Setting.host_name}",
+ name: Setting.app_title
+ )
+ end
+
def logout_other_sessions
if current_user == target_user
Rails.logger.info { "First 2FA device registered for #{target_user}, terminating other logged in sessions." }
diff --git a/modules/two_factor_authentication/app/models/two_factor_authentication/device/webauthn.rb b/modules/two_factor_authentication/app/models/two_factor_authentication/device/webauthn.rb
index 1b011621a48..c1b7dbe88f6 100644
--- a/modules/two_factor_authentication/app/models/two_factor_authentication/device/webauthn.rb
+++ b/modules/two_factor_authentication/app/models/two_factor_authentication/device/webauthn.rb
@@ -18,15 +18,15 @@ module TwoFactorAuthentication
end
validates_inclusion_of :channel, in: supported_channels
- def options_for_create
- @options_for_create ||= WebAuthn::Credential.options_for_create(
+ def options_for_create(relying_party)
+ @options_for_create ||= relying_party.options_for_registration(
user: { id: user.webauthn_id, name: user.name },
exclude: TwoFactorAuthentication::Device::Webauthn.where(user:).pluck(:webauthn_external_id)
)
end
- def options_for_get
- @options_for_get ||= WebAuthn::Credential.options_for_get(
+ def options_for_get(relying_party)
+ @options_for_get ||= relying_party.options_for_authentication(
allow: webauthn_external_id # TODO: Maybe also allow all other tokens? Let's see
)
end
diff --git a/modules/two_factor_authentication/lib/open_project/two_factor_authentication/engine.rb b/modules/two_factor_authentication/lib/open_project/two_factor_authentication/engine.rb
index b6c4b31763a..6b7a9cfc16b 100644
--- a/modules/two_factor_authentication/lib/open_project/two_factor_authentication/engine.rb
+++ b/modules/two_factor_authentication/lib/open_project/two_factor_authentication/engine.rb
@@ -61,12 +61,6 @@ module OpenProject::TwoFactorAuthentication
}) do
two_factor_authentication_request_path
end
-
- WebAuthn.configure do |config|
- # TODO: Real settings here
- config.origin = "http://localhost:3000"
- config.rp_name = "Example Inc."
- end
end
end
end
diff --git a/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy/webauthn.rb b/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy/webauthn.rb
index 69acd0731ed..3c035a22130 100644
--- a/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy/webauthn.rb
+++ b/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy/webauthn.rb
@@ -3,14 +3,13 @@ require 'webauthn'
module OpenProject::TwoFactorAuthentication
module TokenStrategy
class Webauthn < Base
- def verify(webauthn_credential, webauthn_challenge:)
- credential = WebAuthn::Credential.from_get(JSON.parse(webauthn_credential))
-
+ def verify(webauthn_credential, webauthn_challenge:, webauthn_relying_party:)
# This will raise WebAuthn::Error
- credential.verify(
+ credential = webauthn_relying_party.verify_authentication(
+ webauthn_credential,
webauthn_challenge,
- public_key: device.webauthn_public_key,
- sign_count: device.webauthn_sign_count
+ sign_count: device.webauthn_sign_count,
+ public_key: device.webauthn_public_key
)
device.update!(webauthn_sign_count: credential.sign_count)