Python Auth0 JWT Validator
Project description
Python Auth0 JWT Validator
Documentation: https://immersa-co.github.io/python-auth0-jwt-validator
Source Code: https://github.com/immersa-co/python-auth0-jwt-validator
Getting Started
The package is framework agnostic, so we need to implement it on each framework since each framework handles the requests and responses in different ways.
Requirements
- Python 3.8+
Installation
From PyPi
pip install auth0-jwt-validator
Examples
Flask Example
from functools import wraps
from flask import Flask, request, jsonify
from auth0_jwt_validator import (
get_token,
AccessTokenVerifier,
MissingClaimError,
InvalidClaimError,
)
app = Flask(__name__)
auth0_jwks_uri = "https://<auth0-tenant>.us.auth0.com/.well-known/jwks.json"
issuer = "https://<auth0-tenant>.us.auth0.com/"
audience = "https://<auth0-tenant>.us.auth0.com/api/v2/"
access_token_verifier = AccessTokenVerifier(auth0_jwks_uri, issuer, audience)
@app.errorhandler(MissingClaimError)
def missing_claim_error_handler(e: MissingClaimError):
return e.description, 401
@app.errorhandler(InvalidClaimError)
def missing_claim_error_handler(e: InvalidClaimError):
return e.description, 401
def get_bearer_token(authorization: str | None) -> str | None:
return get_token(authorization)
def get_access_token_payload(bearer_token: str | None) -> dict:
return access_token_verifier.verify(bearer_token)
def route_get_access_token_payload(f):
@wraps(f)
def _route_get_access_token_payload(*args, **kwargs):
authorization = request.headers.get("authorization")
bearer_token = get_bearer_token(authorization)
access_token_payload = get_access_token_payload(bearer_token)
return f(*args, **kwargs, access_token_payload=access_token_payload)
return _route_get_access_token_payload
@app.get("/")
@route_get_access_token_payload
def index(access_token_payload: dict):
return jsonify({"access_token_payload": access_token_payload})
Fast API Example
from fastapi import FastAPI, Header, Request, HTTPException, Depends
from fastapi.exception_handlers import http_exception_handler
from auth0_jwt_validator import (
get_token,
AccessTokenVerifier,
MissingClaimError,
InvalidClaimError,
)
app = FastAPI()
auth0_jwks_uri = "https://<auth0-tenant>.us.auth0.com/.well-known/jwks.json"
issuer = "https://<auth0-tenant>.us.auth0.com/"
audience = "https://<auth0-tenant>.us.auth0.com/api/v2/"
access_token_verifier = AccessTokenVerifier(auth0_jwks_uri, issuer, audience)
@app.exception_handler(MissingClaimError)
def missing_claim_error_handler(request: Request, exc: MissingClaimError):
return await http_exception_handler(
request, HTTPException(status_code=401, detail=exc.description)
)
@app.exception_handler(InvalidClaimError)
def missing_claim_error_handler(request: Request, exc: InvalidClaimError):
return await http_exception_handler(
request, HTTPException(status_code=401, detail=exc.description)
)
async def get_bearer_token(
authorization: str | None = Header(default=None),
) -> str | None:
return get_token(authorization)
async def get_access_token_payload(
bearer_token: str | None = Depends(get_bearer_token),
) -> dict:
return access_token_verifier.verify(bearer_token)
@app.get("/")
async def index(access_token_payload: dict = Depends(get_access_token_payload)):
return {"access_token_payload": access_token_payload}
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
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 auth0-jwt-validator-1.2.tar.gz.
File metadata
- Download URL: auth0-jwt-validator-1.2.tar.gz
- Upload date:
- Size: 122.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ff9d9499b9770d68fe4be6e22da5ee74e62f4f6f0052e894ab09fb027f69220
|
|
| MD5 |
550dc3ed88113e5bdec24ede45a16d39
|
|
| BLAKE2b-256 |
bb97cbce8accf11caee49d0df8a405f5422dab06b552267dc8c1ca2376844c3c
|
File details
Details for the file auth0_jwt_validator-1.2-py3-none-any.whl.
File metadata
- Download URL: auth0_jwt_validator-1.2-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba9433b41841e5e74228542f00c6a0752e86cbcd74dd237ce7de92205372e52a
|
|
| MD5 |
06587c65acc1cb08350bdfa26918c960
|
|
| BLAKE2b-256 |
3709a70da2ec436b34a3818c9cd0d11168cf37939a58071a0e037db2cfac0e87
|