OpenID Connect Provider (OP) library for Python

3.4.2 · active · verified Fri Apr 17

pyop is an OpenID Connect Provider (OP) library in Python, enabling applications to act as identity providers. It is actively maintained with a regular release cadence, adding new features, improving compatibility, and addressing bug fixes. The current version is 3.4.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a basic pyop OpenID Connect Provider (OP) server using an in-memory dictionary storage. It sets up essential configuration like the issuer, JWKS URI, supported response types, and client information. For production environments, you would replace `DictStorage` with a persistent storage solution (e.g., `MongoStorage`, `RedisStorage`) and integrate the `Server` instance with your web framework (e.g., Flask, Django) to handle incoming OIDC requests at appropriate endpoints.

import os
from pyop.server import Server
from pyop.storage import DictStorage

def create_op_server():
    # In a real application, configuration would be loaded from a file or environment
    OP_BASE_URL = os.environ.get('OP_BASE_URL', 'http://localhost:8090')
    JWKS_URI = f'{OP_BASE_URL}/jwks.json'

    # Client information (for registered clients)
    # In a real scenario, this would come from a client registration process/database
    CLIENTS = {
        'test_client': {
            'client_id': 'test_client',
            'client_secret': 'test_secret',
            'redirect_uris': ['http://localhost:8000/cb'],
            'response_types': ['code', 'id_token', 'code id_token'],
            'scope': ['openid', 'profile', 'email'],
            'subject_type': 'pairwise'
        }
    }

    # In-memory storage for demonstration purposes
    # For production, use MongoStorage, RedisStorage, or a custom persistent storage
    storage = DictStorage()
    storage.store_clients(CLIENTS)

    # Minimal server configuration
    server_config = {
        'issuer': OP_BASE_URL,
        'jwks_uri': JWKS_URI,
        'authentication_methods': ['client_secret_basic'],
        'response_types_supported': ['code', 'id_token', 'code id_token'],
        'subject_types_supported': ['pairwise'],
        'scopes_supported': ['openid', 'profile', 'email'],
        'claims_supported': ['sub', 'name', 'email', 'given_name', 'family_name']
    }

    op_server = Server(server_config, storage)
    print(f"OpenID Connect Provider Server initialized with issuer: {op_server.configuration.issuer}")
    return op_server

if __name__ == '__main__':
    # Example usage: this only initializes the server, does not run a web server.
    # A production app would integrate this into Flask, Django, FastAPI, etc.
    # Example: op_server.handle_authentication_request(request_params, session_id)
    op = create_op_server()
    # You would then integrate 'op' with your web framework to handle OIDC endpoints.

view raw JSON →