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.0.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.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: oatk-0.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 6b659f63e03b9e495f8337b065ab23870df8f0b8678e10a1464e958b16101531
MD5 85f8f0633852423eabc8f6f7359a4ca3
BLAKE2b-256 a0fb99c309f9766897066cd7e95b8e1dde5e2819eb73bb436fc6df90ac547a8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oatk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bb28a24205a3c3536252822ebc5a010777518bc2268b09a88d59229f66f2c2c
MD5 7076a0d91f2fadd3a7c0e658d40eb8b7
BLAKE2b-256 659433b3437f583f00a5fc71540af0662ed0d1d20273dd7f117c7f37a4c8a8e2

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