U2F Host Library

0.1.5 · abandoned · verified Thu Apr 16

pyu2f is a Python-based U2F host library for Linux, Windows, and MacOS, providing functionality for interacting with U2F devices over USB. The current version is 0.1.5. The library's support is discontinued as U2F is an outdated FIDO specification, with `python-fido2` being the recommended alternative for FIDO2 and U2F backward compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform a U2F authentication (signing) operation using `pyu2f`. It involves creating a `RegisteredKey` object, preparing challenge data, and then calling the `Authenticate` method on a `CompositeAuthenticator` instance. In a real-world scenario, the `APP_ID`, `ORIGIN`, `CHALLENGE_BASE64`, and `KEY_HANDLE_BASE64` values would be dynamically provided by a U2F relying party server after a registration process. Users may be prompted to physically interact with their U2F device. The library also supports offloading signing to a pluggable command-line tool by setting the `SK_SIGNING_PLUGIN` environment variable.

import os
from pyu2f import model
from pyu2f.convenience import authenticator

# --- Placeholder values for demonstration ---
# In a real application, these would come from a U2F challenge
# issued by a relying party (e.g., a web service).
APP_ID = 'https://example.com'
ORIGIN = 'https://example.com'
CHALLENGE_BASE64 = 'some_base64_challenge_data_from_server'
KEY_HANDLE_BASE64 = 'some_base64_key_handle_from_previous_registration'

print("Attempting U2F authentication...")

try:
    # 1. Prepare registered key and challenge data
    # The RegisteredKey model requires a base64 encoded key handle.
    registered_key = model.RegisteredKey(KEY_HANDLE_BASE64.encode('utf-8'))
    
    # The challenge data is a list of dictionaries, each containing
    # a RegisteredKey object and the raw challenge.
    challenge_data = [{
        'key': registered_key,
        'challenge': CHALLENGE_BASE64.encode('utf-8')
    }]

    # 2. Create the authenticator interface
    api = authenticator.CreateCompositeAuthenticator(ORIGIN)

    # 3. Authenticate with the U2F device
    # This will typically prompt the user to touch their security key.
    response = api.Authenticate(APP_ID, challenge_data)

    if response:
        print("Authentication successful!")
        print(f"Client Data: {response.client_data.decode('utf-8')}")
        print(f"Signature Data: {response.signature_data.decode('utf-8')}")
    else:
        print("Authentication failed or timed out.")

except Exception as e:
    print(f"An error occurred during authentication: {e}")

# Optional: Using a custom authenticator plugin via environment variable
# SK_SIGNING_PLUGIN = '/path/to/your/custom_authenticator_script.py'
# os.environ['SK_SIGNING_PLUGIN'] = SK_SIGNING_PLUGIN
# print(f"Custom authenticator plugin set: {os.environ.get('SK_SIGNING_PLUGIN')}")
# Then call Authenticate again. The plugin script must adhere to
# the specification in customauthenticator.py (refer to source code).

view raw JSON →