/
test_virtual_authenticator.py
249 lines (183 loc) · 9.03 KB
/
test_virtual_authenticator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import pytest
from base64 import urlsafe_b64decode, urlsafe_b64encode
from selenium.common.exceptions import InvalidArgumentException
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.virtual_authenticator import (
Credential,
VirtualAuthenticatorOptions,
)
BASE64__ENCODED_PK = '''
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDbBOu5Lhs4vpowbCnmCyLUpIE7JM9sm9QXzye2G+jr+Kr
MsinWohEce47BFPJlTaDzHSvOW2eeunBO89ZcvvVc8RLz4qyQ8rO98xS1jtgqi1NcBPETDrtzthODu/gd0sjB2Tk3TLuBGV
oPXt54a+Oo4JbBJ6h3s0+5eAfGplCbSNq6hN3Jh9YOTw5ZA6GCEy5l8zBaOgjXytd2v2OdSVoEDNiNQRkjJd2rmS2oi9AyQ
FR3B7BrPSiDlCcITZFOWgLF5C31Wp/PSHwQhlnh7/6YhnE2y9tzsUvzx0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2S7aa
9QulbC3C/qgGFNrcWgcT9kCgYAZTa1P9bFCDU7hJc2mHwJwAW7/FQKEJg8SL33KINpLwcR8fqaYOdAHWWz636osVEqosRrH
zJOGpf9x2RSWzQJ+dq8+6fACgfFZOVpN644+sAHfNPAI/gnNKU5OfUv+eav8fBnzlf1A3y3GIkyMyzFN3DE7e0n/lyqxE4H
BYGpI8g==
'''
@pytest.fixture(scope="module", autouse=True)
def driver():
yield WebDriver()
def test_virtual_authenticator_options():
options = VirtualAuthenticatorOptions()
options.is_user_verified = True
options.has_user_verification = True
options.is_user_consenting = True
options.transport = VirtualAuthenticatorOptions.Transport.USB
options.protocol = VirtualAuthenticatorOptions.Protocol.U2F
options.has_resident_key = False
assert len(options.to_dict()) == 6
def test_create_authenticator(driver):
# Create virtual authenticator options
options = VirtualAuthenticatorOptions()
options.protocol = VirtualAuthenticatorOptions.Protocol.U2F
options.has_resident_key = False
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# Get list of credentials
credential_list = driver.get_credentials()
assert len(credential_list) == 0
def test_remove_authenticator(driver):
# Create default virtual authenticator option
options = VirtualAuthenticatorOptions()
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# Remove virtual authenticator
driver.remove_virtual_authenticator()
assert driver.virtual_authenticator_id is None
def test_create_and_add_resident_key(driver):
# Create virtual authenticator options
options = VirtualAuthenticatorOptions()
options.protocol = VirtualAuthenticatorOptions.Protocol.CTAP2
options.has_resident_key = True
options.has_user_verification = True
options.is_user_verified = True
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# parameters for Resident Credential
credential_id = bytearray({1, 2, 3, 4})
rp_id = "localhost"
user_handle = bytearray({1})
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK)
sign_count = 0
# create a resident credential using above parameters
resident_credential = Credential.create_resident_credential(credential_id, rp_id, user_handle, privatekey, sign_count)
# add the credential created to virtual authenticator
driver.add_credential(resident_credential)
# get list of all the registered credentials
credential_list = driver.get_credentials()
assert len(credential_list) == 1
def test_add_resident_credential_not_supported_when_authenticator_uses_u2f_protocol(driver):
# Create virtual authenticator options
options = VirtualAuthenticatorOptions()
options.protocol = VirtualAuthenticatorOptions.Protocol.U2F
options.has_resident_key = False
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# parameters for Resident Credential
credential_id = bytearray({1, 2, 3, 4})
rp_id = "localhost"
user_handle = bytearray({1})
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK)
sign_count = 0
# create a resident credential using above parameters
credential = Credential.create_resident_credential(credential_id, rp_id, user_handle, privatekey, sign_count)
# Expect InvalidArgumentException
with pytest.raises(InvalidArgumentException):
driver.add_credential(credential)
def test_create_and_add_non_resident_key(driver):
# Create virtual authenticator options
options = VirtualAuthenticatorOptions()
options.protocol = VirtualAuthenticatorOptions.Protocol.U2F
options.has_resident_key = False
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# parameters for Non Resident Credential
credential_id = bytearray({1, 2, 3, 4})
rp_id = "localhost"
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK)
sign_count = 0
# create a non resident credential using above parameters
credential = Credential.create_non_resident_credential(credential_id, rp_id, privatekey, sign_count)
# add the credential created to virtual authenticator
driver.add_credential(credential)
# get list of all the registered credentials
credential_list = driver.get_credentials()
assert len(credential_list) == 1
def test_get_credential(driver):
# Create virtual authenticator options
options = VirtualAuthenticatorOptions()
options.protocol = VirtualAuthenticatorOptions.Protocol.CTAP2
options.has_resident_key = True
options.has_user_verfied = True
options.is_user_verified = True
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# parameters for Resident Credential
credential_id = bytearray({1, 2, 3, 4})
rp_id = "localhost"
user_handle = bytearray({1})
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK)
sign_count = 0
# create a resident credential using above parameters
credential = Credential.create_resident_credential(credential_id, rp_id, user_handle, privatekey, sign_count)
# add the credential created to virtual authenticator
driver.add_credential(credential)
# get list of all the registered credentials
credential_list = driver.get_credentials()
assert len(credential_list) == 1
assert credential_list[0].id == urlsafe_b64encode(credential_id).decode()
def test_remove_credential(driver):
# Create default virtual authenticator options
options = VirtualAuthenticatorOptions()
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# parameters for Non Resident Credential
credential_id = bytearray({1, 2, 3, 4})
rp_id = "localhost"
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK)
sign_count = 0
# create a non resident credential using above parameters
credential = Credential.create_non_resident_credential(credential_id, rp_id, privatekey, sign_count)
# add the credential created to virtual authenticator
driver.add_credential(credential)
# remove the credential created from virtual authenticator
driver.remove_credential(credential.id)
# credential can also be removed using Byte Array
# driver.remove_credential(credential_id)
assert len(driver.get_credentials()) == 0
def test_remove_all_credentials(driver):
# Create default virtual authenticator options
options = VirtualAuthenticatorOptions()
options.has_resident_key = True
# Register a virtual authenticator
driver.add_virtual_authenticator(options)
# parameters for Resident Credential
credential_id = bytearray({1, 2, 3, 4})
rp_id = "localhost"
user_handle = bytearray({1})
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK)
sign_count = 0
# create a resident credential using above parameters
resident_credential = Credential.create_resident_credential(credential_id, rp_id, user_handle, privatekey, sign_count)
# add the credential created to virtual authenticator
driver.add_credential(resident_credential)
# remove all credentials in virtual authenticator
driver.remove_all_credentials()
assert len(driver.get_credentials()) == 0
def test_set_user_verified():
# Create virtual authenticator options
options = VirtualAuthenticatorOptions()
options.is_user_verified = True
assert options.to_dict().get("isUserVerified") is True