2021-12-01T10:32:42,366 Created temporary directory: /tmp/pip-ephem-wheel-cache-bp_c1iv8 2021-12-01T10:32:42,369 Created temporary directory: /tmp/pip-req-tracker-s19bf9xm 2021-12-01T10:32:42,369 Initialized build tracking at /tmp/pip-req-tracker-s19bf9xm 2021-12-01T10:32:42,370 Created build tracker: /tmp/pip-req-tracker-s19bf9xm 2021-12-01T10:32:42,370 Entered build tracker: /tmp/pip-req-tracker-s19bf9xm 2021-12-01T10:32:42,370 Created temporary directory: /tmp/pip-wheel-emgqt68u 2021-12-01T10:32:42,400 Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple 2021-12-01T10:32:42,403 2 location(s) to search for versions of fastapi-authz: 2021-12-01T10:32:42,403 * https://pypi.org/simple/fastapi-authz/ 2021-12-01T10:32:42,403 * https://www.piwheels.org/simple/fastapi-authz/ 2021-12-01T10:32:42,403 Fetching project page and analyzing links: https://pypi.org/simple/fastapi-authz/ 2021-12-01T10:32:42,404 Getting page https://pypi.org/simple/fastapi-authz/ 2021-12-01T10:32:42,405 Found index url https://pypi.org/simple 2021-12-01T10:32:42,498 Found link https://files.pythonhosted.org/packages/8b/9b/9f6f2758e16a9468b73dffcda68e2e9d32e14a13d772c14cd94a904530fc/fastapi-authz-0.0.2.tar.gz#sha256=d44dd72ba1c9cbf725d44269d1c648e1c46a19a461accd251b4f8dc01b4f7a09 (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.2 2021-12-01T10:32:42,498 Found link https://files.pythonhosted.org/packages/4f/e3/8ecf3c892289933710e40acbdab5f90c2497fb710b340c827ae8c30882f0/fastapi-authz-0.0.3.tar.gz#sha256=fcfe5ed7f7634dc9d89cfb673e603174eb6da3382bad02dc1c08a51edd2d6474 (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.3 2021-12-01T10:32:42,499 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#sha256=754216ce9cec748afc138ea225e783425741382bf14644f631b8c469049d28ff (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2021-12-01T10:32:42,499 Found link https://files.pythonhosted.org/packages/b9/f6/1ec5d2b7ef5ee8cd683f68d5bdce8a14385829de8e1b8678cdbba2806519/fastapi-authz-0.0.4.tar.gz#sha256=4158b408d9ab64a25aac88fbeae224d27c52476b08709366917d417df660274d (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.4 2021-12-01T10:32:42,500 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#sha256=0838320254118b500b6315181195fb1a70ebcbe9f5048a86a766108bbbf948a9 (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2021-12-01T10:32:42,500 Found link https://files.pythonhosted.org/packages/6f/bf/8f100730f093857d2d449fda70ab2edd64cdbc81f315351486ce8a5932cb/fastapi-authz-0.0.5.tar.gz#sha256=69759886fdecda882a8ba2263ae98809865c319a2acfd26e939ca5d974bb5723 (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.0.5 2021-12-01T10:32:42,500 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#sha256=ea503184efc7dfdf37c08ad7a1e130692810883891a7c76e2865a69482326c35 (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2021-12-01T10:32:42,501 Found link https://files.pythonhosted.org/packages/ef/fb/11538a3ff6c41077ceba2eac1c09a22b33a2fd486c0b85d6199537c31f61/fastapi-authz-0.1.0.tar.gz#sha256=52c51184b60bee25501178b419b23c31007b6b4b6ba8940908d7f9a252f669de (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6), version: 0.1.0 2021-12-01T10:32:42,501 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#sha256=7dc70d67f97a78fc795a2f62797d9e04aaa7bf8786fbb696d4487dac9cc19b35 (from https://pypi.org/simple/fastapi-authz/) (requires-python:>=3.6) 2021-12-01T10:32:42,502 Fetching project page and analyzing links: https://www.piwheels.org/simple/fastapi-authz/ 2021-12-01T10:32:42,502 Getting page https://www.piwheels.org/simple/fastapi-authz/ 2021-12-01T10:32:42,503 Found index url https://www.piwheels.org/simple 2021-12-01T10:32:42,679 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) 2021-12-01T10:32:42,679 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) 2021-12-01T10:32:42,679 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) 2021-12-01T10:32:42,679 Skipping link: not a file: https://www.piwheels.org/simple/fastapi-authz/ 2021-12-01T10:32:42,680 Skipping link: not a file: https://pypi.org/simple/fastapi-authz/ 2021-12-01T10:32:42,695 Given no hashes to check 1 links for project 'fastapi-authz': discarding no candidates 2021-12-01T10:32:42,708 Collecting fastapi-authz==0.1.0 2021-12-01T10:32:42,710 Created temporary directory: /tmp/pip-unpack-w7xb5mut 2021-12-01T10:32:42,904 Downloading fastapi-authz-0.1.0.tar.gz (8.3 kB) 2021-12-01T10:32:42,956 Added fastapi-authz==0.1.0 from https://files.pythonhosted.org/packages/ef/fb/11538a3ff6c41077ceba2eac1c09a22b33a2fd486c0b85d6199537c31f61/fastapi-authz-0.1.0.tar.gz#sha256=52c51184b60bee25501178b419b23c31007b6b4b6ba8940908d7f9a252f669de to build tracker '/tmp/pip-req-tracker-s19bf9xm' 2021-12-01T10:32:42,957 Running setup.py (path:/tmp/pip-wheel-emgqt68u/fastapi-authz_86fbe83605ad4aa4b2d56a3fc60570f1/setup.py) egg_info for package fastapi-authz 2021-12-01T10:32:42,957 Created temporary directory: /tmp/pip-pip-egg-info-tarmkhoe 2021-12-01T10:32:42,958 Preparing metadata (setup.py): started 2021-12-01T10:32:42,958 Running command python setup.py egg_info 2021-12-01T10:32:43,974 # fastapi-authz 2021-12-01T10:32:43,974 [![Build Status](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml) 2021-12-01T10:32:43,975 [![Coverage Status](https://coveralls.io/repos/github/pycasbin/fastapi-authz/badge.svg)](https://coveralls.io/github/pycasbin/fastapi-authz) 2021-12-01T10:32:43,975 [![Version](https://img.shields.io/pypi/v/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:43,975 [![PyPI - Wheel](https://img.shields.io/pypi/wheel/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:43,975 [![Pyversions](https://img.shields.io/pypi/pyversions/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:43,975 [![Download](https://img.shields.io/pypi/dm/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:43,975 [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/casbin/lobby) 2021-12-01T10:32:43,976 fastapi-authz is an authorization middleware for [FastAPI](https://fastapi.tiangolo.com/), it's based 2021-12-01T10:32:43,976 on [PyCasbin](https://github.com/casbin/pycasbin). 2021-12-01T10:32:43,976 ## Installation 2021-12-01T10:32:43,977 Install from pip 2021-12-01T10:32:43,977 ```bash 2021-12-01T10:32:43,977 pip install fastapi-authz 2021-12-01T10:32:43,977 ``` 2021-12-01T10:32:43,977 Clone this repo 2021-12-01T10:32:43,978 ```bash 2021-12-01T10:32:43,978 git clone https://github.com/pycasbin/fastapi-authz.git 2021-12-01T10:32:43,978 python setup.py install 2021-12-01T10:32:43,978 ``` 2021-12-01T10:32:43,978 ## Quickstart 2021-12-01T10:32:43,979 This middleware is designed to work with another middleware which implement `AuthenticationMiddleware` interface. 2021-12-01T10:32:43,979 ```python 2021-12-01T10:32:43,979 import base64 2021-12-01T10:32:43,980 import binascii 2021-12-01T10:32:43,980 import casbin 2021-12-01T10:32:43,980 from fastapi import FastAPI 2021-12-01T10:32:43,980 from starlette.authentication import AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials 2021-12-01T10:32:43,980 from starlette.middleware.authentication import AuthenticationMiddleware 2021-12-01T10:32:43,981 from fastapi_authz import CasbinMiddleware 2021-12-01T10:32:43,981 app = FastAPI() 2021-12-01T10:32:43,982 class BasicAuth(AuthenticationBackend): 2021-12-01T10:32:43,982 async def authenticate(self, request): 2021-12-01T10:32:43,982 if "Authorization" not in request.headers: 2021-12-01T10:32:43,982 return None 2021-12-01T10:32:43,982 auth = request.headers["Authorization"] 2021-12-01T10:32:43,982 try: 2021-12-01T10:32:43,983 scheme, credentials = auth.split() 2021-12-01T10:32:43,983 decoded = base64.b64decode(credentials).decode("ascii") 2021-12-01T10:32:43,983 except (ValueError, UnicodeDecodeError, binascii.Error): 2021-12-01T10:32:43,983 raise AuthenticationError("Invalid basic auth credentials") 2021-12-01T10:32:43,983 username, _, password = decoded.partition(":") 2021-12-01T10:32:43,983 return AuthCredentials(["authenticated"]), SimpleUser(username) 2021-12-01T10:32:43,984 enforcer = casbin.Enforcer('../examples/rbac_model.conf', '../examples/rbac_policy.csv') 2021-12-01T10:32:43,984 app.add_middleware(CasbinMiddleware, enforcer=enforcer) 2021-12-01T10:32:43,984 app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) 2021-12-01T10:32:43,985 @app.get('/') 2021-12-01T10:32:43,985 async def index(): 2021-12-01T10:32:43,985 return "If you see this, you have been authenticated." 2021-12-01T10:32:43,985 @app.get('/dataset1/protected') 2021-12-01T10:32:43,986 async def auth_test(): 2021-12-01T10:32:43,986 return "You must be alice to see this." 2021-12-01T10:32:43,986 ``` 2021-12-01T10:32:43,986 - anonymous request 2021-12-01T10:32:43,986 ```bash 2021-12-01T10:32:43,987 curl -i http://127.0.0.1:8000/dataset1/protected 2021-12-01T10:32:43,987 ``` 2021-12-01T10:32:43,987 ```bash 2021-12-01T10:32:43,987 HTTP/1.1 403 Forbidden 2021-12-01T10:32:43,987 date: Mon, 01 Mar 2021 09:00:08 GMT 2021-12-01T10:32:43,987 server: uvicorn 2021-12-01T10:32:43,988 content-length: 11 2021-12-01T10:32:43,988 content-type: application/json 2021-12-01T10:32:43,988 "Forbidden" 2021-12-01T10:32:43,988 ``` 2021-12-01T10:32:43,988 - authenticated request 2021-12-01T10:32:43,989 ```bash 2021-12-01T10:32:43,989 curl -i -u alice:password http://127.0.0.1:8000/dataset1/protected 2021-12-01T10:32:43,989 ``` 2021-12-01T10:32:43,989 ```bash 2021-12-01T10:32:43,989 HTTP/1.1 200 OK 2021-12-01T10:32:43,989 date: Mon, 01 Mar 2021 09:04:54 GMT 2021-12-01T10:32:43,990 server: uvicorn 2021-12-01T10:32:43,990 content-length: 32 2021-12-01T10:32:43,990 content-type: application/json 2021-12-01T10:32:43,990 "You must be alice to see this." 2021-12-01T10:32:43,990 ``` 2021-12-01T10:32:43,991 It used the casbin config from `examples` folder, and you can find this demo in `demo` folder. 2021-12-01T10:32:43,991 You can also view the unit tests to understand this middleware. 2021-12-01T10:32:43,991 ## Development 2021-12-01T10:32:43,991 ### Run unit tests 2021-12-01T10:32:43,992 1. Fork/Clone repository 2021-12-01T10:32:43,992 2. Install fastapi-authz dependencies, and run `pytest` 2021-12-01T10:32:43,992 ```bash 2021-12-01T10:32:43,992 pip install -r dev_requirements.txt 2021-12-01T10:32:43,992 pip install -r requirements.txt 2021-12-01T10:32:43,992 pytest 2021-12-01T10:32:43,993 ``` 2021-12-01T10:32:43,993 ### Update requirements with pip-tools 2021-12-01T10:32:43,993 ```bash 2021-12-01T10:32:43,993 # update requirements.txt 2021-12-01T10:32:43,993 pip-compile --no-annotate --no-header --rebuild requirements.in 2021-12-01T10:32:43,994 # sync venv 2021-12-01T10:32:43,994 pip-sync 2021-12-01T10:32:43,994 ``` 2021-12-01T10:32:43,994 ### Manually Bump Version 2021-12-01T10:32:43,994 ``` 2021-12-01T10:32:43,995 bumpversion major # major release 2021-12-01T10:32:43,995 or 2021-12-01T10:32:43,995 bumpversion minor # minor release 2021-12-01T10:32:43,995 or 2021-12-01T10:32:43,995 bumpversion patch # hotfix release 2021-12-01T10:32:43,995 ``` 2021-12-01T10:32:43,996 ## Documentation 2021-12-01T10:32:43,996 The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform 2021-12-01T10:32:43,996 what ``action`` on what ``object``. In this plugin, the meanings are: 2021-12-01T10:32:43,996 1. ``subject``: the logged-in user name 2021-12-01T10:32:43,996 2. ``object``: the URL path for the web resource like `dataset1/item1` 2021-12-01T10:32:43,996 3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", " 2021-12-01T10:32:43,997 write-blog" (currently no official support in this middleware) 2021-12-01T10:32:43,997 For how to write authorization policy and other details, please refer 2021-12-01T10:32:43,997 to [the Casbin's documentation](https://casbin.org). 2021-12-01T10:32:43,997 ## Getting Help 2021-12-01T10:32:43,997 - [Casbin](https://casbin.org) 2021-12-01T10:32:43,998 ## License 2021-12-01T10:32:43,998 This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. 2021-12-01T10:32:43,998 running egg_info 2021-12-01T10:32:43,998 creating /tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info 2021-12-01T10:32:43,999 writing /tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/PKG-INFO 2021-12-01T10:32:43,999 writing dependency_links to /tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/dependency_links.txt 2021-12-01T10:32:43,999 writing requirements to /tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/requires.txt 2021-12-01T10:32:43,999 writing top-level names to /tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/top_level.txt 2021-12-01T10:32:43,999 writing manifest file '/tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/SOURCES.txt' 2021-12-01T10:32:44,013 reading manifest file '/tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/SOURCES.txt' 2021-12-01T10:32:44,015 adding license file 'LICENSE' 2021-12-01T10:32:44,017 writing manifest file '/tmp/pip-pip-egg-info-tarmkhoe/fastapi_authz.egg-info/SOURCES.txt' 2021-12-01T10:32:44,082 Preparing metadata (setup.py): finished with status 'done' 2021-12-01T10:32:44,086 Source in /tmp/pip-wheel-emgqt68u/fastapi-authz_86fbe83605ad4aa4b2d56a3fc60570f1 has version 0.1.0, which satisfies requirement fastapi-authz==0.1.0 from https://files.pythonhosted.org/packages/ef/fb/11538a3ff6c41077ceba2eac1c09a22b33a2fd486c0b85d6199537c31f61/fastapi-authz-0.1.0.tar.gz#sha256=52c51184b60bee25501178b419b23c31007b6b4b6ba8940908d7f9a252f669de 2021-12-01T10:32:44,087 Removed fastapi-authz==0.1.0 from https://files.pythonhosted.org/packages/ef/fb/11538a3ff6c41077ceba2eac1c09a22b33a2fd486c0b85d6199537c31f61/fastapi-authz-0.1.0.tar.gz#sha256=52c51184b60bee25501178b419b23c31007b6b4b6ba8940908d7f9a252f669de from build tracker '/tmp/pip-req-tracker-s19bf9xm' 2021-12-01T10:32:44,094 Created temporary directory: /tmp/pip-unpack-kbwf51zq 2021-12-01T10:32:44,095 Building wheels for collected packages: fastapi-authz 2021-12-01T10:32:44,099 Created temporary directory: /tmp/pip-wheel-1u7sle9l 2021-12-01T10:32:44,099 Building wheel for fastapi-authz (setup.py): started 2021-12-01T10:32:44,100 Destination directory: /tmp/pip-wheel-1u7sle9l 2021-12-01T10:32:44,100 Running command /usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-wheel-emgqt68u/fastapi-authz_86fbe83605ad4aa4b2d56a3fc60570f1/setup.py'"'"'; __file__='"'"'/tmp/pip-wheel-emgqt68u/fastapi-authz_86fbe83605ad4aa4b2d56a3fc60570f1/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-1u7sle9l 2021-12-01T10:32:44,932 # fastapi-authz 2021-12-01T10:32:44,933 [![Build Status](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/fastapi-authz/actions/workflows/release.yml) 2021-12-01T10:32:44,933 [![Coverage Status](https://coveralls.io/repos/github/pycasbin/fastapi-authz/badge.svg)](https://coveralls.io/github/pycasbin/fastapi-authz) 2021-12-01T10:32:44,934 [![Version](https://img.shields.io/pypi/v/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:44,934 [![PyPI - Wheel](https://img.shields.io/pypi/wheel/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:44,934 [![Pyversions](https://img.shields.io/pypi/pyversions/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:44,934 [![Download](https://img.shields.io/pypi/dm/fastapi-authz.svg)](https://pypi.org/project/fastapi-authz/) 2021-12-01T10:32:44,934 [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/casbin/lobby) 2021-12-01T10:32:44,934 fastapi-authz is an authorization middleware for [FastAPI](https://fastapi.tiangolo.com/), it's based 2021-12-01T10:32:44,935 on [PyCasbin](https://github.com/casbin/pycasbin). 2021-12-01T10:32:44,935 ## Installation 2021-12-01T10:32:44,935 Install from pip 2021-12-01T10:32:44,935 ```bash 2021-12-01T10:32:44,936 pip install fastapi-authz 2021-12-01T10:32:44,936 ``` 2021-12-01T10:32:44,936 Clone this repo 2021-12-01T10:32:44,936 ```bash 2021-12-01T10:32:44,936 git clone https://github.com/pycasbin/fastapi-authz.git 2021-12-01T10:32:44,936 python setup.py install 2021-12-01T10:32:44,937 ``` 2021-12-01T10:32:44,937 ## Quickstart 2021-12-01T10:32:44,937 This middleware is designed to work with another middleware which implement `AuthenticationMiddleware` interface. 2021-12-01T10:32:44,937 ```python 2021-12-01T10:32:44,938 import base64 2021-12-01T10:32:44,938 import binascii 2021-12-01T10:32:44,938 import casbin 2021-12-01T10:32:44,938 from fastapi import FastAPI 2021-12-01T10:32:44,938 from starlette.authentication import AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials 2021-12-01T10:32:44,939 from starlette.middleware.authentication import AuthenticationMiddleware 2021-12-01T10:32:44,939 from fastapi_authz import CasbinMiddleware 2021-12-01T10:32:44,939 app = FastAPI() 2021-12-01T10:32:44,939 class BasicAuth(AuthenticationBackend): 2021-12-01T10:32:44,940 async def authenticate(self, request): 2021-12-01T10:32:44,940 if "Authorization" not in request.headers: 2021-12-01T10:32:44,940 return None 2021-12-01T10:32:44,940 auth = request.headers["Authorization"] 2021-12-01T10:32:44,940 try: 2021-12-01T10:32:44,940 scheme, credentials = auth.split() 2021-12-01T10:32:44,941 decoded = base64.b64decode(credentials).decode("ascii") 2021-12-01T10:32:44,941 except (ValueError, UnicodeDecodeError, binascii.Error): 2021-12-01T10:32:44,941 raise AuthenticationError("Invalid basic auth credentials") 2021-12-01T10:32:44,941 username, _, password = decoded.partition(":") 2021-12-01T10:32:44,941 return AuthCredentials(["authenticated"]), SimpleUser(username) 2021-12-01T10:32:44,942 enforcer = casbin.Enforcer('../examples/rbac_model.conf', '../examples/rbac_policy.csv') 2021-12-01T10:32:44,942 app.add_middleware(CasbinMiddleware, enforcer=enforcer) 2021-12-01T10:32:44,942 app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) 2021-12-01T10:32:44,943 @app.get('/') 2021-12-01T10:32:44,943 async def index(): 2021-12-01T10:32:44,943 return "If you see this, you have been authenticated." 2021-12-01T10:32:44,943 @app.get('/dataset1/protected') 2021-12-01T10:32:44,943 async def auth_test(): 2021-12-01T10:32:44,944 return "You must be alice to see this." 2021-12-01T10:32:44,944 ``` 2021-12-01T10:32:44,944 - anonymous request 2021-12-01T10:32:44,944 ```bash 2021-12-01T10:32:44,944 curl -i http://127.0.0.1:8000/dataset1/protected 2021-12-01T10:32:44,945 ``` 2021-12-01T10:32:44,945 ```bash 2021-12-01T10:32:44,945 HTTP/1.1 403 Forbidden 2021-12-01T10:32:44,945 date: Mon, 01 Mar 2021 09:00:08 GMT 2021-12-01T10:32:44,945 server: uvicorn 2021-12-01T10:32:44,945 content-length: 11 2021-12-01T10:32:44,945 content-type: application/json 2021-12-01T10:32:44,946 "Forbidden" 2021-12-01T10:32:44,946 ``` 2021-12-01T10:32:44,946 - authenticated request 2021-12-01T10:32:44,946 ```bash 2021-12-01T10:32:44,947 curl -i -u alice:password http://127.0.0.1:8000/dataset1/protected 2021-12-01T10:32:44,947 ``` 2021-12-01T10:32:44,947 ```bash 2021-12-01T10:32:44,947 HTTP/1.1 200 OK 2021-12-01T10:32:44,947 date: Mon, 01 Mar 2021 09:04:54 GMT 2021-12-01T10:32:44,947 server: uvicorn 2021-12-01T10:32:44,948 content-length: 32 2021-12-01T10:32:44,948 content-type: application/json 2021-12-01T10:32:44,948 "You must be alice to see this." 2021-12-01T10:32:44,948 ``` 2021-12-01T10:32:44,948 It used the casbin config from `examples` folder, and you can find this demo in `demo` folder. 2021-12-01T10:32:44,949 You can also view the unit tests to understand this middleware. 2021-12-01T10:32:44,949 ## Development 2021-12-01T10:32:44,949 ### Run unit tests 2021-12-01T10:32:44,949 1. Fork/Clone repository 2021-12-01T10:32:44,949 2. Install fastapi-authz dependencies, and run `pytest` 2021-12-01T10:32:44,950 ```bash 2021-12-01T10:32:44,950 pip install -r dev_requirements.txt 2021-12-01T10:32:44,950 pip install -r requirements.txt 2021-12-01T10:32:44,950 pytest 2021-12-01T10:32:44,950 ``` 2021-12-01T10:32:44,951 ### Update requirements with pip-tools 2021-12-01T10:32:44,951 ```bash 2021-12-01T10:32:44,951 # update requirements.txt 2021-12-01T10:32:44,951 pip-compile --no-annotate --no-header --rebuild requirements.in 2021-12-01T10:32:44,951 # sync venv 2021-12-01T10:32:44,951 pip-sync 2021-12-01T10:32:44,951 ``` 2021-12-01T10:32:44,952 ### Manually Bump Version 2021-12-01T10:32:44,952 ``` 2021-12-01T10:32:44,952 bumpversion major # major release 2021-12-01T10:32:44,952 or 2021-12-01T10:32:44,952 bumpversion minor # minor release 2021-12-01T10:32:44,953 or 2021-12-01T10:32:44,953 bumpversion patch # hotfix release 2021-12-01T10:32:44,953 ``` 2021-12-01T10:32:44,953 ## Documentation 2021-12-01T10:32:44,953 The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform 2021-12-01T10:32:44,953 what ``action`` on what ``object``. In this plugin, the meanings are: 2021-12-01T10:32:44,954 1. ``subject``: the logged-in user name 2021-12-01T10:32:44,954 2. ``object``: the URL path for the web resource like `dataset1/item1` 2021-12-01T10:32:44,954 3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", " 2021-12-01T10:32:44,954 write-blog" (currently no official support in this middleware) 2021-12-01T10:32:44,954 For how to write authorization policy and other details, please refer 2021-12-01T10:32:44,954 to [the Casbin's documentation](https://casbin.org). 2021-12-01T10:32:44,955 ## Getting Help 2021-12-01T10:32:44,955 - [Casbin](https://casbin.org) 2021-12-01T10:32:44,955 ## License 2021-12-01T10:32:44,955 This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. 2021-12-01T10:32:45,110 running bdist_wheel 2021-12-01T10:32:45,116 running build 2021-12-01T10:32:45,116 running build_py 2021-12-01T10:32:45,120 creating build 2021-12-01T10:32:45,121 creating build/lib 2021-12-01T10:32:45,121 creating build/lib/fastapi_authz 2021-12-01T10:32:45,122 copying fastapi_authz/__init__.py -> build/lib/fastapi_authz 2021-12-01T10:32:45,124 copying fastapi_authz/middleware.py -> build/lib/fastapi_authz 2021-12-01T10:32:45,126 running egg_info 2021-12-01T10:32:45,141 writing fastapi_authz.egg-info/PKG-INFO 2021-12-01T10:32:45,143 writing dependency_links to fastapi_authz.egg-info/dependency_links.txt 2021-12-01T10:32:45,145 writing requirements to fastapi_authz.egg-info/requires.txt 2021-12-01T10:32:45,146 writing top-level names to fastapi_authz.egg-info/top_level.txt 2021-12-01T10:32:45,168 reading manifest file 'fastapi_authz.egg-info/SOURCES.txt' 2021-12-01T10:32:45,170 adding license file 'LICENSE' 2021-12-01T10:32:45,173 writing manifest file 'fastapi_authz.egg-info/SOURCES.txt' 2021-12-01T10:32:45,181 /usr/local/lib/python3.7/dist-packages/setuptools/command/install.py:37: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. 2021-12-01T10:32:45,181 setuptools.SetuptoolsDeprecationWarning, 2021-12-01T10:32:45,184 installing to build/bdist.linux-armv7l/wheel 2021-12-01T10:32:45,184 running install 2021-12-01T10:32:45,186 running install_lib 2021-12-01T10:32:45,190 creating build/bdist.linux-armv7l 2021-12-01T10:32:45,190 creating build/bdist.linux-armv7l/wheel 2021-12-01T10:32:45,192 creating build/bdist.linux-armv7l/wheel/fastapi_authz 2021-12-01T10:32:45,193 copying build/lib/fastapi_authz/__init__.py -> build/bdist.linux-armv7l/wheel/fastapi_authz 2021-12-01T10:32:45,195 copying build/lib/fastapi_authz/middleware.py -> build/bdist.linux-armv7l/wheel/fastapi_authz 2021-12-01T10:32:45,197 running install_data 2021-12-01T10:32:45,199 creating build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.data 2021-12-01T10:32:45,200 creating build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.data/data 2021-12-01T10:32:45,200 warning: install_data: setup script did not provide a directory for 'README.md' -- installing right in 'build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.data/data' 2021-12-01T10:32:45,201 copying README.md -> build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.data/data 2021-12-01T10:32:45,203 warning: install_data: setup script did not provide a directory for 'requirements.txt' -- installing right in 'build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.data/data' 2021-12-01T10:32:45,204 copying requirements.txt -> build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.data/data 2021-12-01T10:32:45,205 running install_egg_info 2021-12-01T10:32:45,215 Copying fastapi_authz.egg-info to build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0-py3.7.egg-info 2021-12-01T10:32:45,227 running install_scripts 2021-12-01T10:32:45,303 adding license file "LICENSE" (matched pattern "LICEN[CS]E*") 2021-12-01T10:32:45,308 creating build/bdist.linux-armv7l/wheel/fastapi_authz-0.1.0.dist-info/WHEEL 2021-12-01T10:32:45,311 creating '/tmp/pip-wheel-1u7sle9l/fastapi_authz-0.1.0-py3-none-any.whl' and adding 'build/bdist.linux-armv7l/wheel' to it 2021-12-01T10:32:45,313 adding 'fastapi_authz/__init__.py' 2021-12-01T10:32:45,315 adding 'fastapi_authz/middleware.py' 2021-12-01T10:32:45,318 adding 'fastapi_authz-0.1.0.data/data/README.md' 2021-12-01T10:32:45,319 adding 'fastapi_authz-0.1.0.data/data/requirements.txt' 2021-12-01T10:32:45,322 adding 'fastapi_authz-0.1.0.dist-info/LICENSE' 2021-12-01T10:32:45,324 adding 'fastapi_authz-0.1.0.dist-info/METADATA' 2021-12-01T10:32:45,325 adding 'fastapi_authz-0.1.0.dist-info/WHEEL' 2021-12-01T10:32:45,326 adding 'fastapi_authz-0.1.0.dist-info/top_level.txt' 2021-12-01T10:32:45,327 adding 'fastapi_authz-0.1.0.dist-info/RECORD' 2021-12-01T10:32:45,328 removing build/bdist.linux-armv7l/wheel 2021-12-01T10:32:45,402 Building wheel for fastapi-authz (setup.py): finished with status 'done' 2021-12-01T10:32:45,404 Created wheel for fastapi-authz: filename=fastapi_authz-0.1.0-py3-none-any.whl size=10706 sha256=3748966021c96b4db8a2b5beff8e9e17bc6b0316439aaa0121d8b6ea985bb643 2021-12-01T10:32:45,405 Stored in directory: /tmp/pip-ephem-wheel-cache-bp_c1iv8/wheels/88/ed/e9/5e882bb3b381feb14d585c7755eec1307db983adc4a47216ad 2021-12-01T10:32:45,415 Successfully built fastapi-authz 2021-12-01T10:32:45,418 Removed build tracker: '/tmp/pip-req-tracker-s19bf9xm'