Skip to content

Commit 37a0409

Browse files
author
Vitalii Tereshchenko
committed
feat:allow multiple origins
* add a possibility to set `allowed_origins` configuration option that would be an alternative to `origin`
1 parent 4cb8365 commit 37a0409

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

lib/webauthn/authenticator_response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(client_data_json:, relying_party: WebAuthn.configuration.relying_
2525
end
2626

2727
def verify(expected_challenge, expected_origin = nil, user_verification: nil, rp_id: nil)
28-
expected_origin ||= relying_party.origin || raise("Unspecified expected origin")
28+
expected_origin ||= relying_party.origin || relying_party.allowed_origins || raise("Unspecified expected origin")
2929
rp_id ||= relying_party.id
3030

3131
verify_item(:type)

lib/webauthn/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Configuration
2222
:encoding=,
2323
:origin,
2424
:origin=,
25+
:allowed_origins,
26+
:allowed_origins=,
2527
:verify_attestation_statement,
2628
:verify_attestation_statement=,
2729
:credential_options_timeout,

lib/webauthn/relying_party.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def self.if_pss_supported(algorithm)
1818
def initialize(
1919
algorithms: DEFAULT_ALGORITHMS.dup,
2020
encoding: WebAuthn::Encoder::STANDARD_ENCODING,
21+
allowed_origins: nil,
2122
origin: nil,
2223
id: nil,
2324
name: nil,
@@ -31,6 +32,7 @@ def initialize(
3132
@algorithms = algorithms
3233
@encoding = encoding
3334
@origin = origin
35+
@allowed_origins = allowed_origins.nil? ? [origin] : allowed_origins
3436
@id = id
3537
@name = name
3638
@verify_attestation_statement = verify_attestation_statement
@@ -43,6 +45,7 @@ def initialize(
4345

4446
attr_accessor :algorithms,
4547
:encoding,
48+
:allowed_origins,
4649
:origin,
4750
:id,
4851
:name,

spec/webauthn/authenticator_attestation_response_spec.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
end
4040

4141
before do
42-
WebAuthn.configuration.origin = allowed_origins
42+
WebAuthn.configuration.allowed_origins = allowed_origins
4343
end
4444
end
4545

@@ -49,7 +49,7 @@
4949
end
5050
end
5151

52-
shared_examples "is valid and verifies" do
52+
shared_examples "a valid attestation response" do
5353
it "verifies" do
5454
expect(attestation_response.verify(original_challenge)).to be_truthy
5555
end
@@ -63,7 +63,7 @@
6363
context "when there is a single origin" do
6464
include_context "with a single origin"
6565

66-
it_behaves_like "is valid and verifies"
66+
it_behaves_like "a valid attestation response"
6767

6868
# TODO: let FakeClient#create recieve a fixed credential
6969
# https://github.com/cedarcode/webauthn-ruby/pull/302#discussion_r365338434
@@ -83,7 +83,7 @@
8383
context "when rp_id is set explicitly" do
8484
include_context "with rp_id set to", "localhost"
8585

86-
it_behaves_like "is valid and verifies"
86+
it_behaves_like "a valid attestation response"
8787

8888
# TODO: let FakeClient#create recieve a fixed credential
8989
# https://github.com/cedarcode/webauthn-ruby/pull/302#discussion_r365338434
@@ -100,11 +100,11 @@
100100
context "when rp_id is not set explicitly" do
101101
include_context "with rp_id set to", nil
102102

103-
it "verifies" do
103+
it "raises error" do
104104
expect { attestation_response.verify(original_challenge) }.to raise_error(WebAuthn::RpIdVerificationError)
105105
end
106106

107-
it "is valid" do
107+
it "is not valid" do
108108
expect(attestation_response.valid?(original_challenge)).to be_falsey
109109
end
110110

@@ -145,7 +145,7 @@
145145
WebAuthn.configuration.attestation_root_certificates_finders = finder_for('feitian_ft_fido_0200.pem')
146146
end
147147

148-
it_behaves_like "is valid and verifies"
148+
it_behaves_like "a valid attestation response"
149149

150150
it "returns attestation info" do
151151
attestation_response.valid?(original_challenge)
@@ -171,17 +171,17 @@
171171
context "when rp_id is set explicitly" do
172172
include_context "with rp_id set to", "localhost"
173173

174-
it_behaves_like "is valid and verifies"
174+
it_behaves_like "a valid attestation response"
175175
end
176176

177177
context "when rp_id is not set explicitly" do
178178
include_context "with rp_id set to", nil
179179

180-
it "verifies" do
180+
it "raises error" do
181181
expect { attestation_response.verify(original_challenge) }.to raise_error(WebAuthn::RpIdVerificationError)
182182
end
183183

184-
it "is valid" do
184+
it "is not valid" do
185185
expect(attestation_response.valid?(original_challenge)).to be_falsey
186186
end
187187
end
@@ -208,7 +208,7 @@
208208
)
209209
end
210210

211-
it_behaves_like "is valid and verifies"
211+
it_behaves_like "a valid attestation response"
212212

213213
it "returns attestation info" do
214214
attestation_response.valid?(original_challenge)
@@ -250,7 +250,7 @@
250250
WebAuthn.configuration.attestation_root_certificates_finders = finder_for('yubico_u2f_root.pem')
251251
end
252252

253-
it_behaves_like "is valid and verifies"
253+
it_behaves_like "a valid attestation response"
254254

255255
it "returns attestation info" do
256256
attestation_response.valid?(original_challenge)
@@ -350,7 +350,7 @@
350350
allow(attestation_response.attestation_statement).to receive(:time).and_return(time)
351351
end
352352

353-
it_behaves_like "is valid and verifies"
353+
it_behaves_like "a valid attestation response"
354354

355355
it "returns attestation info" do
356356
attestation_response.valid?(original_challenge)
@@ -391,7 +391,7 @@
391391

392392
include_context "with a single origin"
393393

394-
it_behaves_like "is valid and verifies"
394+
it_behaves_like "a valid attestation response"
395395

396396
it "returns attestation info" do
397397
attestation_response.valid?(original_challenge)
@@ -423,7 +423,7 @@
423423
context "when rp_id is set explicitly" do
424424
include_context "with rp_id set to", "localhost"
425425

426-
it_behaves_like "is valid and verifies"
426+
it_behaves_like "a valid attestation response"
427427

428428
it "returns attestation info" do
429429
attestation_response.valid?(original_challenge)
@@ -444,11 +444,11 @@
444444
context "when rp_id is not set explicitly" do
445445
include_context "with rp_id set to", nil
446446

447-
it "verifies" do
447+
it "raises error" do
448448
expect { attestation_response.verify(original_challenge) }.to raise_error(WebAuthn::RpIdVerificationError)
449449
end
450450

451-
it "is valid" do
451+
it "is not valid" do
452452
expect(attestation_response.valid?(original_challenge)).to be_falsey
453453
end
454454
end
@@ -479,7 +479,7 @@
479479
fake_certificate_chain_validation_time(attestation_response.attestation_statement, Time.parse("2021-02-23"))
480480
end
481481

482-
it_behaves_like "is valid and verifies"
482+
it_behaves_like "a valid attestation response"
483483

484484
it "returns attestation info" do
485485
attestation_response.valid?(original_challenge)
@@ -527,7 +527,7 @@
527527
context "matches the default one" do
528528
let(:actual_origin) { "http://localhost" }
529529

530-
it_behaves_like "is valid and verifies"
530+
it_behaves_like "a valid attestation response"
531531
end
532532

533533
context "doesn't match the default one" do
@@ -563,7 +563,7 @@
563563
context "matches the default one" do
564564
let(:rp_id) { "localhost" }
565565

566-
it_behaves_like "is valid and verifies"
566+
it_behaves_like "a valid attestation response"
567567
end
568568

569569
context "doesn't match the default one" do
@@ -587,7 +587,7 @@
587587
WebAuthn.configuration.rp_id = rp_id
588588
end
589589

590-
it_behaves_like "is valid and verifies"
590+
it_behaves_like "a valid attestation response"
591591
end
592592
end
593593

@@ -690,7 +690,7 @@
690690
WebAuthn.configuration.verify_attestation_statement = true
691691
end
692692

693-
it "verifies the attestation statement" do
693+
it "raises error" do
694694
expect { attestation_response.verify(original_challenge) }.to raise_error(OpenSSL::PKey::PKeyError)
695695
end
696696
end

0 commit comments

Comments
 (0)