Skip to main content

Official OAuth42 SDK for Python applications

Project description

OAuth42 Python SDK

Official Python SDK for OAuth42 - Enterprise OAuth2/OIDC Authentication Provider

Features

  • 🔐 Complete OAuth2/OIDC client implementation
  • ⚡ Async/await support with httpx
  • 🛡️ PKCE (Proof Key for Code Exchange) support
  • 🔄 Automatic token refresh
  • 🎯 Type-safe with Pydantic models
  • 🚀 Framework integrations (FastAPI, Flask)
  • 🐍 Python 3.12+ support

Installation

Install with uv (recommended):

uv add oauth42

Or with pip:

pip install oauth42

Quick Start

Basic Usage

from oauth42 import OAuth42Client

# Initialize client
client = OAuth42Client(
    client_id="your-client-id",
    client_secret="your-client-secret",
    issuer="https://api.oauth42.com",
    redirect_uri="http://localhost:8000/callback"
)

# Create authorization URL
auth_url, state, code_verifier = client.create_authorization_url()

# Exchange authorization code for tokens
tokens = client.exchange_code(
    code=request.args.get("code"),
    state=request.args.get("state"),
    expected_state=state,
    code_verifier=code_verifier
)

# Get user information
user_info = client.get_user_info(tokens.access_token)
print(f"Hello, {user_info.email}!")

Async Support

from oauth42 import OAuth42AsyncClient

async with OAuth42AsyncClient.from_env() as client:
    # Create authorization URL
    auth_url, state, verifier = client.create_authorization_url()
    
    # Exchange code for tokens
    tokens = await client.exchange_code(
        code=code,
        state=state,
        expected_state=expected_state,
        code_verifier=verifier
    )
    
    # Get user info
    user_info = await client.get_user_info(tokens.access_token)

Framework Integration

FastAPI

from fastapi import FastAPI, Depends
from oauth42.middleware.fastapi import OAuth42Middleware, get_current_user
from oauth42 import OAuth42AsyncClient, OAuth42User

app = FastAPI()
client = OAuth42AsyncClient.from_env()
app.add_middleware(OAuth42Middleware, client=client)

@app.get("/protected")
async def protected_route(user: OAuth42User = Depends(get_current_user)):
    return {"message": f"Hello, {user.email}!"}

@app.get("/login")
async def login():
    auth_url, state, verifier = client.create_authorization_url()
    # Store state and verifier in session
    return {"auth_url": auth_url}

@app.get("/callback")
async def callback(code: str, state: str):
    # Retrieve stored state and verifier
    tokens = await client.exchange_code(
        code=code,
        state=state,
        expected_state=stored_state,
        code_verifier=stored_verifier
    )
    return {"access_token": tokens.access_token}

Flask

from flask import Flask, redirect, url_for
from oauth42 import OAuth42Client
from oauth42.middleware.flask import OAuth42Flask

app = Flask(__name__)
client = OAuth42Client.from_env()
oauth42 = OAuth42Flask(app, client)

@app.route("/protected")
@oauth42.require_auth
def protected():
    user = oauth42.get_current_user()
    return f"Hello, {user.email}!"

@app.route("/login")
def login():
    return oauth42.authorize_redirect()

@app.route("/callback")
def callback():
    tokens = oauth42.authorize_access_token()
    return redirect(url_for("protected"))

Configuration

Environment Variables

# Required
OAUTH42_CLIENT_ID=your-client-id
OAUTH42_CLIENT_SECRET=your-client-secret
OAUTH42_ISSUER=https://api.oauth42.com

# Optional
OAUTH42_REDIRECT_URI=http://localhost:8000/callback
OAUTH42_SCOPES=openid profile email
OAUTH42_VERIFY_SSL=true

Programmatic Configuration

from oauth42 import Config, OAuth42Client

config = Config(
    client_id="your-client-id",
    client_secret="your-client-secret",
    issuer="https://api.oauth42.com",
    redirect_uri="http://localhost:8000/callback",
    scopes=["openid", "profile", "email", "company"]
)

client = OAuth42Client(config=config)

Examples

See the examples/ directory for complete working examples:

  • FastAPI Example: Modern async web application with session management
  • Flask Example: Traditional web application with template rendering

Running the Examples

FastAPI Example

cd examples/fastapi_app
uv sync
uv run python main.py

Visit http://localhost:8000

Flask Example

cd examples/flask_app
uv sync
uv run python app.py

Visit http://localhost:5000

Testing

Run unit tests:

uv run pytest tests/unit

Run integration tests (requires OAuth42 backend):

# Start OAuth42 backend first
make hybrid-dev-up
make dev-setup

# Run integration tests
uv run pytest tests/integration -m integration

Development

Setup Development Environment

# Install dependencies
uv sync

# Run tests
uv run pytest

# Format code
uv run black .
uv run ruff check --fix .

# Type checking
uv run mypy oauth42

Project Structure

oauth42/
├── client.py           # Main OAuth42 client implementation
├── auth/              # Authentication flows
├── middleware/        # Framework integrations
│   ├── fastapi.py    # FastAPI middleware
│   └── flask.py      # Flask extension
├── types/            # Type definitions
│   ├── models.py     # Pydantic models
│   └── exceptions.py # Custom exceptions
└── utils/            # Utility functions

License

Copyright (c) 2024 OAuth42, Inc. All rights reserved.

This SDK is proprietary software provided by OAuth42, Inc. for use with OAuth42 services. See LICENSE file for terms and conditions.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oauth42-0.2.1.tar.gz (34.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oauth42-0.2.1-py3-none-any.whl (44.7 kB view details)

Uploaded Python 3

File details

Details for the file oauth42-0.2.1.tar.gz.

File metadata

  • Download URL: oauth42-0.2.1.tar.gz
  • Upload date:
  • Size: 34.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Amazon Linux","version":"2023","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for oauth42-0.2.1.tar.gz
Algorithm Hash digest
SHA256 52d5f9ca9c10752f654fb9c6a296a2b336ae56ab85c29a6c4623959bf8634538
MD5 1779abd305b5e24eb38c5fad73397063
BLAKE2b-256 51cfdecd5d9768d4155a3eb306a1817127f028a5f5c4951c167e3e7a6db5dbbc

See more details on using hashes here.

File details

Details for the file oauth42-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: oauth42-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 44.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Amazon Linux","version":"2023","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for oauth42-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e83dd9dcc8c4ab8014ceeb83fa17c2c57f047d2f7b6d15b7a8904ea297fbc7a
MD5 7defa10bbaa727ea043fb22cb3475936
BLAKE2b-256 7b82c0e2e017229c0b0b749f1e90d17d64eb3b42b79411478caf24cb8a9f4913

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page