from webauthn import ( generate_registration_options, verify_registration_response, options_to_json, base64url_to_bytes, ) from webauthn.helpers.cose import COSEAlgorithmIdentifier from webauthn.helpers.structs import ( AttestationConveyancePreference, AuthenticatorAttachment, AuthenticatorSelectionCriteria, PublicKeyCredentialDescriptor, PublicKeyCredentialHint, ResidentKeyRequirement, ) ################ # # Examples of using webauthn for registration ceremonies # ################ # Simple Options simple_registration_options = generate_registration_options( rp_id="example.com", rp_name="Example Co", user_name="bob", ) print("\n[Registration Options - Simple]") print(options_to_json(simple_registration_options)) # Complex Options complex_registration_options = generate_registration_options( rp_id="example.com", rp_name="Example Co", user_id=bytes([1, 2, 3, 4]), user_name="lee", user_display_name="Lee", attestation=AttestationConveyancePreference.DIRECT, authenticator_selection=AuthenticatorSelectionCriteria( authenticator_attachment=AuthenticatorAttachment.PLATFORM, resident_key=ResidentKeyRequirement.REQUIRED, ), challenge=bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), exclude_credentials=[ PublicKeyCredentialDescriptor(id=b"1234567890"), ], supported_pub_key_algs=[COSEAlgorithmIdentifier.ECDSA_SHA_512], timeout=12000, hints=[PublicKeyCredentialHint.CLIENT_DEVICE], ) print("\n[Registration Options - Complex]") print(options_to_json(complex_registration_options)) # Registration Response Verification registration_verification = verify_registration_response( # Demonstrating the ability to handle a plain dict version of the WebAuthn response credential={ "id": "ZoIKP1JQvKdrYj1bTUPJ2eTUsbLeFkv-X5xJQNr4k6s", "rawId": "ZoIKP1JQvKdrYj1bTUPJ2eTUsbLeFkv-X5xJQNr4k6s", "response": { "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YVkBZ0mWDeWIDoxodDQXD2R2YFuP5K65ooYyx5lc87qDHZdjRQAAAAAAAAAAAAAAAAAAAAAAAAAAACBmggo_UlC8p2tiPVtNQ8nZ5NSxst4WS_5fnElA2viTq6QBAwM5AQAgWQEA31dtHqc70D_h7XHQ6V_nBs3Tscu91kBL7FOw56_VFiaKYRH6Z4KLr4J0S12hFJ_3fBxpKfxyMfK66ZMeAVbOl_wemY4S5Xs4yHSWy21Xm_dgWhLJjZ9R1tjfV49kDPHB_ssdvP7wo3_NmoUPYMgK-edgZ_ehttp_I6hUUCnVaTvn_m76b2j9yEPReSwl-wlGsabYG6INUhTuhSOqG-UpVVQdNJVV7GmIPHCA2cQpJBDZBohT4MBGme_feUgm4sgqVCWzKk6CzIKIz5AIVnspLbu05SulAVnSTB3NxTwCLNJR_9v9oSkvphiNbmQBVQH1tV_psyi9HM1Jtj9VJVKMeyFDAQAB", "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiQ2VUV29nbWcwY2NodWlZdUZydjhEWFhkTVpTSVFSVlpKT2dhX3hheVZWRWNCajBDdzN5NzN5aEQ0RmtHU2UtUnJQNmhQSkpBSW0zTFZpZW40aFhFTGciLCJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjUwMDAiLCJjcm9zc09yaWdpbiI6ZmFsc2V9", "transports": ["internal"], }, "type": "public-key", "clientExtensionResults": {}, "authenticatorAttachment": "platform", }, expected_challenge=base64url_to_bytes( "CeTWogmg0cchuiYuFrv8DXXdMZSIQRVZJOga_xayVVEcBj0Cw3y73yhD4FkGSe-RrP6hPJJAIm3LVien4hXELg" ), expected_origin="http://localhost:5000", expected_rp_id="localhost", require_user_verification=True, ) print("\n[Registration Verification - None]") print(registration_verification) assert registration_verification.credential_id == base64url_to_bytes( "ZoIKP1JQvKdrYj1bTUPJ2eTUsbLeFkv-X5xJQNr4k6s" )