This is a simple project to quickstart a project using FastAPI (Python), Docker, and Auth0 authentication.
- Docker. Docker-compose is recommended, but not required.
- Python3 (for the setup script)
- Clone this repository
git clone https://github.com/amunchet/fastapi-auth0-quickstart.git
cd fastapi-auth0-quickstart
./setup.py
(orpython setup.py
)- Follow the instructions.
- Enjoy!
There are some basic tests to make sure the URLs are protected.
These must be run in the created docker
cd /src/
pytest
When writing further test, take a look at common/test.py
for the unwrap
function. That will allow you to test functions that are wrapped in the security decorator. Syntax is unwrap(wrapped_function)(function_arguments)
This project is meant to be a starting point. From here, a typical workflow could look at follows.
mv [PROJECT NAME] ..
cat docker-compose.yml >> ../docker-compose.yml
cd .. && rm -rf fastapi-auth0-quickstart
To try out the authentication, you will need your Authorization Header from an already authenticated request. Copy that (it should begin wtih "Bearer ...")
-
import requests
-
a = requests.get("http://localhost:[PORT]/secure", headers={"Authorization" : "Bearer ..."})
-
If you did not set your user to have permissions
read
, the request will return401
. Otherwise,a.text
contain "Secure route."
If you have a problem, check to make sure your Auth0 permissions are configured correctly for your user.
The simple FastAPI application in serve.py
is meant as a starting point.
Permissions are defined at the top of the file.
From there, use the definied wrappers on FastAPI routes. Two examples are given, with and without a wrapper.