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 by providing settings and hook functions.
  • 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", authz="employee")
def get_store(pattern: str = "%"):
    # return the list of stores matching optional parameter pattern
    return ..., 200

@app.post("/store/<sid>/product", authz=("store", "sid", "manager"))
def post_store_sid_product(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 authz parameter. How these are checked is also set from the configuration. HTTP or JSON parameters are automatically converted to the expected type, with features on par with FastAPI.

Authentication and authorizations are provided to the framework with callback functions. For our example, we will need to retrieve the salted hashed password for a user, to check whether a user belongs to a group, and to tell whether a user can access a given store in a particular role:

# File "auth.py"
def get_user_pass(user: str) -> str|None:
    return ...  # hashed password retrieved from somewhere

def user_is_employee(user: str) -> bool:
    return ...  # whether user belongs to group employee

def store_perms(user: str, sid: int, role: str) -> bool|None:
    return ...  # whether user can access store sid in role

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

# File "acme.conf"
import os
import auth

FSA_MODE = "dev"
FSA_AUTH = ["token", "basic"]
FSA_TOKEN_TYPE = "jwt"
FSA_TOKEN_SECRET = os.environ["ACME_SECRET"]
FSA_GET_USER_PASS = auth.get_user_pass
FSA_GROUP_CHECK = { "employee": auth.user_is_employee }
FSA_OBJECT_PERMS = { "store": auth.store_perms }

The framework will ensure 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. Also, pydantic, dataclass and generic type parameters are supported out of the box.

More

License

This code 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

36.0

Download files

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

Source Distribution

flasksimpleauth-36.0.tar.gz (52.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flasksimpleauth-36.0-py3-none-any.whl (49.7 kB view details)

Uploaded Python 3

File details

Details for the file flasksimpleauth-36.0.tar.gz.

File metadata

  • Download URL: flasksimpleauth-36.0.tar.gz
  • Upload date:
  • Size: 52.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flasksimpleauth-36.0.tar.gz
Algorithm Hash digest
SHA256 8e9a16128c1b0a007bc72709218cc02e21fc563fdedf2736cac03cf14d11b2cd
MD5 2e4863edffac2f2cda9bdeac605c536c
BLAKE2b-256 602d1e90c1c9848154cd7c18b0eb994f545a78eb248f95db252455c55045c7b7

See more details on using hashes here.

File details

Details for the file flasksimpleauth-36.0-py3-none-any.whl.

File metadata

  • Download URL: flasksimpleauth-36.0-py3-none-any.whl
  • Upload date:
  • Size: 49.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flasksimpleauth-36.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68c6a4507d3e55ec7ea705e773869c4731b89a3bd13bc9d5042b50b8b87d6da4
MD5 0fef0c2a1f7a7c23615fc345c1185161
BLAKE2b-256 a11eeef44e2a81df1e49f2ebfa8cbc5285a78f5a1102d2dd4a68641626551f42

See more details on using hashes here.

Supported by

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