Skip to main content

OAuth 2.0 scope validator

Project description

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░  ░░░░░░░░░░░░░░░░░░░   ░░░░░░░░░░░░░░░░░░░░░░░░░░     ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░   ░
▒▒▒▒▒▒  ▒  ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒   ▒▒▒   ▒▒▒▒▒▒▒   ▒  ▒▒▒▒▒  ▒▒▒▒   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒   ▒
▒▒▒▒▒  ▒▒   ▒▒▒▒▒   ▒▒   ▒    ▒  ▒   ▒▒▒▒▒▒  ▒▒▒▒▒   ▒  ▒▒▒▒▒▒▒▒▒▒▒   ▒▒   ▒▒▒▒   ▒▒▒▒▒  ▒    ▒▒▒▒▒▒   ▒
▓▓▓▓   ▓▓▓   ▓▓▓▓   ▓▓   ▓▓▓   ▓▓▓     ▓▓▓▓▓▓▓▓▓   ▓▓▓   ▓▓▓▓▓▓▓▓▓▓   ▓▓   ▓▓   ▓▓   ▓▓▓   ▓▓▓▓▓   ▓   ▓
▓▓▓       ▓   ▓▓▓   ▓▓   ▓▓▓   ▓▓▓   ▓▓  ▓▓▓▓▓   ▓▓▓▓▓   ▓▓▓      ▓   ▓▓   ▓   ▓▓▓   ▓▓▓   ▓▓▓▓  ▓▓▓   ▓
▓▓   ▓▓▓▓▓▓▓   ▓▓   ▓▓   ▓▓▓   ▓ ▓  ▓▓▓   ▓▓   ▓▓▓▓▓▓▓▓   ▓▓▓▓  ▓▓▓   ▓▓   ▓   ▓▓▓   ▓▓▓   ▓▓▓▓  ▓▓▓   ▓
█   █████████   ███      ████   ██  ███   █         ████      ███████      ███   █    █    █████   █   █
████████████████████████████████████████████████████████████████████████████████████████████████████████
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(["test1"], and_validation=True, token_content=True)
def route_callback(request, token_content: dict):
    pass

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

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

auth2guard-0.3.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

auth2guard-0.3.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file auth2guard-0.3.0.tar.gz.

File metadata

  • Download URL: auth2guard-0.3.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.19.0-38-generic

File hashes

Hashes for auth2guard-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f597649190cffc75507b5a1d35d3966b4a9756ea21bd8a0beb04f8d166143983
MD5 66cc60c5daccd630b481e17e90463691
BLAKE2b-256 8b496bcdd0e06480323ec244cf57432683f5ad97ffba50017ee329b1eb06bcee

See more details on using hashes here.

File details

Details for the file auth2guard-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: auth2guard-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.19.0-38-generic

File hashes

Hashes for auth2guard-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0515500e9fe6cbc10786e6a90d2b5c53194ec9c9cd2ee3f2a6c555428f75d58f
MD5 df0247242640e0884e1aa2bbe5a58a8b
BLAKE2b-256 6d94fd998b05206a74df8c051e8e1e3d706c0b485d33e282ae5e651bcce3c2e7

See more details on using hashes here.

Supported by

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