|
1 | 1 | from unittest import TestCase
|
2 | 2 |
|
3 |
| -from webauthn.helpers import base64url_to_bytes |
| 3 | +from webauthn.helpers import base64url_to_bytes, options_to_json |
4 | 4 | from webauthn.helpers.exceptions import InvalidJSONStructure
|
5 | 5 | from webauthn.helpers.structs import (
|
6 | 6 | AuthenticatorTransport,
|
|
16 | 16 | )
|
17 | 17 | from webauthn.helpers.cose import COSEAlgorithmIdentifier
|
18 | 18 | from webauthn.helpers.parse_registration_options_json import parse_registration_options_json
|
| 19 | +from webauthn.registration.generate_registration_options import generate_registration_options |
19 | 20 |
|
20 | 21 |
|
21 | 22 | class TestParseRegistrationOptionsJSON(TestCase):
|
@@ -221,6 +222,49 @@ def test_supports_json_string(self) -> None:
|
221 | 222 | )
|
222 | 223 | self.assertEqual(parsed.timeout, 60000)
|
223 | 224 |
|
| 225 | + def test_supports_options_to_json_output(self) -> None: |
| 226 | + """ |
| 227 | + Test that output from `generate_registration_options()` that's fed directly into |
| 228 | + `options_to_json()` gets parsed back into the original options without any changes along |
| 229 | + the way. |
| 230 | + """ |
| 231 | + opts = generate_registration_options( |
| 232 | + rp_id="example.com", |
| 233 | + rp_name="Example Co", |
| 234 | + user_id=bytes([1, 2, 3, 4]), |
| 235 | + user_name="lee", |
| 236 | + user_display_name="Lee", |
| 237 | + attestation=AttestationConveyancePreference.DIRECT, |
| 238 | + authenticator_selection=AuthenticatorSelectionCriteria( |
| 239 | + authenticator_attachment=AuthenticatorAttachment.PLATFORM, |
| 240 | + resident_key=ResidentKeyRequirement.REQUIRED, |
| 241 | + require_resident_key=True, |
| 242 | + user_verification=UserVerificationRequirement.DISCOURAGED, |
| 243 | + ), |
| 244 | + challenge=bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), |
| 245 | + exclude_credentials=[ |
| 246 | + PublicKeyCredentialDescriptor( |
| 247 | + id=b"1234567890", |
| 248 | + transports=[AuthenticatorTransport.INTERNAL, AuthenticatorTransport.HYBRID], |
| 249 | + ), |
| 250 | + ], |
| 251 | + supported_pub_key_algs=[COSEAlgorithmIdentifier.ECDSA_SHA_512], |
| 252 | + timeout=12000, |
| 253 | + ) |
| 254 | + |
| 255 | + opts_json = options_to_json(opts) |
| 256 | + |
| 257 | + parsed_opts_json = parse_registration_options_json(opts_json) |
| 258 | + |
| 259 | + self.assertEqual(parsed_opts_json.rp, opts.rp) |
| 260 | + self.assertEqual(parsed_opts_json.user, opts.user) |
| 261 | + self.assertEqual(parsed_opts_json.attestation, opts.attestation) |
| 262 | + self.assertEqual(parsed_opts_json.authenticator_selection, opts.authenticator_selection) |
| 263 | + self.assertEqual(parsed_opts_json.challenge, opts.challenge) |
| 264 | + self.assertEqual(parsed_opts_json.exclude_credentials, opts.exclude_credentials) |
| 265 | + self.assertEqual(parsed_opts_json.pub_key_cred_params, opts.pub_key_cred_params) |
| 266 | + self.assertEqual(parsed_opts_json.timeout, opts.timeout) |
| 267 | + |
224 | 268 | def test_raises_on_non_dict_json(self) -> None:
|
225 | 269 | with self.assertRaisesRegex(InvalidJSONStructure, "not a JSON object"):
|
226 | 270 | parse_registration_options_json("[0]")
|
|
0 commit comments