Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/helpers/parse_authentication_credential_json.py

raw
1 import json
2 from json.decoder import JSONDecodeError
3 from typing import Union
4
5 from .exceptions import InvalidAuthenticationResponse, InvalidJSONStructure
6 from .base64url_to_bytes import base64url_to_bytes
7 from .structs import (
8 AuthenticationCredential,
9 AuthenticatorAssertionResponse,
10 AuthenticatorAttachment,
11 PublicKeyCredentialType,
12 )
13
14
15 def parse_authentication_credential_json(json_val: Union[str, dict]) -> AuthenticationCredential:
16 """
17 Parse a JSON form of an authentication credential, as either a stringified JSON object or a
18 plain dict, into an instance of AuthenticationCredential
19 """
20 if isinstance(json_val, str):
21 try:
22 json_val = json.loads(json_val)
23 except JSONDecodeError:
24 raise InvalidJSONStructure("Unable to decode credential as JSON")
25
26 if not isinstance(json_val, dict):
27 raise InvalidJSONStructure("Credential was not a JSON object")
28
29 cred_id = json_val.get("id")
30 if not isinstance(cred_id, str):
31 raise InvalidJSONStructure("Credential missing required id")
32
33 cred_raw_id = json_val.get("rawId")
34 if not isinstance(cred_raw_id, str):
35 raise InvalidJSONStructure("Credential missing required rawId")
36
37 cred_response = json_val.get("response")
38 if not isinstance(cred_response, dict):
39 raise InvalidJSONStructure("Credential missing required response")
40
41 response_client_data_json = cred_response.get("clientDataJSON")
42 if not isinstance(response_client_data_json, str):
43 raise InvalidJSONStructure("Credential response missing required clientDataJSON")
44
45 response_authenticator_data = cred_response.get("authenticatorData")
46 if not isinstance(response_authenticator_data, str):
47 raise InvalidJSONStructure("Credential response missing required authenticatorData")
48
49 response_signature = cred_response.get("signature")
50 if not isinstance(response_signature, str):
51 raise InvalidJSONStructure("Credential response missing required signature")
52
53 cred_type = json_val.get("type")
54 try:
55 # Simply try to get the single matching Enum. We'll set the literal value below assuming
56 # the code can get past here (this is basically a mypy optimization)
57 PublicKeyCredentialType(cred_type)
58 except ValueError as cred_type_exc:
59 raise InvalidJSONStructure("Credential had unexpected type") from cred_type_exc
60
61 response_user_handle = cred_response.get("userHandle")
62 if isinstance(response_user_handle, str):
63 # The `userHandle` string will most likely be base64url-encoded for ease of JSON
64 # transmission as per the L3 Draft spec:
65 # https://w3c.github.io/webauthn/#dictdef-authenticatorassertionresponsejson
66 response_user_handle = base64url_to_bytes(response_user_handle)
67 elif response_user_handle is not None:
68 # If it's not a string, and it's not None, then it's definitely not valid
69 raise InvalidJSONStructure("Credential response had unexpected userHandle")
70
71 cred_authenticator_attachment = json_val.get("authenticatorAttachment")
72 if isinstance(cred_authenticator_attachment, str):
73 try:
74 cred_authenticator_attachment = AuthenticatorAttachment(cred_authenticator_attachment)
75 except ValueError as cred_attachment_exc:
76 raise InvalidJSONStructure(
77 "Credential had unexpected authenticatorAttachment"
78 ) from cred_attachment_exc
79 else:
80 cred_authenticator_attachment = None
81
82 try:
83 authentication_credential = AuthenticationCredential(
84 id=cred_id,
85 raw_id=base64url_to_bytes(cred_raw_id),
86 response=AuthenticatorAssertionResponse(
87 client_data_json=base64url_to_bytes(response_client_data_json),
88 authenticator_data=base64url_to_bytes(response_authenticator_data),
89 signature=base64url_to_bytes(response_signature),
90 user_handle=response_user_handle,
91 ),
92 authenticator_attachment=cred_authenticator_attachment,
93 type=PublicKeyCredentialType.PUBLIC_KEY,
94 )
95 except Exception as exc:
96 raise InvalidAuthenticationResponse(
97 "Could not parse authentication credential from JSON data"
98 ) from exc
99
100 return authentication_credential