| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from base64 import urlsafe_b64decode |
| 19 |
from base64 import urlsafe_b64encode |
| 20 |
from typing import Tuple |
| 21 |
|
| 22 |
import pytest |
| 23 |
|
| 24 |
from selenium.webdriver.common.virtual_authenticator import Credential |
| 25 |
|
| 26 |
BASE64__ENCODED_PK = """ |
| 27 |
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDbBOu5Lhs4vpowbCnmCyLUpIE7JM9sm9QXzye2G+jr+Kr |
| 28 |
MsinWohEce47BFPJlTaDzHSvOW2eeunBO89ZcvvVc8RLz4qyQ8rO98xS1jtgqi1NcBPETDrtzthODu/gd0sjB2Tk3TLuBGV |
| 29 |
oPXt54a+Oo4JbBJ6h3s0+5eAfGplCbSNq6hN3Jh9YOTw5ZA6GCEy5l8zBaOgjXytd2v2OdSVoEDNiNQRkjJd2rmS2oi9AyQ |
| 30 |
FR3B7BrPSiDlCcITZFOWgLF5C31Wp/PSHwQhlnh7/6YhnE2y9tzsUvzx0wJXrBADW13+oMxrneDK3WGbxTNYgIi1PvSqXlq |
| 31 |
GjHtCK+R2QkXAgMBAAECggEAVc6bu7VAnP6v0gDOeX4razv4FX/adCao9ZsHZ+WPX8PQxtmWYqykH5CY4TSfsuizAgyPuQ0 |
| 32 |
+j4Vjssr9VODLqFoanspT6YXsvaKanncUYbasNgUJnfnLnw3an2XpU2XdmXTNYckCPRX9nsAAURWT3/n9ljc/XYY22ecYxM |
| 33 |
8sDWnHu2uKZ1B7M3X60bQYL5T/lVXkKdD6xgSNLeP4AkRx0H4egaop68hoW8FIwmDPVWYVAvo8etzWCtibRXz5FcNld9MgD |
| 34 |
/Ai7ycKy4Q1KhX5GBFI79MVVaHkSQfxPHpr7/XcmpQOEAr+BMPon4s4vnKqAGdGB3j/E3d/+4F2swykoQKBgQD8hCsp6FIQ |
| 35 |
5umJlk9/j/nGsMl85LgLaNVYpWlPRKPc54YNumtvj5vx1BG+zMbT7qIE3nmUPTCHP7qb5ERZG4CdMCS6S64/qzZEqijLCqe |
| 36 |
pwj6j4fV5SyPWEcpxf6ehNdmcfgzVB3Wolfwh1ydhx/96L1jHJcTKchdJJzlfTvq8wwKBgQDeCnKws1t5GapfE1rmC/h4ol |
| 37 |
L2qZTth9oQmbrXYohVnoqNFslDa43ePZwL9Jmd9kYb0axOTNMmyrP0NTj41uCfgDS0cJnNTc63ojKjegxHIyYDKRZNVUR/d |
| 38 |
xAYB/vPfBYZUS7M89pO6LLsHhzS3qpu3/hppo/Uc/AM/r8PSflNHQKBgDnWgBh6OQncChPUlOLv9FMZPR1ZOfqLCYrjYEqi |
| 39 |
uzGm6iKM13zXFO4AGAxu1P/IAd5BovFcTpg79Z8tWqZaUUwvscnl+cRlj+mMXAmdqCeO8VASOmqM1ml667axeZDIR867ZG8 |
| 40 |
K5V029Wg+4qtX5uFypNAAi6GfHkxIKrD04yOHAoGACdh4wXESi0oiDdkz3KOHPwIjn6BhZC7z8mx+pnJODU3cYukxv3WTct |
| 41 |
lUhAsyjJiQ/0bK1yX87ulqFVgO0Knmh+wNajrb9wiONAJTMICG7tiWJOm7fW5cfTJwWkBwYADmkfTRmHDvqzQSSvoC2S7aa |
| 42 |
9QulbC3C/qgGFNrcWgcT9kCgYAZTa1P9bFCDU7hJc2mHwJwAW7/FQKEJg8SL33KINpLwcR8fqaYOdAHWWz636osVEqosRrH |
| 43 |
zJOGpf9x2RSWzQJ+dq8+6fACgfFZOVpN644+sAHfNPAI/gnNKU5OfUv+eav8fBnzlf1A3y3GIkyMyzFN3DE7e0n/lyqxE4H |
| 44 |
BYGpI8g== |
| 45 |
""" |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture() |
| 49 |
def data() -> Tuple: |
| 50 |
_id = bytearray({1, 2, 3, 4}) |
| 51 |
rp_id = "localhost" |
| 52 |
user_handle = bytearray({1}) |
| 53 |
privatekey = urlsafe_b64decode(BASE64__ENCODED_PK) |
| 54 |
sign_count = 0 |
| 55 |
return (_id, rp_id, user_handle, privatekey, sign_count) |
| 56 |
|
| 57 |
|
| 58 |
def test_rk_enabled_credential(data): |
| 59 |
_id, rp_id, user_handle, privatekey, sign_count = data |
| 60 |
|
| 61 |
credential = Credential.create_resident_credential(_id, rp_id, user_handle, privatekey, sign_count) |
| 62 |
assert credential.id == urlsafe_b64encode(bytearray({1, 2, 3, 4})).decode() |
| 63 |
if credential.is_resident_credential is True: |
| 64 |
assert True |
| 65 |
assert credential.rp_id == "localhost" |
| 66 |
assert credential.user_handle == urlsafe_b64encode(bytearray({1})).decode() |
| 67 |
assert credential.private_key == urlsafe_b64encode(privatekey).decode() |
| 68 |
assert credential.sign_count == 0 |
| 69 |
|
| 70 |
|
| 71 |
def test_rk_disabled_credentials(data): |
| 72 |
_id, rp_id, user_handle, privatekey, sign_count = data |
| 73 |
credential = Credential.create_non_resident_credential(_id, rp_id, privatekey, sign_count) |
| 74 |
|
| 75 |
assert credential.id == urlsafe_b64encode(bytearray({1, 2, 3, 4})).decode() |
| 76 |
assert credential.private_key == urlsafe_b64encode(privatekey).decode() |
| 77 |
assert credential.sign_count == 0 |
| 78 |
assert credential.rp_id == "localhost" |
| 79 |
if credential.is_resident_credential is False: |
| 80 |
assert True |
| 81 |
else: |
| 82 |
assert False |
| 83 |
if credential.user_handle is None: |
| 84 |
assert True |
| 85 |
else: |
| 86 |
assert False |
| 87 |
|
| 88 |
|
| 89 |
def test_to_dict(data): |
| 90 |
_id, rp_id, user_handle, privatekey, sign_count = data |
| 91 |
credential = Credential.create_resident_credential(_id, rp_id, user_handle, privatekey, sign_count) |
| 92 |
|
| 93 |
credential_dict = credential.to_dict() |
| 94 |
assert credential_dict["credentialId"] == urlsafe_b64encode(bytearray({1, 2, 3, 4})).decode() |
| 95 |
if credential_dict["isResidentCredential"] is True: |
| 96 |
assert True |
| 97 |
else: |
| 98 |
assert False |
| 99 |
assert credential_dict["rpId"] == "localhost" |
| 100 |
assert credential_dict["userHandle"] == urlsafe_b64encode(bytearray({1})).decode() |
| 101 |
assert credential_dict["privateKey"] == urlsafe_b64encode(privatekey).decode() |
| 102 |
assert credential_dict["signCount"] == 0 |
| 103 |
|
| 104 |
|
| 105 |
def test_from_dict(): |
| 106 |
data = { |
| 107 |
"credentialId": urlsafe_b64encode(bytearray({1, 2, 3, 4})).decode(), |
| 108 |
"isResidentCredential": True, |
| 109 |
"rpId": "localhost", |
| 110 |
"userHandle": urlsafe_b64encode(bytearray({1})).decode(), |
| 111 |
"privateKey": BASE64__ENCODED_PK, |
| 112 |
"signCount": 0, |
| 113 |
} |
| 114 |
|
| 115 |
credential = Credential.from_dict(data) |
| 116 |
|
| 117 |
key = urlsafe_b64decode(BASE64__ENCODED_PK) |
| 118 |
|
| 119 |
assert credential.id == urlsafe_b64encode(bytearray({1, 2, 3, 4})).decode() |
| 120 |
if credential.is_resident_credential is True: |
| 121 |
assert True |
| 122 |
else: |
| 123 |
assert False |
| 124 |
assert credential.rp_id == "localhost" |
| 125 |
assert credential.user_handle == urlsafe_b64encode(bytearray({1})).decode() |
| 126 |
assert credential.private_key == urlsafe_b64encode(key).decode() |
| 127 |
assert credential.sign_count == 0 |