WebAuthn Python Library
The `webauthn` library provides a Pythonic implementation for integrating WebAuthn (Web Authentication API) into web applications, enabling strong, phishing-resistant, and passwordless authentication. It handles the server-side verification and generation of WebAuthn credentials, abstracting away the complexities of the FIDO2 protocol. The library is actively maintained with a relatively frequent release cadence, with the current version being 2.7.1, and supports Python 3.9 and newer.
Warnings
- breaking The minimum supported Python version was bumped to Python 3.9. Users on older Python versions (3.8 or below) will need to upgrade their Python environment to use `webauthn>=2.3.0`.
- gotcha Support for ML-DSA (Post-Quantum Cryptography) public keys for authenticators was introduced, but it requires an optional dependency (`dilithium-py`). If you intend to support PQC-enabled authenticators, this dependency must be explicitly installed.
- gotcha The `webauthn.helpers.options_to_json_dict` helper introduced a `bytes_encoder` argument. If not specified, `bytes` values are Base64URL encoded by default. Custom encoding logic might be required for specific client-side interoperability or if your application expects a different serialization format for byte arrays.
- gotcha Type annotations for bare `dict`s were replaced with `Dict[str, Any]` for stricter type checking. While this doesn't break runtime behavior, it might cause issues with type checkers (e.g., MyPy) in projects with strict configurations.
Install
-
pip install webauthn
Imports
- generate_registration_options
from webauthn import generate_registration_options
- verify_registration_response
from webauthn import verify_registration_response
- generate_authentication_options
from webauthn import generate_authentication_options
- verify_authentication_response
from webauthn import verify_authentication_response
- options_to_json_dict
from webauthn.helpers import options_to_json_dict
Quickstart
import os
from webauthn import generate_registration_options
from webauthn.helpers.structs import PublicKeyCredentialUserEntity
# Placeholder values (in a real app, these would come from your user management)
RP_ID = "localhost" # Or your domain, e.g., "example.com"
RP_NAME = "My Awesome App"
USER_ID = os.environ.get('WEBAUTHN_USER_ID', 'test_user_id').encode('utf-8')
USER_NAME = os.environ.get('WEBAUTHN_USER_NAME', 'testuser')
USER_DISPLAY_NAME = os.environ.get('WEBAAUTHN_USER_DISPLAY_NAME', 'Test User')
user_entity = PublicKeyCredentialUserEntity(
id=USER_ID,
name=USER_NAME,
display_name=USER_DISPLAY_NAME,
)
registration_options = generate_registration_options(
rp_id=RP_ID,
rp_name=RP_NAME,
user_entity=user_entity,
challenge=os.urandom(16) # A new random challenge for each registration attempt
)
print("Generated WebAuthn Registration Options:")
print(registration_options)
# In a real application, you would serialize these options (e.g., to JSON)
# and send them to the client-side JavaScript for WebAuthn API calls.