FastAPI Security
Implements authentication and authorization as dependencies in FastAPI.
Features
- Authentication via JWT-based OAuth 2 access tokens and via Basic Auth
- Pydantic-based
Usermodel for authenticated and anonymous users - Sub-classable
UserPermissiondependency to check against thepermissionsattribute returned in OAuth 2 access tokens - Able to extract user info from access tokens via OpenID Connect
Limitations
- Only supports validating access tokens using public keys from a JSON Web Key Set (JWKS) endpoint. I.e. for use with external identity providers such as Auth0 and ORY Hydra.
- Permissions can only be picked up automatically from OAuth2 tokens, from the non-standard
permissionslist attribute (Auth0 provides this, maybe other identity providers as well). For all other use cases,permission_overridesmust be used. For example if there's a basic auth user calleduser1you can setpermission_overrides={"user1": ["*"]}to give the user access to all permissions, orpermission_overrides={"user1": ["products:create"]}to only assignuser1with the permissionproducts:create.
Installation
pip install fastapi-security
Usage examples
Examples on how to use can be found here.
TODO
- Write more tests
,
(screenshot taken from the latest CI action of this project)
(screenshot taken from another open-source project)