| 1 |
import json |
| 2 |
from json.decoder import JSONDecodeError |
| 3 |
from typing import Union, Optional, List |
| 4 |
|
| 5 |
from .base64url_to_bytes import base64url_to_bytes |
| 6 |
from .exceptions import InvalidRegistrationResponse, InvalidJSONStructure |
| 7 |
from .structs import ( |
| 8 |
AuthenticatorAttachment, |
| 9 |
AuthenticatorAttestationResponse, |
| 10 |
AuthenticatorTransport, |
| 11 |
PublicKeyCredentialType, |
| 12 |
RegistrationCredential, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
def parse_registration_credential_json(json_val: Union[str, dict]) -> RegistrationCredential: |
| 17 |
""" |
| 18 |
Parse a JSON form of a registration credential, as either a stringified JSON object or a |
| 19 |
plain dict, into an instance of RegistrationCredential |
| 20 |
""" |
| 21 |
if isinstance(json_val, str): |
| 22 |
try: |
| 23 |
json_val = json.loads(json_val) |
| 24 |
except JSONDecodeError: |
| 25 |
raise InvalidJSONStructure("Unable to decode credential as JSON") |
| 26 |
|
| 27 |
if not isinstance(json_val, dict): |
| 28 |
raise InvalidJSONStructure("Credential was not a JSON object") |
| 29 |
|
| 30 |
cred_id = json_val.get("id") |
| 31 |
if not isinstance(cred_id, str): |
| 32 |
raise InvalidJSONStructure("Credential missing required id") |
| 33 |
|
| 34 |
cred_raw_id = json_val.get("rawId") |
| 35 |
if not isinstance(cred_raw_id, str): |
| 36 |
raise InvalidJSONStructure("Credential missing required rawId") |
| 37 |
|
| 38 |
cred_response = json_val.get("response") |
| 39 |
if not isinstance(cred_response, dict): |
| 40 |
raise InvalidJSONStructure("Credential missing required response") |
| 41 |
|
| 42 |
response_client_data_json = cred_response.get("clientDataJSON") |
| 43 |
if not isinstance(response_client_data_json, str): |
| 44 |
raise InvalidJSONStructure("Credential response missing required clientDataJSON") |
| 45 |
|
| 46 |
response_attestation_object = cred_response.get("attestationObject") |
| 47 |
if not isinstance(response_attestation_object, str): |
| 48 |
raise InvalidJSONStructure("Credential response missing required attestationObject") |
| 49 |
|
| 50 |
cred_type = json_val.get("type") |
| 51 |
try: |
| 52 |
# Simply try to get the single matching Enum. We'll set the literal value below assuming |
| 53 |
# the code can get past here (this is basically a mypy optimization) |
| 54 |
PublicKeyCredentialType(cred_type) |
| 55 |
except ValueError as cred_type_exc: |
| 56 |
raise InvalidJSONStructure("Credential had unexpected type") from cred_type_exc |
| 57 |
|
| 58 |
transports: Optional[List[AuthenticatorTransport]] = None |
| 59 |
response_transports = cred_response.get("transports") |
| 60 |
if isinstance(response_transports, list): |
| 61 |
transports = [] |
| 62 |
for val in response_transports: |
| 63 |
try: |
| 64 |
transport_enum = AuthenticatorTransport(val) |
| 65 |
transports.append(transport_enum) |
| 66 |
except ValueError: |
| 67 |
pass |
| 68 |
|
| 69 |
cred_authenticator_attachment = json_val.get("authenticatorAttachment") |
| 70 |
if isinstance(cred_authenticator_attachment, str): |
| 71 |
try: |
| 72 |
cred_authenticator_attachment = AuthenticatorAttachment(cred_authenticator_attachment) |
| 73 |
except ValueError as cred_attachment_exc: |
| 74 |
raise InvalidJSONStructure( |
| 75 |
"Credential had unexpected authenticatorAttachment" |
| 76 |
) from cred_attachment_exc |
| 77 |
else: |
| 78 |
cred_authenticator_attachment = None |
| 79 |
|
| 80 |
try: |
| 81 |
registration_credential = RegistrationCredential( |
| 82 |
id=cred_id, |
| 83 |
raw_id=base64url_to_bytes(cred_raw_id), |
| 84 |
response=AuthenticatorAttestationResponse( |
| 85 |
client_data_json=base64url_to_bytes(response_client_data_json), |
| 86 |
attestation_object=base64url_to_bytes(response_attestation_object), |
| 87 |
transports=transports, |
| 88 |
), |
| 89 |
authenticator_attachment=cred_authenticator_attachment, |
| 90 |
type=PublicKeyCredentialType.PUBLIC_KEY, |
| 91 |
) |
| 92 |
except Exception as exc: |
| 93 |
raise InvalidRegistrationResponse( |
| 94 |
"Could not parse registration credential from JSON data" |
| 95 |
) from exc |
| 96 |
|
| 97 |
return registration_credential |