Debian Codegraph @

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

raw
1 import json
2 from typing import Union, Dict, Any
3
4 from .structs import (
5 PublicKeyCredentialCreationOptions,
6 PublicKeyCredentialRequestOptions,
7 )
8 from .bytes_to_base64url import bytes_to_base64url
9
10
11 def options_to_json(
12 options: Union[
13 PublicKeyCredentialCreationOptions,
14 PublicKeyCredentialRequestOptions,
15 ]
16 ) -> str:
17 """
18 Prepare options for transmission to the front end as JSON
19 """
20 if isinstance(options, PublicKeyCredentialCreationOptions):
21 _rp = {"name": options.rp.name}
22 if options.rp.id:
23 _rp["id"] = options.rp.id
24
25 _user: Dict[str, Any] = {
26 "id": bytes_to_base64url(options.user.id),
27 "name": options.user.name,
28 "displayName": options.user.display_name,
29 }
30
31 reg_to_return: Dict[str, Any] = {
32 "rp": _rp,
33 "user": _user,
34 "challenge": bytes_to_base64url(options.challenge),
35 "pubKeyCredParams": [
36 {"type": param.type, "alg": param.alg} for param in options.pub_key_cred_params
37 ],
38 }
39
40 # Begin handling optional values
41
42 if options.timeout is not None:
43 reg_to_return["timeout"] = options.timeout
44
45 if options.exclude_credentials is not None:
46 _excluded = options.exclude_credentials
47 json_excluded = []
48
49 for cred in _excluded:
50 json_excluded_cred: Dict[str, Any] = {
51 "id": bytes_to_base64url(cred.id),
52 "type": cred.type.value,
53 }
54
55 if cred.transports:
56 json_excluded_cred["transports"] = [
57 transport.value for transport in cred.transports
58 ]
59
60 json_excluded.append(json_excluded_cred)
61
62 reg_to_return["excludeCredentials"] = json_excluded
63
64 if options.authenticator_selection is not None:
65 _selection = options.authenticator_selection
66 json_selection: Dict[str, Any] = {}
67
68 if _selection.authenticator_attachment is not None:
69 json_selection[
70 "authenticatorAttachment"
71 ] = _selection.authenticator_attachment.value
72
73 if _selection.resident_key is not None:
74 json_selection["residentKey"] = _selection.resident_key.value
75
76 if _selection.require_resident_key is not None:
77 json_selection["requireResidentKey"] = _selection.require_resident_key
78
79 if _selection.user_verification is not None:
80 json_selection["userVerification"] = _selection.user_verification.value
81
82 reg_to_return["authenticatorSelection"] = json_selection
83
84 if options.attestation is not None:
85 reg_to_return["attestation"] = options.attestation.value
86
87 return json.dumps(reg_to_return)
88
89 if isinstance(options, PublicKeyCredentialRequestOptions):
90 auth_to_return: Dict[str, Any] = {"challenge": bytes_to_base64url(options.challenge)}
91
92 if options.timeout is not None:
93 auth_to_return["timeout"] = options.timeout
94
95 if options.rp_id is not None:
96 auth_to_return["rpId"] = options.rp_id
97
98 if options.allow_credentials is not None:
99 _allowed = options.allow_credentials
100 json_allowed = []
101
102 for cred in _allowed:
103 json_allowed_cred: Dict[str, Any] = {
104 "id": bytes_to_base64url(cred.id),
105 "type": cred.type.value,
106 }
107
108 if cred.transports:
109 json_allowed_cred["transports"] = [
110 transport.value for transport in cred.transports
111 ]
112
113 json_allowed.append(json_allowed_cred)
114
115 auth_to_return["allowCredentials"] = json_allowed
116
117 if options.user_verification:
118 auth_to_return["userVerification"] = options.user_verification.value
119
120 return json.dumps(auth_to_return)
121
122 raise TypeError(
123 "Options was not instance of PublicKeyCredentialCreationOptions or PublicKeyCredentialRequestOptions"
124 )