py_webauthn

repository·master·Indexed 22 days ago

https://github.com/duo-labs/py_webauthn

A Python 3 implementation of the server-side WebAuthn API for implementing FIDO2-compliant authentication, including security keys, biometrics, and Windows Hello. It provides core methods for generating and verifying registration and authentication options, as well as helpers for Base64URL encoding and JSON serialization for data transmission between the server and browser.

Tokens
1.7K
Snippets
8
Records
11
Agent score
27%

What's inside py_webauthn

  1. How py_webauthn handles data transmission

    master

    When implementing a Relying Party (RP) with this library, follow these data handling patterns:

    1. Server to Webpage: Use JSON to transmit registration and authentication options. Because bytes cannot be transmitted directly in JSON, use options_to_json() to encode bytes values into base64url format. This ensures compatibility with navigator.credentials.create() and navigator.credentials.get().
    2. Webpage to Server: Use JSON to transmit WebAuthn responses from the browser. The library can automatically parse these using parse_registration_credential_json and parse_authentication_credential_json (which handle base64url encoded ArrayBuffer values).
    3. Server-side Storage: Arguments defined as bytes in the library's methods are intended to be stored entirely on the server. You can store these as raw bytes without extra encoding/decoding unless your specific storage implementation requires it.
  2. Run Registration and Authentication examples

    master

    Practical usage examples for registration and authentication are provided in the repository. To run them, you must first set up your development environment using uv.

    Run Registration Example:

    venv $> uv run -m examples.registration

    Run Authentication Example:

    venv $> uv run -m examples.authentication
    venv $> uv run -m examples.registration
    
    venv $> uv run -m examples.authentication
  3. Core API methods in the webauthn module

    master

    The webauthn module provides the primary entry points for managing the WebAuthn lifecycle on the server side:

    • generate_registration_options(): Generates options for a new registration ceremony.
    • verify_registration_response(): Verifies a registration response received from a client.
    • generate_authentication_options(): Generates options for an authentication ceremony.
    • verify_authentication_response(): Verifies an authentication response received from a client.
  4. Helper methods and data structures

    master

    The library provides helpers for data transformation and type safety:

    Helper Methods:

    • options_to_json(): Encodes registration/authentication options to JSON, handling bytes by encoding them to base64url for safe transmission to the browser.
    • base64url_to_bytes(): Decodes base64url encoded strings back into bytes.

    Data Structures:

    • webauthn.helpers.structs: Contains dataclasses used for constructing inputs to core methods and providing type hinting to ensure consistent data shapes.
  5. Convert Base64URL to bytes

    master

    Use base64url_to_bytes to decode Base64URL encoded strings into their original byte representations. This is useful for processing data sent from web clients that use Base64URL encoding for binary data.

    from webauthn import base64url_to_bytes
    
    raw_bytes = base64url_to_bytes("SGVsbG8gd29ybGQ")
  6. Verify registration response

    master

    Use verify_registration_response to validate the credential response received from a client after a successful registration ceremony. This ensures the signature, challenge, and origin are valid according to WebAuthn standards.

    from webauthn import verify_registration_response
    
    # Example usage (parameters depend on implementation)
    verification = verify_registration_response(
        credential="credential_data",
        expected_challenge="challenge_bytes",
        expected_origin="https://example.com",
        expected_rp_id="example.com",
    )
  7. Generate authentication options

    master

    Use generate_authentication_options to create the challenge and configuration required for a WebAuthn authentication (login) ceremony. This is the first step in the authentication workflow, used to prompt an existing user to sign in with a previously registered credential.

    from webauthn import generate_authentication_options
    
    # Example usage (parameters depend on implementation)
    options = generate_authentication_options(
        rp_id="example.com",
        allow_credentials=[{"type": "public-key", "id": b"credential_id"}],
    )
  8. Verify authentication response

    master

    Use verify_authentication_response to validate the assertion response received from a client during an authentication ceremony. This confirms that the user possesses the private key associated with the registered credential.

    from webauthn import verify_authentication_response
    
    # Example usage (parameters depend on implementation)
    verification = verify_authentication_response(
        credential="credential_data",
        expected_challenge="challenge_bytes",
        expected_origin="https://example.com",
        expected_rp_id="example.com",
    )
  9. Generate registration options

    master

    Use generate_registration_options to create the challenge and configuration required for a WebAuthn registration ceremony. This is the first step in the registration workflow, typically called on the server to prepare the client for a new credential request.

    from webauthn import generate_registration_options
    
    # Example usage (parameters depend on implementation)
    options = generate_registration_options(
        rp_id="example.com",
        user={
            "id": b"user_id",
            "name": "user@example.com",
            "display_name": "User Name",
        },
    )