python3-openid

3.2.0 · active · verified Thu Apr 09

python3-openid provides OpenID 1.x and 2.0 support for modern Python 3 servers and consumers. The current version is 3.2.0. The release cadence is slow, focusing on compatibility and maintenance rather than active feature development for new OpenID versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initiating an OpenID consumer authentication flow. It initializes a Consumer object with a file-based store (not for production) and attempts to begin an authentication request for a placeholder OpenID URL. In a real web application, the `auth_request.redirectURL` would be used to redirect the user's browser to the OpenID Provider.

import os
from openid.consumer import consumer
from openid.store import filestore

# Create a file store for nonces and associations (NOT suitable for production)
store_path = "openid_store"
os.makedirs(store_path, exist_ok=True)
store = filestore.FileOpenIDStore(store_path)

# Initialize the consumer
oid_consumer = consumer.Consumer(store)

# Example: Begin OpenID authentication (conceptual example for a web app flow)
# In a real web application, this would involve user input and HTTP redirects.
user_openid_url = "https://openid.example.com/user/alice" # Replace with an actual OpenID Provider URL

try:
    # This call prepares an authentication request.
    # The actual redirect to the OpenID Provider (OP) happens in a web framework.
    auth_request = oid_consumer.begin(user_openid_url)
    
    # Simulate the redirect URL generation (in a real app, this would be returned to the client)
    return_to_url = 'http://localhost:8000/verify'
    trust_root = 'http://localhost:8000'
    
    print(f"OpenID authentication initiated for {user_openid_url}")
    print(f"User should be redirected to: {auth_request.redirectURL(return_to_url, trust_root)}")
    
except consumer.DiscoveryFailure as e:
    print(f"OpenID discovery failed for {user_openid_url}: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

# For demonstration purposes, you might clean up the store directory
# import shutil
# shutil.rmtree(store_path)

view raw JSON →