Skip to main content

Build Code style: black

Authorization-hero

Add authorization to your Flask application in 1 line per endpoint!

This package can be used to efficiently handle authorization in a Flask application. It is fully decoupled from authentication. Therefore, you can use any authentication method you want (Azure AD, username/password, etc.).

Authorization is checked each time an endpoint is requested. It is up to the developer to implement a method to identify the user (authentication) and load user authorizations. One is free to cache authentication data or reload it upon each request. It is up to the developer to find a good tradeoff between security (always reload) and performance (cache).

  • This package fully supports Role-based access control (RBAC). This authorization method is mostly used in enterprise settings.
  • The package also supports Attribute-based access control (ABAC) which is an extension of RBAC, but also includes other attributes.
    • For example, one could check that a user is part of a certain group AND is over 18.
    • One could check that a user is part of a certain group AND only allow access to an endpoint during working hours.
    • One could only allow access to an endpoint when the user has been registered for more than 1 month.

Python 3.11 and Pyton 3.12 are supported.

FastAPI support will be added in a future release.

Installation

The package can be installed using pip. Simply run the command below.

pip install authorization-hero

How to use

To incorporate authorization into your codebase, start by importing the Authorizer class. Next, create two functions: one to load the user and another to be executed when an endpoint is forbidden for a user.

Now, create a function to handle your authorization logic. This function should take the user as its only input argument.

For each endpoint in your application, add a decorator to check whether the user has a certain permission.

from flask import Flask, abort

from authorization_hero import Authorizer


def flask_forbidden():
    abort(403, "Forbidden: you do not have access to this resource")


def load_user() -> dict:
    """Business logic for authentication goes here"""
    return {"name": "Joe Example", "permissions": ["view", "edit"]}


def user_can_view(user: dict) -> bool:
    return 'view' in user["permissions"]


app = Flask(__name__)
authorizer = Authorizer(load_user, flask_forbidden)


@app.route("/")
@authorizer.requires_permission(user_can_view)
def hello_world():
    return "<p>Hello World!</p>"

The order of the wrappers matters!

NOTE: The wrapper indicating the Flask route must come before the wrapper for authorization. Otherwise, authorization will not be executed. So, use the order below.

@app.route("/")
@authorizer.requires_permission(user_can_view)
def hello_world():
    return "<p>Hello World!</p>"

Additional requirements

To initialize the Authorizer class, two input parameters are required: identity_loader and on_forbidden. Both must be functions and must adhere to the following conditions:

  • The identity_loader function must have no input parameters and should return user data.
  • The on_forbidden function must have no input parameters.
  • Each authorization function must take exactly one input parameter, which should be the return value of the identity_loader function.
  • Each authorization function must return a boolean value indicating whether an endpoint is allowed or forbidden for the user.

The package is tested and adheres to the black code style. Have a look at the test suite for more suggestions on how to use this package.

Metadata

Release files for authorization-hero 0.1.1

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

Source distribution (sdist)

Source distribution for authorization-hero 0.1.1
File Size Uploaded Provenance
authorization_hero-0.1.1.tar.gz 3.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for authorization-hero 0.1.1
File Interpreter ABI Platform
authorization_hero-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / authorization_hero-0.1.1.tar.gz

Download URL authorization_hero-0.1.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
975af8a83321cb760ed1109ce0f9ebff285db72b760bd2f40e56d6ebd94fc894
BLAKE2b-256 checksum
How to use checksums
525474a3d2e9124969971d330dbb6b47173b3abc8a54379e51abf4cbaee5284d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release files / authorization_hero-0.1.1-py3-none-any.whl

Download URL authorization_hero-0.1.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
214a77cf6458b5aa029e0f2c7117dc3b3474b1b035e7ff1603c3e5ee665a8a08
BLAKE2b-256 checksum
How to use checksums
1a92e39c00bdf0d760635ffe9f2028080010a009c97f738960c8bd50a615df76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Security for authorization-hero 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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