Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

axioms-flask-py PyPI Pepy Total Downloads

OAuth2/OIDC authentication and authorization for Flask APIs. Supports authentication and claim-based fine-grained authorization (scopes, roles, permissions) using JWT tokens.

Works with access tokens issued by various authorization servers including AWS Cognito, Auth0, Okta, Microsoft Entra, etc.

Using FastAPI or Django REST Framework? This package is specifically for Flask. For FastAPI applications, use axioms-fastapi. For DRF applications, use axioms-drf-py.

GitHub Release GitHub Actions Test Workflow Status PyPI - Version Python Wheels Python Versions GitHub last commit PyPI - Status License PyPI Downloads

Features

  • JWT token validation with automatic JWKS retrieval and refresh
  • Algorithm validation (only secure asymmetric algorithms allowed)
  • Issuer validation to prevent token substitution attacks
  • Middleware for automatic token extraction and validation
  • Authorization decorators: scopes, roles, permissions
  • Object-level permissions for row-level security
  • Custom claim name support for different auth servers
  • Safe methods support (e.g., OPTIONS for CORS)

Installation

pip install axioms-flask-py

Quick Start

1. Configure Flask app:

from flask import Flask
from flask_dotenv import DotEnv
from axioms_flask import init_axioms, setup_token_middleware, register_axioms_error_handler

app = Flask(__name__)
env = DotEnv(app)

init_axioms(app)
setup_token_middleware(app)  # Optional: automatic token validation
register_axioms_error_handler(app)

2. Configure environment (.env):

AXIOMS_AUDIENCE=your-api-audience
AXIOMS_ISS_URL=https://your-auth.domain.com

3. Protect routes:

from axioms_flask.decorators import has_valid_access_token, has_required_permissions

@app.route('/api/protected')
@has_valid_access_token
def protected():
    return {'message': 'Authenticated'}

@app.route('/api/admin')
@has_valid_access_token
@has_required_permissions(['admin:write'])
def admin():
    return {'message': 'Authorized'}

Available Decorators

Decorator Purpose
has_valid_access_token Validates JWT token (signature, expiry, audience, issuer)
has_required_scopes Requires specific scopes in token
has_required_roles Requires specific roles in token
has_required_permissions Requires specific permissions in token
check_object_ownership Validates user owns the resource (row-level security)
require_ownership Simpler ownership check for pre-fetched objects

Authorization Logic

OR Logic (default) - Requires ANY of the specified claims:

@app.route('/api/resource')
@has_valid_access_token
@has_required_scopes(['read', 'write'])  # Needs read OR write
def resource():
    return {'data': 'success'}

AND Logic - Chain decorators to require ALL claims:

@app.route('/api/admin')
@has_valid_access_token
@has_required_scopes(['read'])      # Needs read
@has_required_scopes(['write'])     # AND write
@has_required_roles(['admin'])      # AND admin role
def admin():
    return {'data': 'authorized'}

Examples

Scopes:

@app.route('/api/data')
@has_valid_access_token
@has_required_scopes(['openid', 'profile'])
def get_data():
    return {'data': 'User data'}

Roles:

@app.route('/api/admin/users')
@has_valid_access_token
@has_required_roles(['admin', 'superuser'])
def manage_users():
    return {'users': []}

Permissions:

@app.route('/api/posts', methods=['POST'])
@has_valid_access_token
@has_required_permissions(['posts:create'])
def create_post():
    return {'id': 1, 'message': 'Post created'}

Object Ownership (Row-level security):

from axioms_flask.decorators import check_object_ownership

def get_article(article_id):
    return Article.query.get_or_404(article_id)

@app.route('/articles/<int:article_id>', methods=['PATCH'])
@has_valid_access_token
@check_object_ownership(get_article, inject_as='article')
def update_article(article_id, article):
    # Only owner can update (article.user must match token.sub)
    article.title = request.json.get('title')
    db.session.commit()
    return {'id': article.id}

CORS Support:

@app.route('/api/resource', methods=['GET', 'OPTIONS'])
@has_valid_access_token  # OPTIONS bypassed by default
@has_required_scopes(['read'])
def resource():
    return {'data': 'success'}

Documentation

Full documentation: https://axioms-flask-py.abhishek-tiwari.com

License

MIT License - see LICENSE file for details.

Metadata

Release files for axioms-flask-py 0.0.20rc101763935971

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

Source distribution (sdist)

Source distribution for axioms-flask-py 0.0.20rc101763935971
File Size Uploaded
axioms_flask_py-0.0.20rc101763935971.tar.gz 57.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for axioms-flask-py 0.0.20rc101763935971
File Interpreter ABI Platform
axioms_flask_py-0.0.20rc101763935971-py3-none-any.whl Python 3 none any Details

Total release size: 73.7 kB

Release files / axioms_flask_py-0.0.20rc101763935971.tar.gz

Download URL axioms_flask_py-0.0.20rc101763935971.tar.gz
Size 57.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b6e2021e1730bc3f9814de7e75b7377ebd9704d23fd90aa43cef471c65f0e46f
BLAKE2b-256 checksum
How to use checksums
724ae316712a7f08934d2f97c3fb82d52c0fbcfc0c0a450354c60a72a9519354
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2025.

Transparency log

Release files / axioms_flask_py-0.0.20rc101763935971-py3-none-any.whl

Download URL axioms_flask_py-0.0.20rc101763935971-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f3353fa3e2266406c1d0fa861e41df40340894738bf8a44478b25af3d27d2700
BLAKE2b-256 checksum
How to use checksums
1d13402d3d5acf5ceb01dd51e4b12f0a1a4150b741a68280a0fc7f9ac37f2c3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2025.

Transparency log

Release history Release notifications | RSS feed

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.20

2 release files

This release

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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