WebAuthn Python Library

2.7.1 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to generate registration options, which is the first step in registering a new WebAuthn credential. It uses placeholder values for RP (Relying Party) and user details. In a real application, these would be dynamic and securely managed. The generated options are then sent to the client-side JavaScript for interaction with the user's authenticator.

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.

view raw JSON →