Skip to main content

Python Auth0 JWT Validator

Project description

Python Auth0 JWT Validator

Docs Tests


Documentation: https://immersa-co.github.io/python-auth0-jwt-validator

Source Code: https://github.com/immersa-co/python-auth0-jwt-validator


Requirements

  • Python 3.8+

Installation

From repo

pip install git+ssh://github.com:immersa-co/python-auth0-jwt-validator.git

From PyPi

pip install python-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}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auth0-jwt-validator-0.1.tar.gz (122.5 kB view hashes)

Uploaded Source

Built Distribution

auth0_jwt_validator-0.1-py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page