Skip to main content

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.

Metadata

Release files for FlaskSimpleAuth 35.6

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

Source distribution (sdist)

Source distribution for FlaskSimpleAuth 35.6
File Size Uploaded Provenance Actions
flasksimpleauth-35.6.tar.gz 52.0 kB Without provenance Details for flasksimpleauth-35.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for FlaskSimpleAuth 35.6
File Interpreter ABI Platform Actions
flasksimpleauth-35.6-py3-none-any.whl Python 3 none any Details for flasksimpleauth-35.6-py3-none-any.whl

Total release size: 101.0 kB

Release files / flasksimpleauth-35.6.tar.gz

Download URL flasksimpleauth-35.6.tar.gz
Size 52.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fc7f17593c856693ba46d6b81be0e4311d7a101cc1d2d7bffd0313f5bed90e84
BLAKE2b-256 checksum
How to use checksums
0acf9803820beffc8bcf4fc2193a56460a954585538b88a8606bc2402696744a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / flasksimpleauth-35.6-py3-none-any.whl

Download URL flasksimpleauth-35.6-py3-none-any.whl
Size 48.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
11576eac65603f349a065eb54368b4533f015c06596d783a768b99ebe4ea8207
BLAKE2b-256 checksum
How to use checksums
2c58a1597928c55aff8d6a25ae6a94a8824e1ac6c6c0cda21ddca04fde64c00b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for FlaskSimpleAuth 35.6

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

35.6 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