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.1.1rc121763952623

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.1.1rc121763952623
File Size Uploaded
axioms_flask_py-0.1.1rc121763952623.tar.gz 57.4 kB Details

Built distribution (wheel)

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

Total release size: 73.2 kB

Release files / axioms_flask_py-0.1.1rc121763952623.tar.gz

Download URL axioms_flask_py-0.1.1rc121763952623.tar.gz
Size 57.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ac86f1251c2c22e939eb6135dc38ecb8580a265149499baf0b26900623d4ef44
BLAKE2b-256 checksum
How to use checksums
7399b772e670b80b5d16ec1ff215826d4edef6241483d3deba803c8aab0c3dff
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 24, 2025.

Transparency log

Release files / axioms_flask_py-0.1.1rc121763952623-py3-none-any.whl

Download URL axioms_flask_py-0.1.1rc121763952623-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23eafa771e5e9b3abdb6029bb257bb007b8d69d7d785700fb1a21e101a424387
BLAKE2b-256 checksum
How to use checksums
c541cef5f1ec2fddff9b05c2fa0c7bee57c1385a840e606fd550bf216c2a13c6
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 24, 2025.

Transparency log

Release history Release notifications | RSS feed

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0

2 release files

0.0.20

2 release files

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