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 and provides necessary 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", 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.

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_in_group(user: str, group: str) -> bool:
    return ...  # whether user belongs to group

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.

# acme configuration
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_USER_IN_GROUP = auth.user_in_group
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.

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

26.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-26.0.tar.gz (39.4 kB view details)

Uploaded Source

Built Distribution

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

FlaskSimpleAuth-26.0-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

Details for the file FlaskSimpleAuth-26.0.tar.gz.

File metadata

  • Download URL: FlaskSimpleAuth-26.0.tar.gz
  • Upload date:
  • Size: 39.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for FlaskSimpleAuth-26.0.tar.gz
Algorithm Hash digest
SHA256 e1e8ab1bf81847cdb160f716e6077b6199d88364561b5d58745704ab125e116b
MD5 4440b347694a4a851b1d9c1dd9c60fad
BLAKE2b-256 6783da610f0858c3aadb0a9b9eb0c6ed0a9cf8b57254cc66c9103a0e5cb239c5

See more details on using hashes here.

File details

Details for the file FlaskSimpleAuth-26.0-py3-none-any.whl.

File metadata

  • Download URL: FlaskSimpleAuth-26.0-py3-none-any.whl
  • Upload date:
  • Size: 36.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for FlaskSimpleAuth-26.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6192432e421704aaa8ea1cd0b11d0646ae962f1475964db030aa867e7785af04
MD5 0450e7dbf4083c2f3b7fcfec3b451327
BLAKE2b-256 ecb6e374ae1fc6022a23148db1cd18fc1c7ce9836aa3427c4c1a73c4e2ed1c11

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