Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/registration/generate_registration_options.py

raw symbols SCIP
1 from typing import List, Optional
2
3 from ...webauthn.helpers import generate_challenge, generate_user_handle, byteslike_to_bytes
4 from ...webauthn.helpers.cose import COSEAlgorithmIdentifier
5 from ...webauthn.helpers.structs import (
6 AttestationConveyancePreference,
7 AuthenticatorSelectionCriteria,
8 PublicKeyCredentialCreationOptions,
9 PublicKeyCredentialDescriptor,
10 PublicKeyCredentialParameters,
11 PublicKeyCredentialRpEntity,
12 PublicKeyCredentialUserEntity,
13 ResidentKeyRequirement,
14 )
15
16
17 def _generate_pub_key_cred_params(
18 supported_algs: List[COSEAlgorithmIdentifier],
19 ) -> List[PublicKeyCredentialParameters]:
20 """
21 Take an array of algorithm ID ints and return an array of PublicKeyCredentialParameters
22 """
23 return [PublicKeyCredentialParameters(type="public-key", alg=alg) for alg in supported_algs]
24
25
26 default_supported_pub_key_algs = [
27 COSEAlgorithmIdentifier.ECDSA_SHA_256,
28 COSEAlgorithmIdentifier.EDDSA,
29 COSEAlgorithmIdentifier.ECDSA_SHA_512,
30 COSEAlgorithmIdentifier.RSASSA_PSS_SHA_256,
31 COSEAlgorithmIdentifier.RSASSA_PSS_SHA_384,
32 COSEAlgorithmIdentifier.RSASSA_PSS_SHA_512,
33 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256,
34 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_384,
35 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_512,
36 ]
37 default_supported_pub_key_params = _generate_pub_key_cred_params(
38 default_supported_pub_key_algs,
39 )
40
41
42 def generate_registration_options(
43 *,
44 rp_id: str,
45 rp_name: str,
46 user_name: str,
47 user_id: Optional[bytes] = None,
48 user_display_name: Optional[str] = None,
49 challenge: Optional[bytes] = None,
50 timeout: int = 60000,
51 attestation: AttestationConveyancePreference = AttestationConveyancePreference.NONE,
52 authenticator_selection: Optional[AuthenticatorSelectionCriteria] = None,
53 exclude_credentials: Optional[List[PublicKeyCredentialDescriptor]] = None,
54 supported_pub_key_algs: Optional[List[COSEAlgorithmIdentifier]] = None,
55 ) -> PublicKeyCredentialCreationOptions:
56 """Generate options for registering a credential via navigator.credentials.create()
57
58 Args:
59 `rp_id`: A unique, constant identifier for this Relying Party.
60 `rp_name`: A user-friendly, readable name for the Relying Party.
61 `user_name`: A value that will help the user identify which account this credential is associated with. Can be an email address, etc...
62 (optional) `user_id`: A collection of random bytes that identify a user account. For privacy reasons it should NOT be something like an email address. Defaults to 64 random bytes.
63 (optional) `user_display_name`: A user-friendly representation of their account. Can be a full name ,etc... Defaults to the value of `user_name`.
64 (optional) `challenge`: A byte sequence for the authenticator to return back in its response. Defaults to 64 random bytes.
65 (optional) `timeout`: How long in milliseconds the browser should give the user to choose an authenticator. This value is a *hint* and may be ignored by the browser.
66 (optional) `attestation`: The level of attestation to be provided by the authenticator.
67 (optional) `authenticator_selection`: Require certain characteristics about an authenticator, like attachment, support for resident keys, user verification, etc...
68 (optional) `exclude_credentials`: A list of credentials the user has previously registered so that they cannot re-register them.
69 (optional) `supported_pub_key_algs`: A list of public key algorithm IDs the RP chooses to restrict support to. Defaults to all supported algorithm IDs.
70
71 Returns:
72 Registration options ready for the browser. Consider using `helpers.options_to_json()` in this library to quickly convert the options to JSON.
73 """
74
75 if not rp_id:
76 raise ValueError("rp_id cannot be an empty string")
77
78 if not rp_name:
79 raise ValueError("rp_name cannot be an empty string")
80
81 if not user_name:
82 raise ValueError("user_name cannot be an empty string")
83
84 if user_id:
85 if not isinstance(user_id, bytes):
86 raise ValueError("user_id must be bytes")
87 else:
88 user_id = generate_user_handle()
89
90 ########
91 # Set defaults for required values
92 ########
93
94 if not user_display_name:
95 user_display_name = user_name
96
97 pub_key_cred_params = default_supported_pub_key_params
98 if supported_pub_key_algs:
99 pub_key_cred_params = _generate_pub_key_cred_params(supported_pub_key_algs)
100
101 if not challenge:
102 challenge = generate_challenge()
103
104 if not exclude_credentials:
105 exclude_credentials = []
106
107 ########
108 # Generate the actual options
109 ########
110
111 options = PublicKeyCredentialCreationOptions(
112 rp=PublicKeyCredentialRpEntity(
113 name=rp_name,
114 id=rp_id,
115 ),
116 user=PublicKeyCredentialUserEntity(
117 id=user_id,
118 name=user_name,
119 display_name=user_display_name,
120 ),
121 challenge=challenge,
122 pub_key_cred_params=pub_key_cred_params,
123 timeout=timeout,
124 exclude_credentials=exclude_credentials,
125 attestation=attestation,
126 )
127
128 ########
129 # Set optional values if specified
130 ########
131
132 if authenticator_selection is not None:
133 # "Relying Parties SHOULD set [requireResidentKey] to true if, and only if,
134 # residentKey is set to "required""
135 #
136 # See https://www.w3.org/TR/webauthn-2/#dom-authenticatorselectioncriteria-requireresidentkey
137 if authenticator_selection.resident_key == ResidentKeyRequirement.REQUIRED:
138 authenticator_selection.require_resident_key = True
139 options.authenticator_selection = authenticator_selection
140
141 return options