Skip to main content

A collection of useful functions for dealing with OAuth

Project description

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

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

oatk-0.2.1.tar.gz (5.9 MB view details)

Uploaded Source

Built Distribution

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

oatk-0.2.1-py3-none-any.whl (35.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: oatk-0.2.1.tar.gz
  • Upload date:
  • Size: 5.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for oatk-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ec9cace10e48883ea76b4d1d6216ff47a1dea66d734c53b527bb3f4f76ebce1b
MD5 7fc7a10986dab6b69dcf691dab6200f8
BLAKE2b-256 4640c81803ac4157ce0ea501ad7c4b57e3c23aff1bdb15fd2070f5a4f44ebb47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oatk-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 35.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for oatk-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 555e5e7e1a6bbddf7be5aef32f4f2baa6841bf09778e59ea99b535af3796eaf3
MD5 71752951fe3a1d1b2fd3650290226795
BLAKE2b-256 8c4b9c00b1dc3c0d210b4f13150a2141f25a3c8e1b9341d3cd86a3cf1c9a7c38

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