Skip to main content

FastAPI ABAC authorization realization

Project description

DAuth

Python FastAPI ABAC Realization.


Getting started

Requirements:

  • redis - for cache support
  • databases - for database support

Install:

pip install dauth

After installing import main functions by

from dauth import auth

Library represents function

def Policy(
    subject_call: Callable, 
    resource_type: Any, 
    method: str, 
    check_callback: Callable,
    database: Optional[Database] = None,
    cache: Optional[Redis] = None
)
  • subject_callback - Is function that library put in FastAPI Depends . Usually function returns user which tries to work with resource
  • resource_type - Is str which on which User tries to get access
  • method - Is API method by which working endpoint
  • check_callback - Is function that realize Policy's check. Function take arguments:
    check_callback(subject, resource_type, item_id, method, db, cache)
    
    • subject is result of Depends(subject_callback)
    • resource_type is argument of Policy()
    • item_id (by default '*') is providing by FastAPI decorator @app.get(/test/{item_id})
    • method is argument of Policy()
    • db is database connection
    • cache is redis connection
  • database - Is a database connecion
  • cache - Is a Redis connection

Examples

Simple usage

from fastapi import FastAPI, Depends
from dauth import auth

app = FastAPI()

def is_admin(subject, resource_type, item_id, method, db, cache):
    if 'admin' not in subject.scopes:
        raise auth.DENY

@app.get("/test")
# function get_user_auth returns User's object
def test(
    user = Depends(auth.Policy(
        get_user_auth, 
        'test', 
        'get', 
        is_admin
    ))
):
    return {"message":"Good"}


@app.get("/test_with_db")
# function get_database returns Databases connection
def test_db(
    user = Depends(auth.Policy(
        get_user_auth, 
        'test', 
        'get', 
        is_admin, 
        database=Depends(get_database)
    ))
):
    return {"message":"Good"}

@app.get("/test_with_cache")
# function get_cache returns Redis connection
def test_cache(
    user = Depends(auth.Policy(
        get_user_auth, 
        'test', 
        'get', 
        is_admin, 
        cache=Depends(get_cache)
    ))
):
    return {"message":"Good"}

Developed by DenVilk

Project details


Download files

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

Source Distribution

dauth-0.7.tar.gz (2.4 kB view details)

Uploaded Source

File details

Details for the file dauth-0.7.tar.gz.

File metadata

  • Download URL: dauth-0.7.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for dauth-0.7.tar.gz
Algorithm Hash digest
SHA256 20bcf61e3fc91e041a2b8df3d031992f9078ba46ba08f5331e0867f08c34ae3d
MD5 403d451f36bc26ec445a5ae2f2651b97
BLAKE2b-256 a3121ec127361426ea223c291130f73ca23c3b1cc1815bbf93236e25fc239192

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