"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/rb/spec/integration/selenium/webdriver/virtual_authenticator_spec.rb" (17 Feb 2023, 11219 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "virtual_authenticator_spec.rb":
4.7.0_vs_4.8.0.
1 # frozen_string_literal: true
2
3 # Licensed to the Software Freedom Conservancy (SFC) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The SFC licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19
20 require_relative 'spec_helper'
21
22 module Selenium
23 module WebDriver
24 describe VirtualAuthenticator, exclusive: {browser: %i[chrome edge]} do
25 # A pkcs#8 encoded unencrypted EC256 private key as a base64url string.
26 let(:pkcs8_private_key) do
27 'MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8_zMDQDYAxlU-Q' \
28 'hk1Dwkf0v18GZca1DMF3SaJ9HPdmShRANCAASNYX5lyVCOZLzFZzrIKmeZ2jwU' \
29 'RmgsJYxGP__fWN_S-j5sN4tT15XEpN_7QZnt14YvI6uvAgO0uJEboFaZlOEB'
30 end
31 let(:encoded_private_key) do
32 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDbBOu5Lhs4vpowbCnmCyLUpIE7JM9sm9QXzye2G+jr+Kr' \
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h6OQncChPUlOLv9FMZPR1ZOfqLCYrjYEqi' \
44 'uzGm6iKM13zXFO4AGAxu1P/IAd5BovFcTpg79Z8tWqZaUUwvscnl+cRlj+mMXAmdqCeO8VASOmqM1ml667axeZDIR867ZG8' \
45 'K5V029Wg+4qtX5uFypNAAi6GfHkxIKrD04yOHAoGACdh4wXESi0oiDdkz3KOHPwIjn6BhZC7z8mx+pnJODU3cYukxv3WTct' \
46 'lUhAsyjJiQ/0bK1yX87ulqFVgO0Knmh+wNajrb9wiONAJTMICG7tiWJOm7fW5cfTJwWkBwYADmkfTRmHDvqzQSSvoC2S7aa' \
47 '9QulbC3C/qgGFNrcWgcT9kCgYAZTa1P9bFCDU7hJc2mHwJwAW7/FQKEJg8SL33KINpLwcR8fqaYOdAHWWz636osVEqosRrH' \
48 'zJOGpf9x2RSWzQJ+dq8+6fACgfFZOVpN644+sAHfNPAI/gnNKU5OfUv+eav8fBnzlf1A3y3GIkyMyzFN3DE7e0n/lyqxE4H' \
49 'BYGpI8g=='
50 end
51 let(:u2f) { VirtualAuthenticatorOptions.new(protocol: :u2f) }
52 let(:ctap2) { VirtualAuthenticatorOptions.new(user_verification: true, user_verified: true) }
53
54 before { driver.navigate.to url_for('virtual-authenticator.html') }
55
56 after { @authenticator&.remove! if @authenticator&.valid? }
57
58 def register_credential(require_resident: false, user_verification: false)
59 params = if require_resident
60 '{authenticatorSelection: {requireResidentKey: true}}'
61 elsif user_verification
62 "{userVerification: 'required'}"
63 else
64 ''
65 end
66 driver.execute_async_script "registerCredential(#{params}).then(arguments[arguments.length - 1]);"
67 end
68
69 def credential(id)
70 script = <<~CREDENTIAL
71 getCredential([{
72 "type": "public-key",
73 "id": Int8Array.from(arguments[0]),
74 }]).then(arguments[arguments.length - 1]);
75 CREDENTIAL
76 driver.execute_async_script(script, id)
77 end
78
79 describe '#intialize' do
80 it 'creates resident key disabled u2f' do
81 @authenticator = driver.add_virtual_authenticator(u2f)
82
83 expect(@authenticator.options.protocol).to eq :u2f
84 expect(@authenticator.options.resident_key?).to be false
85 end
86
87 it 'creates resident key enabled u2f' do
88 u2f.resident_key = true
89 @authenticator = driver.add_virtual_authenticator(u2f)
90
91 expect(@authenticator.options.protocol).to eq :u2f
92 expect(@authenticator.options.resident_key?).to be true
93 end
94
95 it 'creates resident key disabled ctap2' do
96 @authenticator = driver.add_virtual_authenticator(ctap2)
97
98 expect(@authenticator.options.protocol).to eq :ctap2
99 expect(@authenticator.options.resident_key?).to be false
100 expect(@authenticator.options.user_verified?).to be true
101 expect(@authenticator.options.user_verification?).to be true
102 end
103
104 it 'creates resident key enabled ctap2' do
105 ctap2.resident_key = true
106 @authenticator = driver.add_virtual_authenticator(ctap2)
107
108 expect(@authenticator.options.protocol).to eq :ctap2
109 expect(@authenticator.options.resident_key?).to be true
110 expect(@authenticator.options.user_verified?).to be true
111 expect(@authenticator.options.user_verification?).to be true
112 end
113 end
114
115 describe '#remove!' do
116 it 'removes authenticator' do
117 @authenticator = driver.add_virtual_authenticator(u2f)
118
119 @authenticator.remove!
120
121 expect(@authenticator.valid?).to be false
122 end
123 end
124
125 describe '#add_credential' do
126 it 'to non-resident ctap' do
127 @authenticator = driver.add_virtual_authenticator(ctap2)
128
129 byte_array_id = [1, 2, 3, 4]
130 credential = Credential.non_resident(id: byte_array_id,
131 rp_id: 'localhost',
132 private_key: Credential.decode(encoded_private_key))
133
134 @authenticator.add_credential(credential)
135
136 expect(credential(byte_array_id)['status']).to eq('OK')
137 end
138
139 it 'to non-resident u2f' do
140 @authenticator = driver.add_virtual_authenticator(u2f)
141
142 byte_array_id = [1, 2, 3, 4]
143 credential = Credential.non_resident(id: byte_array_id,
144 rp_id: 'localhost',
145 private_key: Credential.decode(pkcs8_private_key))
146
147 @authenticator.add_credential(credential)
148
149 expect(credential(byte_array_id)['status']).to eq('OK')
150 end
151
152 it 'to resident ctap' do
153 ctap2.resident_key = true
154 @authenticator = driver.add_virtual_authenticator(ctap2)
155
156 byte_array_id = [1, 2, 3, 4]
157 credential = Credential.resident(id: byte_array_id,
158 rp_id: 'localhost',
159 user_handle: [1],
160 private_key: Credential.decode(encoded_private_key))
161
162 @authenticator.add_credential(credential)
163
164 expect(credential(byte_array_id)['status']).to eq('OK')
165 expect(@authenticator.credentials.first.user_handle).to eq [1]
166 end
167
168 it 'to resident u2f' do
169 u2f.resident_key = true
170 @authenticator = driver.add_virtual_authenticator(u2f)
171
172 byte_array_id = [1, 2, 3, 4]
173 credential = Credential.resident(id: byte_array_id,
174 rp_id: 'localhost',
175 user_handle: [1],
176 private_key: Credential.decode(encoded_private_key))
177
178 msg = /The Authenticator does not support Resident Credentials/
179 expect { @authenticator.add_credential(credential) }.to raise_error(Error::InvalidArgumentError, msg)
180 end
181 end
182
183 describe '#credentials' do
184 it 'gets multiple' do
185 ctap2.resident_key = true
186 @authenticator = driver.add_virtual_authenticator(ctap2)
187
188 # Add multiple credentials with JS
189 res_cred_resp = register_credential(require_resident: true)['credential']
190 non_res_cred_resp = register_credential['credential']
191 expect(res_cred_resp['id']).not_to eq(non_res_cred_resp['id'])
192
193 credentials = @authenticator.credentials
194 expect(credentials.length).to eq(2)
195
196 res_cred_output = credentials.find { |cred| Credential.encode(cred.id).match res_cred_resp['id'] }
197 non_res_cred_output = credentials.tap { |cred| cred.delete(res_cred_output) }.first
198
199 expect(res_cred_output.resident_credential?).to be true
200 expect(res_cred_output.rp_id).to eq 'localhost'
201 expect(res_cred_output.sign_count).to eq 1
202 expect(res_cred_output.user_handle).to eq [1]
203
204 expect(non_res_cred_output.resident_credential?).to be false
205 expect(non_res_cred_output.rp_id).to be_nil
206 expect(non_res_cred_output.sign_count).to eq 1
207 expect(non_res_cred_output.user_handle).to be_nil
208 end
209 end
210
211 describe '#remove_credential' do
212 it 'by raw ID' do
213 @authenticator = driver.add_virtual_authenticator(u2f)
214
215 credential = register_credential['credential']
216 raw_id = credential['rawId']
217 id = credential['id']
218 2.times { register_credential }
219
220 @authenticator.remove_credential(raw_id)
221
222 expect(@authenticator.credentials.map(&:id)).not_to include(id)
223 end
224
225 it 'by encoded ID' do
226 @authenticator = driver.add_virtual_authenticator(u2f)
227
228 id = register_credential.dig('credential', 'id')
229 2.times { register_credential }
230
231 @authenticator.remove_credential(id)
232
233 expect(@authenticator.credentials.map(&:id)).not_to include(id)
234 end
235 end
236
237 describe '#remove_all_credentials' do
238 it 'removes multiple' do
239 @authenticator = driver.add_virtual_authenticator(u2f)
240 3.times { register_credential }
241
242 @authenticator.remove_all_credentials
243
244 expect(@authenticator.credentials).to be_empty
245 end
246 end
247
248 describe '#user_verified=' do
249 it 'can not obtain credential requiring verification when set to false' do
250 ctap2.resident_key = true
251 @authenticator = driver.add_virtual_authenticator(ctap2)
252
253 raw_id = register_credential(user_verification: true).dig('credential', 'rawId')
254
255 @authenticator.user_verified = false
256
257 expect(credential(raw_id)['status']).to include 'NotAllowedError'
258 end
259 end
260 end # VirtualAuthenticator
261 end # WebDriver
262 end # Selenium