diff --git a/CHANGELOG.md b/CHANGELOG.md index 6db6734f..bf60d00f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixed + +- Update `RelyingParty#origin` and `WebAuthn.configuration.origin` to return the allowed origin if allowed origins has only one element. + ## [v3.4.2] - 2025-09-22 ### Added diff --git a/lib/webauthn/relying_party.rb b/lib/webauthn/relying_party.rb index 06589d9f..9d2b84d1 100644 --- a/lib/webauthn/relying_party.rb +++ b/lib/webauthn/relying_party.rb @@ -54,7 +54,7 @@ def initialize( :acceptable_attestation_types, :legacy_u2f_appid - attr_reader :attestation_root_certificates_finders, :origin + attr_reader :attestation_root_certificates_finders # This is the user-data encoder. # Used to decode user input and to encode data provided to the user. @@ -121,6 +121,17 @@ def verify_authentication( end end + # DEPRECATED: This method will be removed in future. + def origin + warn( + "DEPRECATION WARNING: `WebAuthn.origin` is deprecated and will be removed in future. "\ + "Please use `WebAuthn.allowed_origins` instead "\ + "that also allows configuring multiple origins per Relying Party" + ) + + allowed_origins.first if allowed_origins&.size == 1 + end + # DEPRECATED: This method will be removed in future. def origin=(new_origin) return if new_origin.nil? diff --git a/spec/webauthn/relying_party_spec.rb b/spec/webauthn/relying_party_spec.rb index b15d6392..a8c46a78 100644 --- a/spec/webauthn/relying_party_spec.rb +++ b/spec/webauthn/relying_party_spec.rb @@ -135,6 +135,42 @@ end end + describe '#origin' do + subject do + old_verbose, $VERBOSE = $VERBOSE, nil # Silence warnings to avoid deprecation warnings + + rp.origin + ensure + $VERBOSE = old_verbose + end + + context 'when relying party has only one allowed origin' do + let(:rp) do + WebAuthn::RelyingParty.new(allowed_origins: ["https://admin.example.test"]) + end + + it 'returns that allowed origin' do + is_expected.to eq("https://admin.example.test") + end + end + + context 'when relying party has multiple allowed origins' do + let(:rp) do + WebAuthn::RelyingParty.new(allowed_origins: ["https://admin.example.test", "https://newadmin.example.test"]) + end + + it { is_expected.to be_nil } + end + + context 'when relying party has not set its allowed origins' do + let(:rp) do + WebAuthn::RelyingParty.new(allowed_origins: nil) + end + + it { is_expected.to be_nil } + end + end + context "without having any global configuration" do let(:consumer_rp) do WebAuthn::RelyingParty.new(