Skip to content

Commit 3126bdc

Browse files
committed
Add VirtualAuthenticator tests to Ruby examples
* Adding these tests helps document how the Selenium library can be used with virtual authenticators
1 parent abbba6c commit 3126bdc

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'VirtualAuthenticatorOptions' do
6+
let(:options) { Selenium::WebDriver::VirtualAuthenticatorOptions.new }
7+
8+
it "can set the various options" do
9+
options.user_verified = true
10+
options.user_verification = true
11+
options.user_consenting = true
12+
options.transport = :usb
13+
options.protocol = :u2f
14+
options.resident_key = false
15+
16+
expect(options.user_verified).to eq(true)
17+
expect(options.user_verification).to eq(true)
18+
expect(options.transport).to eq(:usb)
19+
expect(options.protocol).to eq(:u2f)
20+
expect(options.resident_key).to eq(false)
21+
end
22+
23+
it "sets isUserVerified correctly in as part of as_json" do
24+
options.user_verified = true
25+
expect(options.as_json["isUserVerified"]).to eq(true)
26+
end
27+
end
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'VirtualAuthenticator' do
6+
let(:driver) { start_session }
7+
8+
let(:private_key) {
9+
Base64.strict_encode64(OpenSSL::PKey::RSA.generate(2048).private_to_der)
10+
}
11+
12+
let(:options) { Selenium::WebDriver::VirtualAuthenticatorOptions.new }
13+
14+
it "Registers a new virtual authenticator" do
15+
options.protocol = :u2f
16+
options.resident_key = false
17+
18+
authenticator = driver.add_virtual_authenticator(options)
19+
credential_list = authenticator.credentials
20+
21+
expect(credential_list).to be_empty
22+
end
23+
24+
it "Removes a virtual authenticator" do
25+
authenticator = driver.add_virtual_authenticator(options)
26+
27+
authenticator.remove!
28+
29+
expect {
30+
authenticator.credentials
31+
}.to raise_error(Selenium::WebDriver::Error::InvalidArgumentError)
32+
end
33+
34+
it "Creates and adds a resident_key" do
35+
options.protocol = :ctap2
36+
options.resident_key = true
37+
options.user_verification = true
38+
options.user_verified = true
39+
40+
authenticator = driver.add_virtual_authenticator(options)
41+
42+
credential_id = [1,2,3,4] # byte array
43+
user_handle = [1] # byte array
44+
# decode from Base64, then turn into a byte array
45+
decoded_private_key = Base64.strict_decode64(private_key).bytes
46+
47+
resident_credential = Selenium::WebDriver::Credential.resident(
48+
id: credential_id,
49+
private_key: decoded_private_key,
50+
rp_id: "localhost",
51+
user_handle: user_handle
52+
)
53+
54+
authenticator.add_credential(resident_credential)
55+
56+
credential_list = authenticator.credentials
57+
58+
expect(credential_list.size).to eq(1)
59+
60+
expect(credential_id).to eq(credential_list[0].id)
61+
end
62+
63+
it "adding resident keys is not supported when the authenticator uses the U2F protocol" do
64+
options.protocol = :u2f
65+
options.resident_key = true
66+
67+
authenticator = driver.add_virtual_authenticator(options)
68+
69+
credential_id = [1,2,3,4] # byte array
70+
user_handle = [1] # byte array
71+
# decode from Base64, then turn into a byte array
72+
decoded_private_key = Base64.strict_decode64(private_key).bytes
73+
74+
resident_credential = Selenium::WebDriver::Credential.resident(
75+
id: credential_id,
76+
private_key: decoded_private_key,
77+
rp_id: "localhost",
78+
user_handle: user_handle
79+
)
80+
81+
expect {
82+
authenticator.add_credential(resident_credential)
83+
}.to raise_error(Selenium::WebDriver::Error::InvalidArgumentError)
84+
end
85+
86+
it "Creates and adds a non-resident key" do
87+
options.protocol = :u2f
88+
options.resident_key = false
89+
90+
authenticator = driver.add_virtual_authenticator(options)
91+
92+
credential_id = [1,2,3,4] # byte array
93+
# decode from Base64, then turn into a byte array
94+
decoded_private_key = Base64.strict_decode64(private_key).bytes
95+
96+
resident_credential = Selenium::WebDriver::Credential.non_resident(
97+
id: credential_id,
98+
private_key: decoded_private_key,
99+
rp_id: "localhost"
100+
)
101+
102+
authenticator.add_credential(resident_credential)
103+
104+
credential_list = authenticator.credentials
105+
106+
expect(credential_list.size).to eq(1)
107+
108+
expect(credential_id).to eq(credential_list[0].id)
109+
end
110+
111+
it "Can get a credential" do
112+
options.protocol = :ctap2
113+
options.resident_key = true
114+
options.user_verification = true
115+
options.user_verified = true
116+
117+
authenticator = driver.add_virtual_authenticator(options)
118+
119+
credential_id = [1,2,3,4] # byte array
120+
user_handle = [1] # byte array
121+
# decode from Base64, then turn into a byte array
122+
decoded_private_key = Base64.strict_decode64(private_key).bytes
123+
124+
resident_credential = Selenium::WebDriver::Credential.resident(
125+
id: credential_id,
126+
private_key: decoded_private_key,
127+
rp_id: "localhost",
128+
user_handle: user_handle
129+
)
130+
131+
authenticator.add_credential(resident_credential)
132+
133+
credential_list = authenticator.credentials
134+
135+
expect(credential_list.size).to eq(1)
136+
137+
expect(credential_id).to eq(credential_list[0].id)
138+
expect(private_key).to eq(credential_list[0].private_key)
139+
end
140+
141+
it "Removes all credentials" do
142+
authenticator = driver.add_virtual_authenticator(options)
143+
144+
credential_id = [1,2,3,4] # byte array
145+
# decode from Base64, then turn into a byte array
146+
decoded_private_key = Base64.strict_decode64(private_key).bytes
147+
148+
resident_credential = Selenium::WebDriver::Credential.non_resident(
149+
id: credential_id,
150+
private_key: decoded_private_key,
151+
rp_id: "localhost"
152+
)
153+
154+
authenticator.add_credential(resident_credential)
155+
authenticator.remove_all_credentials
156+
157+
credential_list = authenticator.credentials
158+
159+
expect(credential_list.size).to eq(0)
160+
end
161+
end

0 commit comments

Comments
 (0)