Base Usage
FastJWT core concept relies in generating access tokens and protecting routes. The following examples demonstrates how to use FastJWT to quickly integrate those system within you FastAPI application.
Setup
Let's build our first FastAPI application with FastJWT
Create the FastAPI application
As usual, you create your application with the fastapi.FastAPI
object
Configure the JWT behavior
FastJWT provides a FJWTConfig
object (pydantic's BaseSetting) to customize the behavior of JWT management.
Here we enforce a symmetric encryption algorithm as "HS256"
and we set the SECRET_KEY
as the encoding/decoding key.
Handle secrets
By construction, JSON Web Tokens are not encrypted, you can try your own JWT on https://jwt.io/. However, you will need secrets for your server to sign tokens.
Secrets location
As a best practice do not use explicit secrets within your code. It is recommended to use environment variables to avoid any credentials leakage
Note on Algorithm
For demonstration ease, we use a symmetric algorithm. Note that asymmetric algorithm offers additional layers of protection.
"RS256"
is the recommended algorithm when signing JWTs
Create the FastJWT instance
You can now instantiate the FastJWT
object with the your configuration
Loading configuration after FastJWT.__init__
The config
argument in the FastJWT.__init__
is optional, you can use the FastJWT.load_config
method after
initialisation to apply your configuration
Authentication
Create the access token
To authenticate a user we create a /login
route the usual way with FastAPI.
Once a user has provided good credentials, we use the FastJWT.create_access_token
method to generate a signed token.
To tie the user to the token, we use the uid
argument.
Note on PRIVACY
Using PIDs (Personal Identification Data) should also be avoided in the JWT since its content is fully readable.
As a best practice uid
should usually be a user database index (not ordered). Check UUIDs for addtitional details.
Note on login protection
The /login
route above is a dummy example. Credentials should not be carried through query paramaters.
The appropriate logic should implement deeper checks with regard to authentication.
Protect a route
Let's work on a simple GET
route that can only be accessed by authenticated users.
FastJWT is compliant with the FastAPI dependency injection system.
It provides a FastJWT.access_token_required
method to enforce this behavior.
Whether we provide a bad token or no token at all, the server will forbid the route logic defined in /protected
to be executed.
Default exception behavior
In the curl requests above a 401
HTTP Error is raised when the token is not valid.
Without addtional setup, the expected behavior from FastJWT is an 500 Internal Server Error
HTTP Error.
For ease of demonstration, we do not dive into error handling in this section.