Skip to main content

Simple authentication, authorization and parameters for Flask, emphasizing configurability

Project description

FlaskSimpleAuth: The Secure Flask Framework

FlaskSimpleAuth is a Flask wrapper to add a declarative security layer to routes with authentification, authorization and parameter management.

Status Tests Coverage Issues Python Version Badges License

With FlaskSimpleAuth, application and security concerns are separated:

  • the application focusses on what to do, and declares its security requirements.
  • the configuration declares how the authentification and authorization constraints are checked, with numerous state-of-the-art options made available through directives and hooks.
  • the framework implements and enforces the security on the application routes, with safe defaults so that security cannot be overlooked.

The following Flask application provides two routes:

  • GET /store allows any authenticated user in group employee to access the store list.
  • POST /store/<sid> allows an authenticated user who is a manager of store number sid to add a quantity of product to the store inventory.
# file "app.py"
from FlaskSimpleAuth import Flask

app = Flask("acme")
app.config.from_envvar("ACME_CONFIG")

@app.get("/store", authorize="employee")
def get_store(pattern: str = "%"):
    # return the list of stores matching optional parameter pattern
    return ..., 200

@app.post("/store/<sid>", authorize=("store", "sid", "manager"))
def post_store_sid(sid: int, product: str, quantity: int):
    # product is added in quantity to store sid
    return ..., 201

In this code, there is no clue about how users are authenticated, as this is set from the configuration. Only authorizations are declared on the route with the mandatory authorize parameter. How these are checked is also set from the configuration. HTTP or JSON parameters are automatically converted to the expected type.

Here is an example of configuration for the above application: Users are identified either with a JWT token or with a basic authentification.

# acme configuration
import os

FSA_MODE = "dev"
FSA_AUTH = ["token", "basic"]
FSA_TOKEN_TYPE = "jwt"
FSA_TOKEN_SECRET = os.environ["ACME_SECRET"]

In this example, the framework needs three callbacks: one to retrieve the salted hashed password for a user, one to check whether a user belongs to a group, and one for telling whether a user can access a given store in a particular role.

# authentication and authorization callbacks
@app.get_user_pass
def get_user_pass(user: str) -> str|None:
    return ...  # hashed password retrieved from somewhere

@app.user_in_group
def user_in_group(user: str, group: str) -> bool:
    return ...  # whether user belongs to group

@app.object_perms("store")
def store_permission(user: str, sid: int, role: str) -> bool|None:
    return ...  # whether user can access store sid in role

The framework ensures that routes are only called by authenticated users who have the right authorizations. Secure and reasonable defaults are provided. Most features can be adjusted or extended to particular needs through numerous directives and hooks. Authentication and authorization callback invocations are cached for efficiency.

More

License

This software is public domain.

All software has bug, this is software, hence… Beware that you may lose your hairs or your friends because of it. If you like it, feel free to send a postcard to the author.

Project details


Release history Release notifications | RSS feed

This version

25.2

Download files

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

Source Distribution

FlaskSimpleAuth-25.2.tar.gz (38.9 kB view hashes)

Uploaded Source

Built Distribution

FlaskSimpleAuth-25.2-py3-none-any.whl (36.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