Skip to main content

Simple FastAPI declarative endpoint-level access control.

Project description

Missil

Simple FastAPI declarative endpoint-level access control, somewhat inspired by Pyramid.

[DOCS] [SOURCE]

Package version Supported Python versions

@app.get("/", dependencies=[rules["finances"].READ])
def read_root():
    return {"Hello": "World"}

Installation

pip install missil

Why use Missil?

For most applications the use of scopes to determine the rights of a user is sufficient enough. Nonetheless, scopes are tied to the state of the user, while 'missil' also take the state of the requested resource into account.

Let's take an scientific paper as an example: depending on the state of the submission process (like "draft", "submitted", "peer review" or "published") different users should have different permissions on viewing and editing. This could be acomplished with custom code in the path definition functions, but Missil offers a very legible and to-the-point to define these constraints.

Quick usage

import missil

from fastapi import FastAPI
from fastapi import Response

app = FastAPI()

TOKEN_KEY = "Authorization"
SECRET_KEY = "2ef9451be5d149ceaf5be306b5aa03b41a0331218926e12329c5eeba60ed5cf0"

bearer = missil.FlexibleTokenBearer(TOKEN_KEY, SECRET_KEY)
rules = missil.make_rules(bearer, "finances", "it", "other")

@app.get("/", dependencies=[rules["finances"].READ])
def read_root():
    return {"Hello": "World"}


@app.get("/set-cookies")
def set_cookies(response: Response) -> None:
    """Just for example purposes."""
    sample_user_privileges = {
        "finances": missil.READ,
        "it": missil.WRITE,
    }

    token_expiration_in_hours = 8
    token = missil.encode_jwt_token(claims, SECRET_KEY, token_expiration_in_hours)

    response.set_cookie(
        key=TOKEN_KEY,
        value=f"Bearer {token}",
        httponly=True,
        max_age=1800,
        expires=1800,
    )

Disclaimer

Scopes did not meet my needs and other permission systems were too complex, so I designed this code for me and my team needs, but feel free to use it if you like.

License

This project is licensed under the terms of the MIT license.

Download files

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

Source Distribution

missil-0.1.9.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

missil-0.1.9-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file missil-0.1.9.tar.gz.

File metadata

  • Download URL: missil-0.1.9.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.24.1 CPython/3.12.2 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for missil-0.1.9.tar.gz
Algorithm Hash digest
SHA256 0f376976bb8a4b9e743557fc40f1d7b1e4dabf523585eff2e03579e8ccc3fa79
MD5 7559a076dda4a9898f067990393155b9
BLAKE2b-256 c2581f35e8ae8bc7f6817d7b2279bdef630ac7474fa1aa110f55ea6b235a12e1

See more details on using hashes here.

File details

Details for the file missil-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: missil-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.24.1 CPython/3.12.2 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for missil-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a2ae03d3a3628a9162b80a2f987ad424bb83a039dbe7ced47f8af5d6f3279569
MD5 bb71c078536d333e7011ba311d4bd374
BLAKE2b-256 2bdf2ff23e86891018e7746d9f187ffd3d2f59c0cef8930c2ef2b771587a2bb6

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