Skip to main content

oatk - OAuth Toolkit

Latest Version on PyPI PyPI downloads Supported Implementations License: MIT Tests Code style: ruff

A clean, simple Python OAuth toolkit for quick prototypes and learning. Provides both synchronous and asynchronous implementations with framework integrations for Flask, Quart, and FastAPI.

Quick Start

Installation

pip install oatk

For async support:

pip install oatk[async]

For framework integrations:

pip install oatk[quart]      # Quart (async Flask)
pip install oatk[fastapi]    # FastAPI

Basic Usage (Synchronous)

from oatk import OAuthToolkit

# Create and validate tokens
toolkit = OAuthToolkit()
toolkit.with_private("private_key.pem")
toolkit.with_public("public_key.pem")

# Create a token
toolkit.claims(user="alice", role="admin")
token = toolkit.token

# Validate a token
claims = toolkit.validate(token)
print(claims)  # {'user': 'alice', 'role': 'admin'}

Basic Usage (Asynchronous)

from oatk import AsyncOAuthToolkit

async def main():
    toolkit = AsyncOAuthToolkit()

    # Configure from OAuth provider
    await toolkit.using_provider(
        "https://accounts.google.com/.well-known/openid-configuration"
    )

    # Validate token
    claims = await toolkit.validate(token)
    print(claims)

# Or use with async context
async with AsyncOAuthToolkit() as toolkit:
    await toolkit.with_public("public_key.pem")
    claims = await toolkit.validate(token)

Flask Integration

from flask import Flask
from oatk import OAuthToolkit

app = Flask(__name__)
toolkit = OAuthToolkit()
toolkit.with_jwks("certs.json")

@app.route("/protected")
@toolkit.authenticated
def protected():
    return {"message": "authenticated"}

@app.route("/admin")
@toolkit.authenticated_with_claims(role="admin")
def admin():
    return {"message": "admin only"}

FastAPI Integration

from fastapi import FastAPI, Depends
from oatk.async_toolkit import AsyncOAuthToolkit
from oatk.fastapi import OAuthToolkitDependency

app = FastAPI()
toolkit = AsyncOAuthToolkit()
oauth = OAuthToolkitDependency(toolkit)

@app.get("/protected")
async def protected(user = Depends(oauth.get_current_user)):
    return {"user_id": user["sub"]}

@app.get("/admin")
async def admin(user = Depends(oauth.require_claims(role="admin"))):
    return {"message": "admin access"}

Features

Core Features

  • Token creation with RSA private keys
  • Token validation with RSA public keys
  • JWKS import/export for key distribution
  • Provider-based configuration (OpenID Connect)
  • Token decoding (without validation)

Synchronous API (OAuthToolkit)

  • Flask route decorators (@authenticated, @authenticated_with_claims)
  • Command-line interface for quick operations
  • Clipboard support (macOS)

Asynchronous API (AsyncOAuthToolkit)

  • Async HTTP operations with httpx
  • Async file I/O with anyio
  • Framework-agnostic decorators
  • Context-based token management

Framework Integrations

  • Flask (sync): Route decorators for authentication
  • Quart (async): Automatic token extraction from requests
  • FastAPI (async): Dependency injection pattern

Command Line Interface

Generate a JWKS from a public key:

oatk with_public public_key.pem jwks

Create a token:

oatk with_private private_key.pem with_jwks certs.json claims '{"user":"alice"}' token

Validate a token:

oatk with_jwks certs.json from_file token.txt validate

Decode a token (without validation):

oatk from_file token.txt decode

Running Examples

The repository includes example applications demonstrating framework integrations. Use the Makefile targets to run them:

Quart Example (Async Flask)

make quart-example

This runs the Quart async OAuth example with auto-reload enabled. See examples/quart_example.py.

FastAPI Example

make fastapi-example

This runs the FastAPI async OAuth example with auto-reload enabled. See examples/fastapi_example.py.

Both examples demonstrate:

  • OAuth toolkit initialization and configuration
  • Token validation in route decorators
  • Role-based access control
  • Claim-based authorization

Documentation

Full documentation is available at: https://oatk.readthedocs.io

Use Cases

oatk is designed for:

  • Quick prototypes: Get OAuth up and running in minutes
  • Learning: Understand OAuth/JWT concepts with clean, readable code
  • Development environments: Test OAuth flows without complex setup
  • Understanding: See what's happening under the hood

For production systems, consider battle-tested alternatives like Authlib, python-jose, or similar.

Security Disclaimer

This software deals with authentication and authorization, which are critical security components. All software can contain bugs, including security vulnerabilities.

By using this software, you acknowledge that:

  • You understand the security implications
  • You take responsibility for any security-related issues
  • This toolkit is designed primarily for prototypes and learning
  • For production systems, consider battle-tested alternatives like Authlib, python-jose, or similar

USE AT YOUR OWN RISK. The authors and contributors are not responsible for any security breaches, data loss, or other damages resulting from the use of this software.

Requirements

  • Python 3.9+
  • RSA key pair (for token signing/validation)

Generate keys:

openssl genrsa -out private_key.pem 2048
openssl rsa -in private_key.pem -outform PEM -pubout -out public_key.pem

License

MIT License - see LICENSE.txt

Links

Metadata

Release files for oatk 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oatk 0.2.1
File Size Uploaded Provenance Actions
oatk-0.2.1.tar.gz 5.9 MB Without provenance Details for oatk-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for oatk 0.2.1
File Interpreter ABI Platform Actions
oatk-0.2.1-py3-none-any.whl Python 3 none any Details for oatk-0.2.1-py3-none-any.whl

Total release size: 6.0 MB

Release files / oatk-0.2.1.tar.gz

Download URL oatk-0.2.1.tar.gz
Size 5.9 MB
Tags Source
SHA-256 checksum
How to use checksums
ec9cace10e48883ea76b4d1d6216ff47a1dea66d734c53b527bb3f4f76ebce1b
BLAKE2b-256 checksum
How to use checksums
4640c81803ac4157ce0ea501ad7c4b57e3c23aff1bdb15fd2070f5a4f44ebb47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / oatk-0.2.1-py3-none-any.whl

Download URL oatk-0.2.1-py3-none-any.whl
Size 35.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
555e5e7e1a6bbddf7be5aef32f4f2baa6841bf09778e59ea99b535af3796eaf3
BLAKE2b-256 checksum
How to use checksums
8c4b9c00b1dc3c0d210b4f13150a2141f25a3c8e1b9341d3cd86a3cf1c9a7c38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for oatk 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page