Skip to main content
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░  ░░░░░░░░░░░░░░░░░░░   ░░░░░░░░░░░░░░░░░░░░░░░░░░     ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░   ░
▒▒▒▒▒▒  ▒  ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒   ▒▒▒   ▒▒▒▒▒▒▒   ▒  ▒▒▒▒▒  ▒▒▒▒   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒   ▒
▒▒▒▒▒  ▒▒   ▒▒▒▒▒   ▒▒   ▒    ▒  ▒   ▒▒▒▒▒▒  ▒▒▒▒▒   ▒  ▒▒▒▒▒▒▒▒▒▒▒   ▒▒   ▒▒▒▒   ▒▒▒▒▒  ▒    ▒▒▒▒▒▒   ▒
▓▓▓▓   ▓▓▓   ▓▓▓▓   ▓▓   ▓▓▓   ▓▓▓     ▓▓▓▓▓▓▓▓▓   ▓▓▓   ▓▓▓▓▓▓▓▓▓▓   ▓▓   ▓▓   ▓▓   ▓▓▓   ▓▓▓▓▓   ▓   ▓
▓▓▓       ▓   ▓▓▓   ▓▓   ▓▓▓   ▓▓▓   ▓▓  ▓▓▓▓▓   ▓▓▓▓▓   ▓▓▓      ▓   ▓▓   ▓   ▓▓▓   ▓▓▓   ▓▓▓▓  ▓▓▓   ▓
▓▓   ▓▓▓▓▓▓▓   ▓▓   ▓▓   ▓▓▓   ▓ ▓  ▓▓▓   ▓▓   ▓▓▓▓▓▓▓▓   ▓▓▓▓  ▓▓▓   ▓▓   ▓   ▓▓▓   ▓▓▓   ▓▓▓▓  ▓▓▓   ▓
█   █████████   ███      ████   ██  ███   █         ████      ███████      ███   █    █    █████   █   █
████████████████████████████████████████████████████████████████████████████████████████████████████████
By: CenturyBoys

A simple route decorator JWT scope validator.

This project work with the follow frameworks:

✅ FastApi

✅ aiohttp

Config

Configuration are exposed and can be set in any time including out of the use scope.

Obs: all configs are saved as singleton.

jwk

The jwk key to validate JWT can be bytes, str or dict. This config need to be set!

http_header_name_token

If your application use a custom header to send the authentication token you can use this param to indicate his name. By default, the value is 'Authorization'

request_token_callback

If to extract the request token you need to perform some operation you can set a callback for it. Will receive the request as param and must return a str with token type and the token 'Basic XXX'

import auth2guard

class Request:
    def __init__(self, headers: dict):
        self._headers = headers

    @property
    def headers(self) -> dict:
        return self._headersclass
    
request = Request(headers={"x-token": f"Basic Akj817Hakn122i..."})

def request_token_callback(request: Request):
        return request.headers.get("x-token")
    
    
auth2guard.set_config(
    jwk='{"p":"-7pCvLlzsNIRD7utbLZqB...',
    http_header_name_token="x-token",
    request_token_callback=request_token_callback
)

Exceptions

The package raise exceptions for some cases se bellow.

Obs: By default, all exception are ValueError.

token_not_found

Error when token was not found.

Obs: The config request_token_callback can be the problem.

not_from_origin

Error when token was generated not by the giving JWK.

Obs: Validate the config jwk.

expired

Error when exp JWT param exceeded the time.

unauthorized

Error when the JWT has not all necessary scope to proceed.

import auth2guard

class MyException(Exception):
    pass

auth2guard.overwrite_exceptions(unauthorized=MyException)

Validator

Can be used as decorator and receive a list of scopes. The validator will operate AND validation or a OR validation with the token scope content. For the AND validation all scopes in the allowed_scopes param need to be present in the jwt scope and in the OR if any scope is present that's enough. You can receive the token content if you want by setting token_content to True this will inject the param token_content: dict into your function as kwargs

import auth2guard


class Request:
    def __init__(self, headers: dict):
        self._headers = headers

    @property
    def headers(self) -> dict:
        return self._headers


auth2guard.set_config(jwk='{"p":"-7pCvLlzsNIRD7utbLZqB...')


@auth2guard.validate(
    allowed_scopes=["test1"], 
    scope_and_validation=True, 
    inject_token_content=True,
    allowed_audiences=["test1"],
    audience_and_validation=True
)
def route_callback(request, token_content: dict):
    pass


request = Request(headers={"Authorization": f"Basic XXX"})
route_callback(request=request)

Metadata

Release files for auth2guard 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for auth2guard 0.4.0
File Size Uploaded
auth2guard-0.4.0.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for auth2guard 0.4.0
File Interpreter ABI Platform
auth2guard-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / auth2guard-0.4.0.tar.gz

Download URL auth2guard-0.4.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
15351586cefd0769adb3c12634953ac526198bd59dfff0af6c38823b6dfbfc08
BLAKE2b-256 checksum
How to use checksums
24f46c7ddc82b3db3c07cbffb0376f99d900995310161d2c286883db3a1be8b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.6 Linux/5.19.0-43-generic

Release files / auth2guard-0.4.0-py3-none-any.whl

Download URL auth2guard-0.4.0-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ce4705a6476b926f44835601de55b43e11322daf91f5d288875875f347f1607
BLAKE2b-256 checksum
How to use checksums
974c1fc83484628ef7439e57ad805091fcb29a3ca9e71d9628c52b13a3caa0d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.6 Linux/5.19.0-43-generic

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

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