python3-openid
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
- breaking The `python-openid` library (for Python 2) is distinct from `python3-openid` (for Python 3). While the internal `openid` package namespace is largely consistent, direct dependencies or custom integrations built for Python 2's `python-openid` will require careful migration.
- gotcha The default `openid.store.filestore.FileOpenIDStore` is not suitable for production environments. It lacks support for concurrency, distributed systems, and often requires specific file permissions, making it unreliable for web applications.
- gotcha The OpenID 1.x/2.0 protocol, which `python3-openid` implements, is an older standard. For new authentication needs, consider modern alternatives like OpenID Connect (OIDC), which is built on OAuth 2.0 and offers more features, better security practices, and broader adoption.
Install
-
pip install python3-openid
Imports
- Consumer
from openid.consumer import consumer
- Server
from openid.server import server
- FileOpenIDStore
from openid.store import filestore
Quickstart
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)