2024-03-29T11:55:12,893 Created temporary directory: /tmp/pip-build-tracker-ydbkpilo 2024-03-29T11:55:12,894 Initialized build tracking at /tmp/pip-build-tracker-ydbkpilo 2024-03-29T11:55:12,895 Created build tracker: /tmp/pip-build-tracker-ydbkpilo 2024-03-29T11:55:12,895 Entered build tracker: /tmp/pip-build-tracker-ydbkpilo 2024-03-29T11:55:12,896 Created temporary directory: /tmp/pip-wheel-dof2r793 2024-03-29T11:55:12,899 Created temporary directory: /tmp/pip-ephem-wheel-cache-kxi41v4y 2024-03-29T11:55:12,925 Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple 2024-03-29T11:55:12,929 2 location(s) to search for versions of fastapi-authz: 2024-03-29T11:55:12,929 * https://pypi.org/simple/fastapi-authz/ 2024-03-29T11:55:12,929 * https://www.piwheels.org/simple/fastapi-authz/ 2024-03-29T11:55:12,930 Fetching project page and analyzing links: https://pypi.org/simple/fastapi-authz/ 2024-03-29T11:55:12,931 Getting page https://pypi.org/simple/fastapi-authz/ 2024-03-29T11:55:12,932 Found index url https://pypi.org/simple/ 2024-03-29T11:55:13,154 Fetched page https://pypi.org/simple/fastapi-authz/ as application/vnd.pypi.simple.v1+json 2024-03-29T11:55:13,159 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-29T11:55:13,161 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-29T11:55:13,161 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-29T11:55:13,163 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-29T11:55:13,164 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-29T11:55:13,165 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-29T11:55:13,166 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-29T11:55:13,167 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-29T11:55:13,168 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-29T11:55:13,169 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-29T11:55:13,169 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-29T11:55:13,171 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-29T11:55:13,172 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-29T11:55:13,172 Found link https://files.pythonhosted.org/packages/63/6b/92f34fa9aaf9916cf767a8edd813435b81d15affd05e003670635ff59ca4/fastapi-authz-1.0.0.tar.gz (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 1.0.0 2024-03-29T11:55:13,173 Skipping link: No binaries permitted for fastapi-authz: https://files.pythonhosted.org/packages/b5/eb/c8c8d2e83990461881d0d9ae66ad702426fac05e8715d617d069c1fd3ad8/fastapi_authz-1.0.0-py3-none-any.whl (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-29T11:55:13,174 Fetching project page and analyzing links: https://www.piwheels.org/simple/fastapi-authz/ 2024-03-29T11:55:13,174 Getting page https://www.piwheels.org/simple/fastapi-authz/ 2024-03-29T11:55:13,175 Found index url https://www.piwheels.org/simple/ 2024-03-29T11:55:13,336 Fetched page https://www.piwheels.org/simple/fastapi-authz/ as text/html 2024-03-29T11:55:13,339 Skipping link: No binaries permitted for fastapi-authz: https://www.piwheels.org/simple/fastapi-authz/fastapi_authz-0.3.0-py3-none-any.whl#sha256=6c671344a9c674b3fbbdf2e367fdabcb71deaf3cad35b0a92369bd1adceb7328 (from https://www.piwheels.org/simple/fastapi-authz/) (requires-python:>=3.6) 2024-03-29T11:55:13,340 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-29T11:55:13,341 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-29T11:55:13,341 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-29T11:55:13,342 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-29T11:55:13,342 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-29T11:55:13,343 Skipping link: not a file: https://www.piwheels.org/simple/fastapi-authz/ 2024-03-29T11:55:13,344 Skipping link: not a file: https://pypi.org/simple/fastapi-authz/ 2024-03-29T11:55:13,363 Given no hashes to check 1 links for project 'fastapi-authz': discarding no candidates 2024-03-29T11:55:13,381 Collecting fastapi-authz==1.0.0 2024-03-29T11:55:13,383 Created temporary directory: /tmp/pip-unpack-9_waxiar 2024-03-29T11:55:13,592 Downloading fastapi-authz-1.0.0.tar.gz (8.9 kB) 2024-03-29T11:55:13,629 Added fastapi-authz==1.0.0 from https://files.pythonhosted.org/packages/63/6b/92f34fa9aaf9916cf767a8edd813435b81d15affd05e003670635ff59ca4/fastapi-authz-1.0.0.tar.gz to build tracker '/tmp/pip-build-tracker-ydbkpilo' 2024-03-29T11:55:13,631 Running setup.py (path:/tmp/pip-wheel-dof2r793/fastapi-authz_f82243692f6f4b20b9d8008225313dfb/setup.py) egg_info for package fastapi-authz 2024-03-29T11:55:13,632 Created temporary directory: /tmp/pip-pip-egg-info-6lgq8z43 2024-03-29T11:55:13,633 Preparing metadata (setup.py): started 2024-03-29T11:55:13,634 Running command python setup.py egg_info 2024-03-29T11:55:14,730 # fastapi-authz 2024-03-29T11:55:14,731 [![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-29T11:55:14,731 [![Coverage Status](https://coveralls.io/repos/github/pycasbin/fastapi-authz/badge.svg)](https://coveralls.io/github/pycasbin/fastapi-authz) 2024-03-29T11:55:14,732 [![Version](https://img.shields.io/pypi/v/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:14,732 [![PyPI - Wheel](https://img.shields.io/pypi/wheel/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:14,733 [![Pyversions](https://img.shields.io/pypi/pyversions/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:14,733 [![Download](https://img.shields.io/pypi/dm/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:14,734 [![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN) 2024-03-29T11:55:14,735 fastapi-authz is an authorization middleware for [FastAPI](https://fastapi.tiangolo.com/), it's based 2024-03-29T11:55:14,735 on [PyCasbin](https://github.com/casbin/pycasbin). 2024-03-29T11:55:14,736 ## Installation 2024-03-29T11:55:14,737 Install from pip 2024-03-29T11:55:14,738 ```bash 2024-03-29T11:55:14,739 pip install fastapi-authz 2024-03-29T11:55:14,740 ``` 2024-03-29T11:55:14,741 Clone this repo 2024-03-29T11:55:14,742 ```bash 2024-03-29T11:55:14,742 git clone https://github.com/pycasbin/fastapi-authz.git 2024-03-29T11:55:14,743 python setup.py install 2024-03-29T11:55:14,743 ``` 2024-03-29T11:55:14,745 ## Quickstart 2024-03-29T11:55:14,746 This middleware is designed to work with another middleware which implement `AuthenticationMiddleware` interface. 2024-03-29T11:55:14,747 ```python 2024-03-29T11:55:14,748 import base64 2024-03-29T11:55:14,748 import binascii 2024-03-29T11:55:14,750 import casbin 2024-03-29T11:55:14,751 from fastapi import FastAPI 2024-03-29T11:55:14,751 from starlette.authentication import AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials 2024-03-29T11:55:14,752 from starlette.middleware.authentication import AuthenticationMiddleware 2024-03-29T11:55:14,753 from fastapi_authz import CasbinMiddleware 2024-03-29T11:55:14,754 app = FastAPI() 2024-03-29T11:55:14,756 class BasicAuth(AuthenticationBackend): 2024-03-29T11:55:14,756 async def authenticate(self, request): 2024-03-29T11:55:14,757 if "Authorization" not in request.headers: 2024-03-29T11:55:14,757 return None 2024-03-29T11:55:14,758 auth = request.headers["Authorization"] 2024-03-29T11:55:14,759 try: 2024-03-29T11:55:14,760 scheme, credentials = auth.split() 2024-03-29T11:55:14,760 decoded = base64.b64decode(credentials).decode("ascii") 2024-03-29T11:55:14,761 except (ValueError, UnicodeDecodeError, binascii.Error): 2024-03-29T11:55:14,761 raise AuthenticationError("Invalid basic auth credentials") 2024-03-29T11:55:14,762 username, _, password = decoded.partition(":") 2024-03-29T11:55:14,763 return AuthCredentials(["authenticated"]), SimpleUser(username) 2024-03-29T11:55:14,764 enforcer = casbin.Enforcer('../examples/rbac_model.conf', '../examples/rbac_policy.csv') 2024-03-29T11:55:14,766 app.add_middleware(CasbinMiddleware, enforcer=enforcer) 2024-03-29T11:55:14,766 app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) 2024-03-29T11:55:14,768 @app.get('/') 2024-03-29T11:55:14,768 async def index(): 2024-03-29T11:55:14,768 return "If you see this, you have been authenticated." 2024-03-29T11:55:14,770 @app.get('/dataset1/protected') 2024-03-29T11:55:14,770 async def auth_test(): 2024-03-29T11:55:14,771 return "You must be alice to see this." 2024-03-29T11:55:14,771 ``` 2024-03-29T11:55:14,772 - anonymous request 2024-03-29T11:55:14,773 ```bash 2024-03-29T11:55:14,773 curl -i http://127.0.0.1:8000/dataset1/protected 2024-03-29T11:55:14,774 ``` 2024-03-29T11:55:14,775 ```bash 2024-03-29T11:55:14,775 HTTP/1.1 403 Forbidden 2024-03-29T11:55:14,776 date: Mon, 01 Mar 2021 09:00:08 GMT 2024-03-29T11:55:14,776 server: uvicorn 2024-03-29T11:55:14,776 content-length: 11 2024-03-29T11:55:14,777 content-type: application/json 2024-03-29T11:55:14,778 "Forbidden" 2024-03-29T11:55:14,778 ``` 2024-03-29T11:55:14,779 - authenticated request 2024-03-29T11:55:14,780 ```bash 2024-03-29T11:55:14,781 curl -i -u alice:password http://127.0.0.1:8000/dataset1/protected 2024-03-29T11:55:14,781 ``` 2024-03-29T11:55:14,782 ```bash 2024-03-29T11:55:14,782 HTTP/1.1 200 OK 2024-03-29T11:55:14,783 date: Mon, 01 Mar 2021 09:04:54 GMT 2024-03-29T11:55:14,783 server: uvicorn 2024-03-29T11:55:14,784 content-length: 32 2024-03-29T11:55:14,784 content-type: application/json 2024-03-29T11:55:14,785 "You must be alice to see this." 2024-03-29T11:55:14,786 ``` 2024-03-29T11:55:14,787 It used the casbin config from `examples` folder, and you can find this demo in `demo` folder. 2024-03-29T11:55:14,787 You can also view the unit tests to understand this middleware. 2024-03-29T11:55:14,789 Besides, there is another example for `CasbinMiddleware` which is designed to work with JWT authentication. You can find 2024-03-29T11:55:14,789 it in `demo/jwt_test.py`. 2024-03-29T11:55:14,790 ## Development 2024-03-29T11:55:14,791 ### Run unit tests 2024-03-29T11:55:14,792 1. Fork/Clone repository 2024-03-29T11:55:14,793 2. Install fastapi-authz dependencies, and run `pytest` 2024-03-29T11:55:14,794 ```bash 2024-03-29T11:55:14,795 pip install -r dev_requirements.txt 2024-03-29T11:55:14,795 pip install -r requirements.txt 2024-03-29T11:55:14,796 pytest 2024-03-29T11:55:14,796 ``` 2024-03-29T11:55:14,798 ### Update requirements with pip-tools 2024-03-29T11:55:14,799 ```bash 2024-03-29T11:55:14,799 # update requirements.txt 2024-03-29T11:55:14,800 pip-compile --no-annotate --no-header --rebuild requirements.in 2024-03-29T11:55:14,801 # sync venv 2024-03-29T11:55:14,801 pip-sync 2024-03-29T11:55:14,802 ``` 2024-03-29T11:55:14,803 ### Manually Bump Version 2024-03-29T11:55:14,804 ``` 2024-03-29T11:55:14,805 bumpversion major # major release 2024-03-29T11:55:14,805 or 2024-03-29T11:55:14,806 bumpversion minor # minor release 2024-03-29T11:55:14,807 or 2024-03-29T11:55:14,807 bumpversion patch # hotfix release 2024-03-29T11:55:14,808 ``` 2024-03-29T11:55:14,809 ## Documentation 2024-03-29T11:55:14,810 The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform 2024-03-29T11:55:14,811 what ``action`` on what ``object``. In this plugin, the meanings are: 2024-03-29T11:55:14,812 1. ``subject``: the logged-in user name 2024-03-29T11:55:14,812 2. ``object``: the URL path for the web resource like `dataset1/item1` 2024-03-29T11:55:14,813 3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", " 2024-03-29T11:55:14,813 write-blog" (currently no official support in this middleware) 2024-03-29T11:55:14,815 For how to write authorization policy and other details, please refer 2024-03-29T11:55:14,815 to [the Casbin's documentation](https://casbin.org). 2024-03-29T11:55:14,816 ## Getting Help 2024-03-29T11:55:14,817 - [Casbin](https://casbin.org) 2024-03-29T11:55:14,819 ## License 2024-03-29T11:55:14,819 This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. 2024-03-29T11:55:14,820 running egg_info 2024-03-29T11:55:14,821 creating /tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info 2024-03-29T11:55:14,821 writing /tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/PKG-INFO 2024-03-29T11:55:14,822 writing dependency_links to /tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/dependency_links.txt 2024-03-29T11:55:14,822 writing requirements to /tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/requires.txt 2024-03-29T11:55:14,822 writing top-level names to /tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/top_level.txt 2024-03-29T11:55:14,823 writing manifest file '/tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/SOURCES.txt' 2024-03-29T11:55:14,856 reading manifest file '/tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/SOURCES.txt' 2024-03-29T11:55:14,857 adding license file 'LICENSE' 2024-03-29T11:55:14,859 writing manifest file '/tmp/pip-pip-egg-info-6lgq8z43/fastapi_authz.egg-info/SOURCES.txt' 2024-03-29T11:55:14,968 Preparing metadata (setup.py): finished with status 'done' 2024-03-29T11:55:14,972 Source in /tmp/pip-wheel-dof2r793/fastapi-authz_f82243692f6f4b20b9d8008225313dfb has version 1.0.0, which satisfies requirement fastapi-authz==1.0.0 from https://files.pythonhosted.org/packages/63/6b/92f34fa9aaf9916cf767a8edd813435b81d15affd05e003670635ff59ca4/fastapi-authz-1.0.0.tar.gz 2024-03-29T11:55:14,973 Removed fastapi-authz==1.0.0 from https://files.pythonhosted.org/packages/63/6b/92f34fa9aaf9916cf767a8edd813435b81d15affd05e003670635ff59ca4/fastapi-authz-1.0.0.tar.gz from build tracker '/tmp/pip-build-tracker-ydbkpilo' 2024-03-29T11:55:14,979 Created temporary directory: /tmp/pip-unpack-s1nuriy7 2024-03-29T11:55:14,980 Created temporary directory: /tmp/pip-unpack-j9ra293b 2024-03-29T11:55:14,985 Building wheels for collected packages: fastapi-authz 2024-03-29T11:55:14,989 Created temporary directory: /tmp/pip-wheel-5300oa6v 2024-03-29T11:55:14,990 Building wheel for fastapi-authz (setup.py): started 2024-03-29T11:55:14,991 Destination directory: /tmp/pip-wheel-5300oa6v 2024-03-29T11:55:14,992 Running command python setup.py bdist_wheel 2024-03-29T11:55:15,398 # fastapi-authz 2024-03-29T11:55:15,399 [![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-29T11:55:15,399 [![Coverage Status](https://coveralls.io/repos/github/pycasbin/fastapi-authz/badge.svg)](https://coveralls.io/github/pycasbin/fastapi-authz) 2024-03-29T11:55:15,400 [![Version](https://img.shields.io/pypi/v/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:15,401 [![PyPI - Wheel](https://img.shields.io/pypi/wheel/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:15,401 [![Pyversions](https://img.shields.io/pypi/pyversions/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:15,402 [![Download](https://img.shields.io/pypi/dm/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2024-03-29T11:55:15,402 [![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN) 2024-03-29T11:55:15,404 fastapi-authz is an authorization middleware for [FastAPI](https://fastapi.tiangolo.com/), it's based 2024-03-29T11:55:15,404 on [PyCasbin](https://github.com/casbin/pycasbin). 2024-03-29T11:55:15,406 ## Installation 2024-03-29T11:55:15,407 Install from pip 2024-03-29T11:55:15,408 ```bash 2024-03-29T11:55:15,409 pip install fastapi-authz 2024-03-29T11:55:15,409 ``` 2024-03-29T11:55:15,411 Clone this repo 2024-03-29T11:55:15,412 ```bash 2024-03-29T11:55:15,412 git clone https://github.com/pycasbin/fastapi-authz.git 2024-03-29T11:55:15,413 python setup.py install 2024-03-29T11:55:15,414 ``` 2024-03-29T11:55:15,415 ## Quickstart 2024-03-29T11:55:15,416 This middleware is designed to work with another middleware which implement `AuthenticationMiddleware` interface. 2024-03-29T11:55:15,417 ```python 2024-03-29T11:55:15,417 import base64 2024-03-29T11:55:15,418 import binascii 2024-03-29T11:55:15,419 import casbin 2024-03-29T11:55:15,420 from fastapi import FastAPI 2024-03-29T11:55:15,420 from starlette.authentication import AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials 2024-03-29T11:55:15,421 from starlette.middleware.authentication import AuthenticationMiddleware 2024-03-29T11:55:15,422 from fastapi_authz import CasbinMiddleware 2024-03-29T11:55:15,423 app = FastAPI() 2024-03-29T11:55:15,425 class BasicAuth(AuthenticationBackend): 2024-03-29T11:55:15,425 async def authenticate(self, request): 2024-03-29T11:55:15,426 if "Authorization" not in request.headers: 2024-03-29T11:55:15,426 return None 2024-03-29T11:55:15,427 auth = request.headers["Authorization"] 2024-03-29T11:55:15,427 try: 2024-03-29T11:55:15,428 scheme, credentials = auth.split() 2024-03-29T11:55:15,428 decoded = base64.b64decode(credentials).decode("ascii") 2024-03-29T11:55:15,429 except (ValueError, UnicodeDecodeError, binascii.Error): 2024-03-29T11:55:15,429 raise AuthenticationError("Invalid basic auth credentials") 2024-03-29T11:55:15,430 username, _, password = decoded.partition(":") 2024-03-29T11:55:15,431 return AuthCredentials(["authenticated"]), SimpleUser(username) 2024-03-29T11:55:15,432 enforcer = casbin.Enforcer('../examples/rbac_model.conf', '../examples/rbac_policy.csv') 2024-03-29T11:55:15,433 app.add_middleware(CasbinMiddleware, enforcer=enforcer) 2024-03-29T11:55:15,434 app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) 2024-03-29T11:55:15,436 @app.get('/') 2024-03-29T11:55:15,436 async def index(): 2024-03-29T11:55:15,437 return "If you see this, you have been authenticated." 2024-03-29T11:55:15,439 @app.get('/dataset1/protected') 2024-03-29T11:55:15,439 async def auth_test(): 2024-03-29T11:55:15,440 return "You must be alice to see this." 2024-03-29T11:55:15,441 ``` 2024-03-29T11:55:15,442 - anonymous request 2024-03-29T11:55:15,443 ```bash 2024-03-29T11:55:15,444 curl -i http://127.0.0.1:8000/dataset1/protected 2024-03-29T11:55:15,444 ``` 2024-03-29T11:55:15,446 ```bash 2024-03-29T11:55:15,446 HTTP/1.1 403 Forbidden 2024-03-29T11:55:15,447 date: Mon, 01 Mar 2021 09:00:08 GMT 2024-03-29T11:55:15,448 server: uvicorn 2024-03-29T11:55:15,448 content-length: 11 2024-03-29T11:55:15,449 content-type: application/json 2024-03-29T11:55:15,450 "Forbidden" 2024-03-29T11:55:15,450 ``` 2024-03-29T11:55:15,452 - authenticated request 2024-03-29T11:55:15,453 ```bash 2024-03-29T11:55:15,453 curl -i -u alice:password http://127.0.0.1:8000/dataset1/protected 2024-03-29T11:55:15,454 ``` 2024-03-29T11:55:15,455 ```bash 2024-03-29T11:55:15,456 HTTP/1.1 200 OK 2024-03-29T11:55:15,456 date: Mon, 01 Mar 2021 09:04:54 GMT 2024-03-29T11:55:15,457 server: uvicorn 2024-03-29T11:55:15,457 content-length: 32 2024-03-29T11:55:15,458 content-type: application/json 2024-03-29T11:55:15,459 "You must be alice to see this." 2024-03-29T11:55:15,460 ``` 2024-03-29T11:55:15,461 It used the casbin config from `examples` folder, and you can find this demo in `demo` folder. 2024-03-29T11:55:15,462 You can also view the unit tests to understand this middleware. 2024-03-29T11:55:15,463 Besides, there is another example for `CasbinMiddleware` which is designed to work with JWT authentication. You can find 2024-03-29T11:55:15,463 it in `demo/jwt_test.py`. 2024-03-29T11:55:15,465 ## Development 2024-03-29T11:55:15,466 ### Run unit tests 2024-03-29T11:55:15,467 1. Fork/Clone repository 2024-03-29T11:55:15,468 2. Install fastapi-authz dependencies, and run `pytest` 2024-03-29T11:55:15,469 ```bash 2024-03-29T11:55:15,469 pip install -r dev_requirements.txt 2024-03-29T11:55:15,470 pip install -r requirements.txt 2024-03-29T11:55:15,470 pytest 2024-03-29T11:55:15,471 ``` 2024-03-29T11:55:15,472 ### Update requirements with pip-tools 2024-03-29T11:55:15,472 ```bash 2024-03-29T11:55:15,473 # update requirements.txt 2024-03-29T11:55:15,473 pip-compile --no-annotate --no-header --rebuild requirements.in 2024-03-29T11:55:15,474 # sync venv 2024-03-29T11:55:15,474 pip-sync 2024-03-29T11:55:15,475 ``` 2024-03-29T11:55:15,476 ### Manually Bump Version 2024-03-29T11:55:15,476 ``` 2024-03-29T11:55:15,477 bumpversion major # major release 2024-03-29T11:55:15,477 or 2024-03-29T11:55:15,478 bumpversion minor # minor release 2024-03-29T11:55:15,478 or 2024-03-29T11:55:15,479 bumpversion patch # hotfix release 2024-03-29T11:55:15,479 ``` 2024-03-29T11:55:15,480 ## Documentation 2024-03-29T11:55:15,481 The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform 2024-03-29T11:55:15,482 what ``action`` on what ``object``. In this plugin, the meanings are: 2024-03-29T11:55:15,483 1. ``subject``: the logged-in user name 2024-03-29T11:55:15,483 2. ``object``: the URL path for the web resource like `dataset1/item1` 2024-03-29T11:55:15,484 3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", " 2024-03-29T11:55:15,484 write-blog" (currently no official support in this middleware) 2024-03-29T11:55:15,485 For how to write authorization policy and other details, please refer 2024-03-29T11:55:15,486 to [the Casbin's documentation](https://casbin.org). 2024-03-29T11:55:15,487 ## Getting Help 2024-03-29T11:55:15,488 - [Casbin](https://casbin.org) 2024-03-29T11:55:15,489 ## License 2024-03-29T11:55:15,490 This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. 2024-03-29T11:55:16,036 running bdist_wheel 2024-03-29T11:55:16,132 running build 2024-03-29T11:55:16,133 running build_py 2024-03-29T11:55:16,160 creating build 2024-03-29T11:55:16,161 creating build/lib 2024-03-29T11:55:16,162 creating build/lib/fastapi_authz 2024-03-29T11:55:16,163 copying fastapi_authz/__init__.py -> build/lib/fastapi_authz 2024-03-29T11:55:16,165 copying fastapi_authz/middleware.py -> build/lib/fastapi_authz 2024-03-29T11:55:16,166 running egg_info 2024-03-29T11:55:16,223 writing fastapi_authz.egg-info/PKG-INFO 2024-03-29T11:55:16,227 writing dependency_links to fastapi_authz.egg-info/dependency_links.txt 2024-03-29T11:55:16,229 writing requirements to fastapi_authz.egg-info/requires.txt 2024-03-29T11:55:16,230 writing top-level names to fastapi_authz.egg-info/top_level.txt 2024-03-29T11:55:16,257 reading manifest file 'fastapi_authz.egg-info/SOURCES.txt' 2024-03-29T11:55:16,259 adding license file 'LICENSE' 2024-03-29T11:55:16,261 writing manifest file 'fastapi_authz.egg-info/SOURCES.txt' 2024-03-29T11:55:16,288 /usr/local/lib/python3.11/dist-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated. 2024-03-29T11:55:16,288 !! 2024-03-29T11:55:16,290 ******************************************************************************** 2024-03-29T11:55:16,290 Please avoid running ``setup.py`` directly. 2024-03-29T11:55:16,291 Instead, use pypa/build, pypa/installer or other 2024-03-29T11:55:16,292 standards-based tools. 2024-03-29T11:55:16,293 See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. 2024-03-29T11:55:16,294 ******************************************************************************** 2024-03-29T11:55:16,295 !! 2024-03-29T11:55:16,296 self.initialize_options() 2024-03-29T11:55:16,315 installing to build/bdist.linux-armv7l/wheel 2024-03-29T11:55:16,315 running install 2024-03-29T11:55:16,339 running install_lib 2024-03-29T11:55:16,364 creating build/bdist.linux-armv7l 2024-03-29T11:55:16,364 creating build/bdist.linux-armv7l/wheel 2024-03-29T11:55:16,366 creating build/bdist.linux-armv7l/wheel/fastapi_authz 2024-03-29T11:55:16,367 copying build/lib/fastapi_authz/__init__.py -> build/bdist.linux-armv7l/wheel/fastapi_authz 2024-03-29T11:55:16,369 copying build/lib/fastapi_authz/middleware.py -> build/bdist.linux-armv7l/wheel/fastapi_authz 2024-03-29T11:55:16,370 running install_data 2024-03-29T11:55:16,396 creating build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.data 2024-03-29T11:55:16,396 creating build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.data/data 2024-03-29T11:55:16,397 warning: install_data: setup script did not provide a directory for 'README.md' -- installing right in 'build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.data/data' 2024-03-29T11:55:16,398 copying README.md -> build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.data/data 2024-03-29T11:55:16,399 warning: install_data: setup script did not provide a directory for 'requirements.txt' -- installing right in 'build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.data/data' 2024-03-29T11:55:16,400 copying requirements.txt -> build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.data/data 2024-03-29T11:55:16,401 running install_egg_info 2024-03-29T11:55:16,433 Copying fastapi_authz.egg-info to build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0-py3.11.egg-info 2024-03-29T11:55:16,443 running install_scripts 2024-03-29T11:55:16,457 creating build/bdist.linux-armv7l/wheel/fastapi_authz-1.0.0.dist-info/WHEEL 2024-03-29T11:55:16,460 creating '/tmp/pip-wheel-5300oa6v/fastapi_authz-1.0.0-py3-none-any.whl' and adding 'build/bdist.linux-armv7l/wheel' to it 2024-03-29T11:55:16,462 adding 'fastapi_authz/__init__.py' 2024-03-29T11:55:16,463 adding 'fastapi_authz/middleware.py' 2024-03-29T11:55:16,465 adding 'fastapi_authz-1.0.0.data/data/README.md' 2024-03-29T11:55:16,467 adding 'fastapi_authz-1.0.0.data/data/requirements.txt' 2024-03-29T11:55:16,470 adding 'fastapi_authz-1.0.0.dist-info/LICENSE' 2024-03-29T11:55:16,471 adding 'fastapi_authz-1.0.0.dist-info/METADATA' 2024-03-29T11:55:16,472 adding 'fastapi_authz-1.0.0.dist-info/WHEEL' 2024-03-29T11:55:16,473 adding 'fastapi_authz-1.0.0.dist-info/top_level.txt' 2024-03-29T11:55:16,474 adding 'fastapi_authz-1.0.0.dist-info/RECORD' 2024-03-29T11:55:16,476 removing build/bdist.linux-armv7l/wheel 2024-03-29T11:55:16,591 Building wheel for fastapi-authz (setup.py): finished with status 'done' 2024-03-29T11:55:16,594 Created wheel for fastapi-authz: filename=fastapi_authz-1.0.0-py3-none-any.whl size=10815 sha256=13c16fd364b95a788fd8a301b2cbc0b119c49c062b93f81e9eb2c8f1584eb396 2024-03-29T11:55:16,595 Stored in directory: /tmp/pip-ephem-wheel-cache-kxi41v4y/wheels/50/40/0d/73b0d3c2d97be77d379d75a5cf271212b256dc89a0baf3a1c6 2024-03-29T11:55:16,820 Successfully built fastapi-authz 2024-03-29T11:55:16,823 Removed build tracker: '/tmp/pip-build-tracker-ydbkpilo'