Skip to main content

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.2

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.2
File Size Uploaded
axioms_flask_py-0.1.2.tar.gz 57.3 kB Details

Built distribution (wheel)

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

Total release size: 73.0 kB

Release files / axioms_flask_py-0.1.2.tar.gz

Download URL axioms_flask_py-0.1.2.tar.gz
Size 57.3 kB
Tags Source
SHA-256 checksum
How to use checksums
32a702ded5939b23d0b899fd01b8f9e754692e87505e2fd4bd825f0f3ccf58a8
BLAKE2b-256 checksum
How to use checksums
9418d8683db6ee3d8ed2080aea53d2cf0d4827322bf026b2b1cd5419596549b8
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.2-py3-none-any.whl

Download URL axioms_flask_py-0.1.2-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69a7361083151131c3a54a6355446cf5f5b8a9edd50aa8f23df4cfb382f68e2c
BLAKE2b-256 checksum
How to use checksums
9afc7b6889399e13a8443e8bef018681bc2d355d4ae5436daf08fb00222556ad
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

This release

0.1.2 This release

2 release files

0.1.1

2 release files

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