Core JWT authentication and authorization logic for Axioms packages
Project description
axioms-core-py
Core JWT authentication and authorization library for Python web frameworks.
Overview
axioms-core-py provides framework-agnostic JWT token validation, JWKS management with background refresh, and claim-based authorization. This is the foundation for framework-specific integrations:
- axioms-flask-py - Flask integration
- axioms-fastapi - FastAPI integration
- axioms-drf-py - Django REST Framework integration
Note: For most use cases, use one of the framework integrations above instead of using this package directly.
Supports
- JWT token validation with automatic JWKS retrieval
- Background JWKS refresh with thread-safe caching
- Algorithm validation (only secure asymmetric algorithms allowed)
- Token type validation (typ header)
- Issuer validation (iss claim)
- Claim-based authorization (scopes, roles, permissions)
- Custom claim name support for different authorization servers
- Comprehensive logging with JWT ID (jti) tracking
Prerequisite
- Python 3.10+
Installation
pip install axioms-core-py
Quick Start
Configuration
from axioms_core import AxiomsConfig
config = AxiomsConfig(
AXIOMS_AUDIENCE="your-api-audience",
AXIOMS_ISS_URL="https://auth.example.com", # Recommended
# Or use AXIOMS_DOMAIN for simpler setup:
# AXIOMS_DOMAIN="auth.example.com",
)
Initialize JWKS Manager (Optional)
For production, initialize at startup to enable background refresh:
from axioms_core import initialize_jwks_manager, shutdown_jwks_manager
# At startup
initialize_jwks_manager(
config=config,
refresh_interval=3600, # Refresh every hour
cache_ttl=7200, # Cache for 2 hours
prefetch=True # Fetch JWKS immediately
)
# At shutdown (optional - auto-cleanup on exit)
shutdown_jwks_manager()
Token Validation
from axioms_core import get_key_from_jwks_json, check_token_validity
import jwt
# Extract token header
header = jwt.get_unverified_header(token)
kid = header.get("kid")
alg = header.get("alg")
# Get public key from JWKS
key = get_key_from_jwks_json(kid, config)
# Validate token
payload = check_token_validity(
token=token,
key=key,
alg=alg,
audience=config.AXIOMS_AUDIENCE,
issuer=config.AXIOMS_ISS_URL,
)
if payload:
print(f"Valid token for user: {payload.sub}")
Authorization Checks
from axioms_core import check_scopes, check_roles, check_permissions
# Check scopes (space-separated or list)
has_scope = check_scopes(payload.scope, ["read:data"])
# Check roles (list)
has_role = check_roles(payload.roles, ["admin"])
# Check permissions (list)
has_perm = check_permissions(payload.permissions, ["users:read"])
Configuration Options
See AxiomsConfig class for all options:
config = AxiomsConfig(
# Required
AXIOMS_AUDIENCE="your-api-audience",
# Recommended (choose one)
AXIOMS_ISS_URL="https://auth.example.com",
# AXIOMS_DOMAIN="auth.example.com",
# AXIOMS_JWKS_URL="https://auth.example.com/.well-known/jwks.json",
# Optional
AXIOMS_TOKEN_TYPS=["JWT", "at+jwt"], # Allowed token types
AXIOMS_SAFE_METHODS=["OPTIONS"], # HTTP methods to bypass auth
# JWKS settings
AXIOMS_JWKS_REFRESH_INTERVAL=3600, # 1 hour
AXIOMS_JWKS_CACHE_TTL=7200, # 2 hours
AXIOMS_JWKS_PREFETCH=True,
# Custom claim names (for different auth servers)
AXIOMS_SCOPE_CLAIMS=["scope", "scp"],
AXIOMS_ROLES_CLAIMS=["roles", "cognito:groups"],
AXIOMS_PERMISSIONS_CLAIMS=["permissions"],
)
Configuration Hierarchy
AXIOMS_ISS_URL→ constructs →AXIOMS_JWKS_URL(if not set)AXIOMS_DOMAIN→ constructs →AXIOMS_ISS_URL→AXIOMS_JWKS_URL(if not set)
Security
- Allowed algorithms: RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512
- Token type validation: Validates
typheader against allowed types - Issuer validation: Validates
issclaim to prevent token substitution - Expiration validation: Validates
expclaim exists and is valid - Comprehensive logging: All validation failures logged with jti (if available)
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file axioms_core_py-0.0.2rc11763934375.tar.gz.
File metadata
- Download URL: axioms_core_py-0.0.2rc11763934375.tar.gz
- Upload date:
- Size: 33.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18e988875586b70681eaa812774035c62dc770cec580aea7add909d8fda4a95b
|
|
| MD5 |
2d5bde044888ab10628c70f3984e126d
|
|
| BLAKE2b-256 |
2d8cea9fc84d9717856d5596bd69298bf4f8383f80db161db8c65bebaeed8a21
|
Provenance
The following attestation bundles were made for axioms_core_py-0.0.2rc11763934375.tar.gz:
Publisher:
release.yml on abhishektiwari/axioms-core-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
axioms_core_py-0.0.2rc11763934375.tar.gz -
Subject digest:
18e988875586b70681eaa812774035c62dc770cec580aea7add909d8fda4a95b - Sigstore transparency entry: 718939019
- Sigstore integration time:
-
Permalink:
abhishektiwari/axioms-core-py@014db3272fde20bbaf8c96663a3e6cdbcecb8eca -
Branch / Tag:
refs/pull/1/merge - Owner: https://github.com/abhishektiwari
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@014db3272fde20bbaf8c96663a3e6cdbcecb8eca -
Trigger Event:
pull_request
-
Statement type:
File details
Details for the file axioms_core_py-0.0.2rc11763934375-py3-none-any.whl.
File metadata
- Download URL: axioms_core_py-0.0.2rc11763934375-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4b71dddc66aede3d13cee6409ccfd61dc4e842446b5d7c8b0d89e833bdb8ead
|
|
| MD5 |
04948b29e29bf7e22f80514946a99521
|
|
| BLAKE2b-256 |
0aec1a9a62aae82daea2fc345b7ba0f14bb39674d79da302ef36b7fd5b647add
|
Provenance
The following attestation bundles were made for axioms_core_py-0.0.2rc11763934375-py3-none-any.whl:
Publisher:
release.yml on abhishektiwari/axioms-core-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
axioms_core_py-0.0.2rc11763934375-py3-none-any.whl -
Subject digest:
e4b71dddc66aede3d13cee6409ccfd61dc4e842446b5d7c8b0d89e833bdb8ead - Sigstore transparency entry: 718939058
- Sigstore integration time:
-
Permalink:
abhishektiwari/axioms-core-py@014db3272fde20bbaf8c96663a3e6cdbcecb8eca -
Branch / Tag:
refs/pull/1/merge - Owner: https://github.com/abhishektiwari
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@014db3272fde20bbaf8c96663a3e6cdbcecb8eca -
Trigger Event:
pull_request
-
Statement type: