Skip to main content

Authorization in Flask

Project description

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.

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

authorization_hero-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

authorization_hero-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file authorization_hero-0.1.0.tar.gz.

File metadata

  • Download URL: authorization_hero-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for authorization_hero-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0c50fbe4ca2fd44ab41a7ee90ba0ef2a22db3b882760b79d7dc75eba09fd476c
MD5 f48d4c611c26c25a0807a9d4e7dbd499
BLAKE2b-256 208b018a9fc7053f6a765c7003379df71594bde99516c94f7740f4ba26158c02

See more details on using hashes here.

File details

Details for the file authorization_hero-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for authorization_hero-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5721ed28bccdb29235613c9d0b8ef630089e84d26c3eb4d05846cd93d3a039d
MD5 7d4d7f65b5c0a41e171cb4e321048b93
BLAKE2b-256 252afca222c2a052721e230aa27182241f6ef8e5706a9f12ac96eb052a08329c

See more details on using hashes here.

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