2024-03-02T15:20:50,308 Created temporary directory: /tmp/pip-build-tracker-2twnc54r 2024-03-02T15:20:50,310 Initialized build tracking at /tmp/pip-build-tracker-2twnc54r 2024-03-02T15:20:50,310 Created build tracker: /tmp/pip-build-tracker-2twnc54r 2024-03-02T15:20:50,311 Entered build tracker: /tmp/pip-build-tracker-2twnc54r 2024-03-02T15:20:50,311 Created temporary directory: /tmp/pip-wheel-7g0u48nr 2024-03-02T15:20:50,315 Created temporary directory: /tmp/pip-ephem-wheel-cache-ld3vmohg 2024-03-02T15:20:50,337 Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple 2024-03-02T15:20:50,340 2 location(s) to search for versions of fastapi-authz: 2024-03-02T15:20:50,340 * https://pypi.org/simple/fastapi-authz/ 2024-03-02T15:20:50,340 * https://www.piwheels.org/simple/fastapi-authz/ 2024-03-02T15:20:50,341 Fetching project page and analyzing links: https://pypi.org/simple/fastapi-authz/ 2024-03-02T15:20:50,342 Getting page https://pypi.org/simple/fastapi-authz/ 2024-03-02T15:20:50,343 Found index url https://pypi.org/simple/ 2024-03-02T15:20:50,557 Fetched page https://pypi.org/simple/fastapi-authz/ as application/vnd.pypi.simple.v1+json 2024-03-02T15:20:50,561 Found link https://files.pythonhosted.org/packages/8b/9b/9f6f2758e16a9468b73dffcda68e2e9d32e14a13d772c14cd94a904530fc/fastapi-authz-0.0.2.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.2 2024-03-02T15:20:50,562 Found link https://files.pythonhosted.org/packages/4f/e3/8ecf3c892289933710e40acbdab5f90c2497fb710b340c827ae8c30882f0/fastapi-authz-0.0.3.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.3 2024-03-02T15:20:50,562 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/4a/30/1d69c592fa5e35854c0c1cafd2a15fae1ec5fb2e0544c3206b490f1ddc6c/fastapi_authz-0.0.3-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:50,563 Found link https://files.pythonhosted.org/packages/b9/f6/1ec5d2b7ef5ee8cd683f68d5bdce8a14385829de8e1b8678cdbba2806519/fastapi-authz-0.0.4.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.4 2024-03-02T15:20:50,564 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/4a/8e/2330010480340763acdcea3e23a42b18c9e67ece8abb09477c52918812aa/fastapi_authz-0.0.4-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:50,565 Found link https://files.pythonhosted.org/packages/6f/bf/8f100730f093857d2d449fda70ab2edd64cdbc81f315351486ce8a5932cb/fastapi-authz-0.0.5.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.5 2024-03-02T15:20:50,566 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/d6/7e/77099c723f9509bdc190adf46a4ba51293e3ae9f7ea0ba6d9939179f18ed/fastapi_authz-0.0.5-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:50,567 Found link https://files.pythonhosted.org/packages/ef/fb/11538a3ff6c41077ceba2eac1c09a22b33a2fd486c0b85d6199537c31f61/fastapi-authz-0.1.0.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.1.0 2024-03-02T15:20:50,568 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/ca/ec/a00a111fbaee811f40b03f77b5630a2035f9637bff3a0c7b9a3b9826db3a/fastapi_authz-0.1.0-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:50,569 Found link https://files.pythonhosted.org/packages/b2/f8/1a10eb0520e43ca6b8c874ef9e9db559aa51bf3bff7de7a0b9047b76b01c/fastapi-authz-0.2.0.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.2.0 2024-03-02T15:20:50,569 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/bb/ba/870732ae5918b3974676df01305c234d880c38a0448fde8d1d6dfac57330/fastapi_authz-0.2.0-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:50,570 Found link https://files.pythonhosted.org/packages/70/7f/5356c0736bdbbf3127e053d8893918e9491fc54945c578b32912f2ed1cdb/fastapi-authz-0.3.0.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.3.0 2024-03-02T15:20:50,571 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/f6/cc/44b77fefc0eb53a00103d9447cf7c6ccc9dfb3824aa99fb02103820478f6/fastapi_authz-0.3.0-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:50,572 Fetching project page and analyzing links: https://www.piwheels.org/simple/fastapi-authz/ 2024-03-02T15:20:50,572 Getting page https://www.piwheels.org/simple/fastapi-authz/ 2024-03-02T15:20:50,574 Found index url https://www.piwheels.org/simple/ 2024-03-02T15:20:59,862 Fetched page https://www.piwheels.org/simple/fastapi-authz/ as text/html 2024-03-02T15:20:59,867 Skipping link: No binaries permitted for fastapi-authz: https://www.piwheels.org/simple/fastapi-authz/fastapi_authz-0.2.0-py3-none-any.whl#sha256=0d6b2ce32da52710888a24358b85be62c99a3b15c17eddc0a45e7a33b18adf16 (from https://www.piwheels.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:59,868 Skipping link: No binaries permitted for fastapi-authz: https://www.piwheels.org/simple/fastapi-authz/fastapi_authz-0.1.0-py3-none-any.whl#sha256=3748966021c96b4db8a2b5beff8e9e17bc6b0316439aaa0121d8b6ea985bb643 (from https://www.piwheels.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:59,869 Skipping link: No binaries permitted for fastapi-authz: https://www.piwheels.org/simple/fastapi-authz/fastapi_authz-0.0.5-py3-none-any.whl#sha256=ea503184efc7dfdf37c08ad7a1e130692810883891a7c76e2865a69482326c35 (from https://www.piwheels.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:59,870 Skipping link: No binaries permitted for fastapi-authz: https://www.piwheels.org/simple/fastapi-authz/fastapi_authz-0.0.4-py3-none-any.whl#sha256=0838320254118b500b6315181195fb1a70ebcbe9f5048a86a766108bbbf948a9 (from https://www.piwheels.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:59,871 Skipping link: No binaries permitted for fastapi-authz: https://www.piwheels.org/simple/fastapi-authz/fastapi_authz-0.0.3-py3-none-any.whl#sha256=754216ce9cec748afc138ea225e783425741382bf14644f631b8c469049d28ff (from https://www.piwheels.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-02T15:20:59,872 Skipping link: not a file: https://www.piwheels.org/simple/fastapi-authz/ 2024-03-02T15:20:59,873 Skipping link: not a file: https://pypi.org/simple/fastapi-authz/ 2024-03-02T15:20:59,918 Given no hashes to check 1 links for project 'fastapi-authz': discarding no candidates 2024-03-02T15:20:59,958 Collecting fastapi-authz==0.3.0 2024-03-02T15:20:59,962 Created temporary directory: /tmp/pip-unpack-eobb5sbg 2024-03-02T15:21:00,179 Downloading fastapi-authz-0.3.0.tar.gz (9.0 kB) 2024-03-02T15:21:00,218 Added fastapi-authz==0.3.0 from https://files.pythonhosted.org/packages/70/7f/5356c0736bdbbf3127e053d8893918e9491fc54945c578b32912f2ed1cdb/fastapi-authz-0.3.0.tar.gz to build tracker '/tmp/pip-build-tracker-2twnc54r' 2024-03-02T15:21:00,219 Running setup.py (path:/tmp/pip-wheel-7g0u48nr/fastapi-authz_104c5233e8f74552ac8b2d91a5c72886/setup.py) egg_info for package fastapi-authz 2024-03-02T15:21:00,220 Created temporary directory: /tmp/pip-pip-egg-info-7armsf97 2024-03-02T15:21:00,221 Preparing metadata (setup.py): started 2024-03-02T15:21:00,222 Running command python setup.py egg_info 2024-03-02T15:21:01,040 # fastapi-authz 2024-03-02T15:21:01,041 [![Build Status](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml) 2024-03-02T15:21:01,042 [![Coverage Status](https://coveralls.io/repos/github/pycasbin/fastapi-authz/badge.svg)](https://coveralls.io/github/pycasbin/fastapi-authz) 2024-03-02T15:21:01,042 [![Version](https://img.shields.io/pypi/v/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,043 [![PyPI - Wheel](https://img.shields.io/pypi/wheel/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,043 [![Pyversions](https://img.shields.io/pypi/pyversions/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,044 [![Download](https://img.shields.io/pypi/dm/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,044 [![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN) 2024-03-02T15:21:01,045 fastapi-authz is an authorization middleware for [FastAPI](https://fastapi.tiangolo.com/), it's based 2024-03-02T15:21:01,046 on [PyCasbin](https://github.com/casbin/pycasbin). 2024-03-02T15:21:01,047 ## Installation 2024-03-02T15:21:01,048 Install from pip 2024-03-02T15:21:01,049 ```bash 2024-03-02T15:21:01,049 pip install fastapi-authz 2024-03-02T15:21:01,050 ``` 2024-03-02T15:21:01,051 Clone this repo 2024-03-02T15:21:01,052 ```bash 2024-03-02T15:21:01,052 git clone https://github.com/pycasbin/fastapi-authz.git 2024-03-02T15:21:01,053 python setup.py install 2024-03-02T15:21:01,054 ``` 2024-03-02T15:21:01,054 ## Quickstart 2024-03-02T15:21:01,055 This middleware is designed to work with another middleware which implement `AuthenticationMiddleware` interface. 2024-03-02T15:21:01,056 ```python 2024-03-02T15:21:01,057 import base64 2024-03-02T15:21:01,057 import binascii 2024-03-02T15:21:01,058 import casbin 2024-03-02T15:21:01,059 from fastapi import FastAPI 2024-03-02T15:21:01,060 from starlette.authentication import AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials 2024-03-02T15:21:01,060 from starlette.middleware.authentication import AuthenticationMiddleware 2024-03-02T15:21:01,061 from fastapi_authz import CasbinMiddleware 2024-03-02T15:21:01,062 app = FastAPI() 2024-03-02T15:21:01,064 class BasicAuth(AuthenticationBackend): 2024-03-02T15:21:01,065 async def authenticate(self, request): 2024-03-02T15:21:01,065 if "Authorization" not in request.headers: 2024-03-02T15:21:01,066 return None 2024-03-02T15:21:01,067 auth = request.headers["Authorization"] 2024-03-02T15:21:01,068 try: 2024-03-02T15:21:01,068 scheme, credentials = auth.split() 2024-03-02T15:21:01,069 decoded = base64.b64decode(credentials).decode("ascii") 2024-03-02T15:21:01,069 except (ValueError, UnicodeDecodeError, binascii.Error): 2024-03-02T15:21:01,070 raise AuthenticationError("Invalid basic auth credentials") 2024-03-02T15:21:01,071 username, _, password = decoded.partition(":") 2024-03-02T15:21:01,071 return AuthCredentials(["authenticated"]), SimpleUser(username) 2024-03-02T15:21:01,073 enforcer = casbin.Enforcer('../examples/rbac_model.conf', '../examples/rbac_policy.csv') 2024-03-02T15:21:01,074 app.add_middleware(CasbinMiddleware, enforcer=enforcer) 2024-03-02T15:21:01,074 app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) 2024-03-02T15:21:01,076 @app.get('/') 2024-03-02T15:21:01,077 async def index(): 2024-03-02T15:21:01,077 return "If you see this, you have been authenticated." 2024-03-02T15:21:01,079 @app.get('/dataset1/protected') 2024-03-02T15:21:01,079 async def auth_test(): 2024-03-02T15:21:01,080 return "You must be alice to see this." 2024-03-02T15:21:01,080 ``` 2024-03-02T15:21:01,086 - anonymous request 2024-03-02T15:21:01,087 ```bash 2024-03-02T15:21:01,088 curl -i http://127.0.0.1:8000/dataset1/protected 2024-03-02T15:21:01,088 ``` 2024-03-02T15:21:01,090 ```bash 2024-03-02T15:21:01,090 HTTP/1.1 403 Forbidden 2024-03-02T15:21:01,091 date: Mon, 01 Mar 2021 09:00:08 GMT 2024-03-02T15:21:01,092 server: uvicorn 2024-03-02T15:21:01,092 content-length: 11 2024-03-02T15:21:01,093 content-type: application/json 2024-03-02T15:21:01,094 "Forbidden" 2024-03-02T15:21:01,094 ``` 2024-03-02T15:21:01,095 - authenticated request 2024-03-02T15:21:01,096 ```bash 2024-03-02T15:21:01,097 curl -i -u alice:password http://127.0.0.1:8000/dataset1/protected 2024-03-02T15:21:01,097 ``` 2024-03-02T15:21:01,098 ```bash 2024-03-02T15:21:01,098 HTTP/1.1 200 OK 2024-03-02T15:21:01,099 date: Mon, 01 Mar 2021 09:04:54 GMT 2024-03-02T15:21:01,099 server: uvicorn 2024-03-02T15:21:01,100 content-length: 32 2024-03-02T15:21:01,100 content-type: application/json 2024-03-02T15:21:01,101 "You must be alice to see this." 2024-03-02T15:21:01,102 ``` 2024-03-02T15:21:01,103 It used the casbin config from `examples` folder, and you can find this demo in `demo` folder. 2024-03-02T15:21:01,103 You can also view the unit tests to understand this middleware. 2024-03-02T15:21:01,104 Besides, there is another example for `CasbinMiddleware` which is designed to work with JWT authentication. You can find 2024-03-02T15:21:01,105 it in `demo/jwt_test.py`. 2024-03-02T15:21:01,106 ## Development 2024-03-02T15:21:01,107 ### Run unit tests 2024-03-02T15:21:01,108 1. Fork/Clone repository 2024-03-02T15:21:01,108 2. Install fastapi-authz dependencies, and run `pytest` 2024-03-02T15:21:01,109 ```bash 2024-03-02T15:21:01,110 pip install -r dev_requirements.txt 2024-03-02T15:21:01,110 pip install -r requirements.txt 2024-03-02T15:21:01,110 pytest 2024-03-02T15:21:01,111 ``` 2024-03-02T15:21:01,112 ### Update requirements with pip-tools 2024-03-02T15:21:01,113 ```bash 2024-03-02T15:21:01,113 # update requirements.txt 2024-03-02T15:21:01,114 pip-compile --no-annotate --no-header --rebuild requirements.in 2024-03-02T15:21:01,114 # sync venv 2024-03-02T15:21:01,115 pip-sync 2024-03-02T15:21:01,115 ``` 2024-03-02T15:21:01,116 ### Manually Bump Version 2024-03-02T15:21:01,117 ``` 2024-03-02T15:21:01,118 bumpversion major # major release 2024-03-02T15:21:01,118 or 2024-03-02T15:21:01,119 bumpversion minor # minor release 2024-03-02T15:21:01,119 or 2024-03-02T15:21:01,120 bumpversion patch # hotfix release 2024-03-02T15:21:01,120 ``` 2024-03-02T15:21:01,121 ## Documentation 2024-03-02T15:21:01,122 The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform 2024-03-02T15:21:01,123 what ``action`` on what ``object``. In this plugin, the meanings are: 2024-03-02T15:21:01,124 1. ``subject``: the logged-in user name 2024-03-02T15:21:01,125 2. ``object``: the URL path for the web resource like `dataset1/item1` 2024-03-02T15:21:01,125 3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", " 2024-03-02T15:21:01,126 write-blog" (currently no official support in this middleware) 2024-03-02T15:21:01,127 For how to write authorization policy and other details, please refer 2024-03-02T15:21:01,128 to [the Casbin's documentation](https://casbin.org). 2024-03-02T15:21:01,129 ## Getting Help 2024-03-02T15:21:01,130 - [Casbin](https://casbin.org) 2024-03-02T15:21:01,131 ## License 2024-03-02T15:21:01,132 This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. 2024-03-02T15:21:01,133 running egg_info 2024-03-02T15:21:01,133 creating /tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info 2024-03-02T15:21:01,134 writing /tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/PKG-INFO 2024-03-02T15:21:01,134 writing dependency_links to /tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/dependency_links.txt 2024-03-02T15:21:01,135 writing requirements to /tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/requires.txt 2024-03-02T15:21:01,135 writing top-level names to /tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/top_level.txt 2024-03-02T15:21:01,136 writing manifest file '/tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/SOURCES.txt' 2024-03-02T15:21:01,183 reading manifest file '/tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/SOURCES.txt' 2024-03-02T15:21:01,184 adding license file 'LICENSE' 2024-03-02T15:21:01,186 writing manifest file '/tmp/pip-pip-egg-info-7armsf97/fastapi_authz.egg-info/SOURCES.txt' 2024-03-02T15:21:01,286 Preparing metadata (setup.py): finished with status 'done' 2024-03-02T15:21:01,290 Source in /tmp/pip-wheel-7g0u48nr/fastapi-authz_104c5233e8f74552ac8b2d91a5c72886 has version 0.3.0, which satisfies requirement fastapi-authz==0.3.0 from https://files.pythonhosted.org/packages/70/7f/5356c0736bdbbf3127e053d8893918e9491fc54945c578b32912f2ed1cdb/fastapi-authz-0.3.0.tar.gz 2024-03-02T15:21:01,291 Removed fastapi-authz==0.3.0 from https://files.pythonhosted.org/packages/70/7f/5356c0736bdbbf3127e053d8893918e9491fc54945c578b32912f2ed1cdb/fastapi-authz-0.3.0.tar.gz from build tracker '/tmp/pip-build-tracker-2twnc54r' 2024-03-02T15:21:01,297 Created temporary directory: /tmp/pip-unpack-h2q4f3it 2024-03-02T15:21:01,298 Created temporary directory: /tmp/pip-unpack-s33nbvpo 2024-03-02T15:21:01,303 Building wheels for collected packages: fastapi-authz 2024-03-02T15:21:01,307 Created temporary directory: /tmp/pip-wheel-zkbx377y 2024-03-02T15:21:01,308 Building wheel for fastapi-authz (setup.py): started 2024-03-02T15:21:01,309 Destination directory: /tmp/pip-wheel-zkbx377y 2024-03-02T15:21:01,310 Running command python setup.py bdist_wheel 2024-03-02T15:21:01,717 # fastapi-authz 2024-03-02T15:21:01,718 [![Build Status](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml) 2024-03-02T15:21:01,718 [![Coverage Status](https://coveralls.io/repos/github/pycasbin/fastapi-authz/badge.svg)](https://coveralls.io/github/pycasbin/fastapi-authz) 2024-03-02T15:21:01,719 [![Version](https://img.shields.io/pypi/v/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,719 [![PyPI - Wheel](https://img.shields.io/pypi/wheel/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,720 [![Pyversions](https://img.shields.io/pypi/pyversions/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,720 [![Download](https://img.shields.io/pypi/dm/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-02T15:21:01,721 [![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN) 2024-03-02T15:21:01,722 fastapi-authz is an authorization middleware for [FastAPI](https://fastapi.tiangolo.com/), it's based 2024-03-02T15:21:01,722 on [PyCasbin](https://github.com/casbin/pycasbin). 2024-03-02T15:21:01,724 ## Installation 2024-03-02T15:21:01,725 Install from pip 2024-03-02T15:21:01,726 ```bash 2024-03-02T15:21:01,726 pip install fastapi-authz 2024-03-02T15:21:01,727 ``` 2024-03-02T15:21:01,728 Clone this repo 2024-03-02T15:21:01,729 ```bash 2024-03-02T15:21:01,729 git clone https://github.com/pycasbin/fastapi-authz.git 2024-03-02T15:21:01,730 python setup.py install 2024-03-02T15:21:01,730 ``` 2024-03-02T15:21:01,731 ## Quickstart 2024-03-02T15:21:01,733 This middleware is designed to work with another middleware which implement `AuthenticationMiddleware` interface. 2024-03-02T15:21:01,734 ```python 2024-03-02T15:21:01,734 import base64 2024-03-02T15:21:01,735 import binascii 2024-03-02T15:21:01,736 import casbin 2024-03-02T15:21:01,737 from fastapi import FastAPI 2024-03-02T15:21:01,737 from starlette.authentication import AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials 2024-03-02T15:21:01,738 from starlette.middleware.authentication import AuthenticationMiddleware 2024-03-02T15:21:01,739 from fastapi_authz import CasbinMiddleware 2024-03-02T15:21:01,740 app = FastAPI() 2024-03-02T15:21:01,741 class BasicAuth(AuthenticationBackend): 2024-03-02T15:21:01,742 async def authenticate(self, request): 2024-03-02T15:21:01,743 if "Authorization" not in request.headers: 2024-03-02T15:21:01,743 return None 2024-03-02T15:21:01,744 auth = request.headers["Authorization"] 2024-03-02T15:21:01,745 try: 2024-03-02T15:21:01,745 scheme, credentials = auth.split() 2024-03-02T15:21:01,746 decoded = base64.b64decode(credentials).decode("ascii") 2024-03-02T15:21:01,746 except (ValueError, UnicodeDecodeError, binascii.Error): 2024-03-02T15:21:01,746 raise AuthenticationError("Invalid basic auth credentials") 2024-03-02T15:21:01,747 username, _, password = decoded.partition(":") 2024-03-02T15:21:01,748 return AuthCredentials(["authenticated"]), SimpleUser(username) 2024-03-02T15:21:01,749 enforcer = casbin.Enforcer('../examples/rbac_model.conf', '../examples/rbac_policy.csv') 2024-03-02T15:21:01,750 app.add_middleware(CasbinMiddleware, enforcer=enforcer) 2024-03-02T15:21:01,750 app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) 2024-03-02T15:21:01,752 @app.get('/') 2024-03-02T15:21:01,752 async def index(): 2024-03-02T15:21:01,753 return "If you see this, you have been authenticated." 2024-03-02T15:21:01,754 @app.get('/dataset1/protected') 2024-03-02T15:21:01,755 async def auth_test(): 2024-03-02T15:21:01,755 return "You must be alice to see this." 2024-03-02T15:21:01,756 ``` 2024-03-02T15:21:01,757 - anonymous request 2024-03-02T15:21:01,758 ```bash 2024-03-02T15:21:01,758 curl -i http://127.0.0.1:8000/dataset1/protected 2024-03-02T15:21:01,759 ``` 2024-03-02T15:21:01,760 ```bash 2024-03-02T15:21:01,760 HTTP/1.1 403 Forbidden 2024-03-02T15:21:01,761 date: Mon, 01 Mar 2021 09:00:08 GMT 2024-03-02T15:21:01,761 server: uvicorn 2024-03-02T15:21:01,762 content-length: 11 2024-03-02T15:21:01,762 content-type: application/json 2024-03-02T15:21:01,763 "Forbidden" 2024-03-02T15:21:01,764 ``` 2024-03-02T15:21:01,765 - authenticated request 2024-03-02T15:21:01,766 ```bash 2024-03-02T15:21:01,766 curl -i -u alice:password http://127.0.0.1:8000/dataset1/protected 2024-03-02T15:21:01,767 ``` 2024-03-02T15:21:01,768 ```bash 2024-03-02T15:21:01,768 HTTP/1.1 200 OK 2024-03-02T15:21:01,769 date: Mon, 01 Mar 2021 09:04:54 GMT 2024-03-02T15:21:01,769 server: uvicorn 2024-03-02T15:21:01,770 content-length: 32 2024-03-02T15:21:01,770 content-type: application/json 2024-03-02T15:21:01,771 "You must be alice to see this." 2024-03-02T15:21:01,772 ``` 2024-03-02T15:21:01,773 It used the casbin config from `examples` folder, and you can find this demo in `demo` folder. 2024-03-02T15:21:01,774 You can also view the unit tests to understand this middleware. 2024-03-02T15:21:01,775 Besides, there is another example for `CasbinMiddleware` which is designed to work with JWT authentication. You can find 2024-03-02T15:21:01,776 it in `demo/jwt_test.py`. 2024-03-02T15:21:01,777 ## Development 2024-03-02T15:21:01,778 ### Run unit tests 2024-03-02T15:21:01,779 1. Fork/Clone repository 2024-03-02T15:21:01,780 2. Install fastapi-authz dependencies, and run `pytest` 2024-03-02T15:21:01,781 ```bash 2024-03-02T15:21:01,781 pip install -r dev_requirements.txt 2024-03-02T15:21:01,782 pip install -r requirements.txt 2024-03-02T15:21:01,782 pytest 2024-03-02T15:21:01,783 ``` 2024-03-02T15:21:01,784 ### Update requirements with pip-tools 2024-03-02T15:21:01,785 ```bash 2024-03-02T15:21:01,785 # update requirements.txt 2024-03-02T15:21:01,786 pip-compile --no-annotate --no-header --rebuild requirements.in 2024-03-02T15:21:01,786 # sync venv 2024-03-02T15:21:01,787 pip-sync 2024-03-02T15:21:01,787 ``` 2024-03-02T15:21:01,788 ### Manually Bump Version 2024-03-02T15:21:01,790 ``` 2024-03-02T15:21:01,790 bumpversion major # major release 2024-03-02T15:21:01,791 or 2024-03-02T15:21:01,791 bumpversion minor # minor release 2024-03-02T15:21:01,792 or 2024-03-02T15:21:01,792 bumpversion patch # hotfix release 2024-03-02T15:21:01,793 ``` 2024-03-02T15:21:01,794 ## Documentation 2024-03-02T15:21:01,795 The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform 2024-03-02T15:21:01,795 what ``action`` on what ``object``. In this plugin, the meanings are: 2024-03-02T15:21:01,796 1. ``subject``: the logged-in user name 2024-03-02T15:21:01,797 2. ``object``: the URL path for the web resource like `dataset1/item1` 2024-03-02T15:21:01,797 3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", " 2024-03-02T15:21:01,798 write-blog" (currently no official support in this middleware) 2024-03-02T15:21:01,799 For how to write authorization policy and other details, please refer 2024-03-02T15:21:01,799 to [the Casbin's documentation](https://casbin.org). 2024-03-02T15:21:01,800 ## Getting Help 2024-03-02T15:21:01,801 - [Casbin](https://casbin.org) 2024-03-02T15:21:01,802 ## License 2024-03-02T15:21:01,803 This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. 2024-03-02T15:21:02,159 running bdist_wheel 2024-03-02T15:21:02,291 running build 2024-03-02T15:21:02,292 running build_py 2024-03-02T15:21:02,320 creating build 2024-03-02T15:21:02,320 creating build/lib 2024-03-02T15:21:02,321 creating build/lib/fastapi_authz 2024-03-02T15:21:02,322 copying fastapi_authz/__init__.py -> build/lib/fastapi_authz 2024-03-02T15:21:02,324 copying fastapi_authz/middleware.py -> build/lib/fastapi_authz 2024-03-02T15:21:02,326 running egg_info 2024-03-02T15:21:02,351 writing fastapi_authz.egg-info/PKG-INFO 2024-03-02T15:21:02,354 writing dependency_links to fastapi_authz.egg-info/dependency_links.txt 2024-03-02T15:21:02,356 writing requirements to fastapi_authz.egg-info/requires.txt 2024-03-02T15:21:02,357 writing top-level names to fastapi_authz.egg-info/top_level.txt 2024-03-02T15:21:02,386 reading manifest file 'fastapi_authz.egg-info/SOURCES.txt' 2024-03-02T15:21:02,388 adding license file 'LICENSE' 2024-03-02T15:21:02,390 writing manifest file 'fastapi_authz.egg-info/SOURCES.txt' 2024-03-02T15:21:02,417 /usr/local/lib/python3.11/dist-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated. 2024-03-02T15:21:02,417 !! 2024-03-02T15:21:02,419 ******************************************************************************** 2024-03-02T15:21:02,419 Please avoid running ``setup.py`` directly. 2024-03-02T15:21:02,420 Instead, use pypa/build, pypa/installer or other 2024-03-02T15:21:02,420 standards-based tools. 2024-03-02T15:21:02,421 See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. 2024-03-02T15:21:02,422 ******************************************************************************** 2024-03-02T15:21:02,423 !! 2024-03-02T15:21:02,423 self.initialize_options() 2024-03-02T15:21:02,450 installing to build/bdist.linux-armv7l/wheel 2024-03-02T15:21:02,451 running install 2024-03-02T15:21:02,475 running install_lib 2024-03-02T15:21:02,502 creating build/bdist.linux-armv7l 2024-03-02T15:21:02,503 creating build/bdist.linux-armv7l/wheel 2024-03-02T15:21:02,505 creating build/bdist.linux-armv7l/wheel/fastapi_authz 2024-03-02T15:21:02,506 copying build/lib/fastapi_authz/__init__.py -> build/bdist.linux-armv7l/wheel/fastapi_authz 2024-03-02T15:21:02,508 copying build/lib/fastapi_authz/middleware.py -> build/bdist.linux-armv7l/wheel/fastapi_authz 2024-03-02T15:21:02,510 running install_data 2024-03-02T15:21:02,535 creating build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.data 2024-03-02T15:21:02,536 creating build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.data/data 2024-03-02T15:21:02,537 warning: install_data: setup script did not provide a directory for 'README.md' -- installing right in 'build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.data/data' 2024-03-02T15:21:02,538 copying README.md -> build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.data/data 2024-03-02T15:21:02,539 warning: install_data: setup script did not provide a directory for 'requirements.txt' -- installing right in 'build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.data/data' 2024-03-02T15:21:02,540 copying requirements.txt -> build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.data/data 2024-03-02T15:21:02,541 running install_egg_info 2024-03-02T15:21:02,572 Copying fastapi_authz.egg-info to build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0-py3.11.egg-info 2024-03-02T15:21:02,582 running install_scripts 2024-03-02T15:21:02,596 creating build/bdist.linux-armv7l/wheel/fastapi_authz-0.3.0.dist-info/WHEEL 2024-03-02T15:21:02,599 creating '/tmp/pip-wheel-zkbx377y/fastapi_authz-0.3.0-py3-none-any.whl' and adding 'build/bdist.linux-armv7l/wheel' to it 2024-03-02T15:21:02,601 adding 'fastapi_authz/__init__.py' 2024-03-02T15:21:02,602 adding 'fastapi_authz/middleware.py' 2024-03-02T15:21:02,605 adding 'fastapi_authz-0.3.0.data/data/README.md' 2024-03-02T15:21:02,606 adding 'fastapi_authz-0.3.0.data/data/requirements.txt' 2024-03-02T15:21:02,609 adding 'fastapi_authz-0.3.0.dist-info/LICENSE' 2024-03-02T15:21:02,611 adding 'fastapi_authz-0.3.0.dist-info/METADATA' 2024-03-02T15:21:02,612 adding 'fastapi_authz-0.3.0.dist-info/WHEEL' 2024-03-02T15:21:02,613 adding 'fastapi_authz-0.3.0.dist-info/top_level.txt' 2024-03-02T15:21:02,614 adding 'fastapi_authz-0.3.0.dist-info/RECORD' 2024-03-02T15:21:02,615 removing build/bdist.linux-armv7l/wheel 2024-03-02T15:21:02,721 Building wheel for fastapi-authz (setup.py): finished with status 'done' 2024-03-02T15:21:02,723 Created wheel for fastapi-authz: filename=fastapi_authz-0.3.0-py3-none-any.whl size=10858 sha256=6c671344a9c674b3fbbdf2e367fdabcb71deaf3cad35b0a92369bd1adceb7328 2024-03-02T15:21:02,725 Stored in directory: /tmp/pip-ephem-wheel-cache-ld3vmohg/wheels/d1/69/43/daeea7c11f36b4ae3c5e7e2102092855fe0b824259ea320609 2024-03-02T15:21:02,736 Successfully built fastapi-authz 2024-03-02T15:21:02,739 Removed build tracker: '/tmp/pip-build-tracker-2twnc54r'