Python Sample Applications

This document provides complete Python sample applications demonstrating both client-direct and backend-proxied FIDO2 authentication patterns. These examples are useful for developers building Python backends or desktop applications that integrate with the Arculus FIDO2 Server.

Minimal Examples

These minimal examples show the essential REST API calls to register and authenticate a user. Note that Python does not have an SDK, so all operations use direct REST API calls to the FIDO2 server.

Minimal Client-Direct Example

import requests

FIDO_SERVER_URL = "https://fido.example.com"
USERNAME = "[email protected]"
RP_ID = "example.com"

# Registration
def register_user():
    # Phase 1: Get registration options
    begin_response = requests.post(
        f"{FIDO_SERVER_URL}/fidoapi/certify/attestation/options",
        json={"username": USERNAME, "rpId": RP_ID}
    )
    begin_data = begin_response.json()
    challenge = begin_data["challenge"]
    
    # Phase 2: Card operations (performed by mobile app with SDK)
    # Mobile app returns card_response_data after calling registerCardOnly()
    card_response_data = {
        "id": "credential-id",
        "rawId": "base64-raw-id",
        "type": "public-key",
        "response": {
            "authenticatorData": "base64-data",
            "clientDataJSON": "base64-data",
            "signature": "base64-signature"
        }
    }
    
    # Phase 3: Complete registration
    complete_response = requests.post(
        f"{FIDO_SERVER_URL}/fidoapi/certify/attestation/result",
        json=card_response_data,
        cookies=begin_response.cookies
    )
    print("Registration:", complete_response.json())

# Authentication
def authenticate_user():
    # Phase 1: Get authentication options
    begin_response = requests.post(
        f"{FIDO_SERVER_URL}/fidoapi/certify/assertion/options",
        json={"username": USERNAME, "rpId": RP_ID}
    )
    begin_data = begin_response.json()
    
    # Phase 2: Card operations (performed by mobile app with SDK)
    # Mobile app returns card_response_data after calling authenticateCardOnly()
    card_response_data = {
        "id": "credential-id",
        "rawId": "base64-raw-id",
        "type": "public-key",
        "response": {
            "authenticatorData": "base64-data",
            "clientDataJSON": "base64-data",
            "signature": "base64-signature"
        }
    }
    
    # Phase 3: Complete authentication
    complete_response = requests.post(
        f"{FIDO_SERVER_URL}/fidoapi/certify/assertion/result",
        json=card_response_data,
        cookies=begin_response.cookies
    )
    print("Authentication:", complete_response.json())

Minimal Backend-Proxied Example

Complete Sample Applications

The following sections provide complete, production-ready sample applications with full error handling, session management, and proper integration patterns.

Client-Direct Pattern

In the client-direct pattern, the Python application communicates directly with the FIDO2 server. This pattern is suitable for desktop applications or simple backend services.

Basic Client-Direct Example

In the backend-proxied pattern, a backend service handles all FIDO2 server communication, and the client (mobile app or desktop app) only performs card operations. This is the recommended pattern for production deployments.

Backend API Client

Complete Backend-Proxied Authentication Example

Integration with Mobile Apps

When using the backend-proxied pattern, the Python backend coordinates with mobile applications that perform the card operations:

Backend Flow

  1. Phase 1: Backend receives authentication/registration request

  2. Phase 2: Backend sends challenge to mobile app (via API, WebSocket, or push notification)

  3. Phase 3: Mobile app performs card operations using SDK

  4. Phase 4: Mobile app sends card response back to backend

  5. Phase 5: Backend completes authentication/registration with FIDO2 server

Example: Flask Backend Endpoint

Error Handling

Session Management

Complete Example: Flask Application

Last updated