Skip to content

Setup

To customize the library, you will need to do a few simple things.

Prerequisites

Make sure you have installed the library. How to do this was explained on the previous install page.

Setting variables

To work with Quick JWT, you will need to set up variables in the QuickJWTConfig configuration class.

The simplest example of setting up QuickJWTConfig that will allow a full user of the library:

from quick_jwt import QuickJWTConfig

key = "default_key"
config = QuickJWTConfig(encode_key=key, decode_key=key)

The encode_key and decode_key variables are the only mandatory arguments in the configuration class.

Note

The key variable will be responsible for encrypting and decrypting JWT tokens, as the example will use the HS256 symmetric encryption algorithm.

Environment variables

This is only a tutorial example. Don't use hardcode in your project. It is better to use .env files with environment variables. If the variables are already in the environment, this code will suffice:

from quick_jwt import QuickJWTConfig

config = QuickJWTConfig()

This is possible thanks to the pydantic_settings library.

Middleware

In order for Quick JWT to know what variables you have defined for your project you must use QuickJWTMiddleware:

from fastapi import FastAPI
from quick_jwt import (
    QuickJWTConfig,
    QuickJWTMiddleware,
)

key = "default_key"
config = QuickJWTConfig(encode_key=key, decode_key=key)

app = FastAPI()
app.add_middleware(QuickJWTMiddleware, config)

Now your project is ready to fully utilize the Quick JWT library

Advanced settings

Inside the library there is a wide range of functionality for customizing its behavior. The following is a list of the most common ways to override the standard logic.

PyJWT options

There are cases when it is necessary to strictly specify which fields will be used in access and refresh tokens. You can override the driver for this purpose:

from jwt import PyJWT
from quick_jwt import QuickJWTConfig

options = {
    "verify_signature": True,
    "verify_exp": True,
    "verify_nbf": False,
    "verify_iat": False,
    "verify_aud": False,
    "verify_iss": False,
    "verify_sub": True,
    "verify_jti": False,
    "require": [],
}
driver = PyJWT(
    options=options
)
config = QuickJWTConfig(driver=driver)
from datetime import timedelta

from quick_jwt import QuickJWTConfig

config = QuickJWTConfig(
    access_token_name='access',
    access_token_expires=timedelta(days=2),
    access_token_path='/',
    access_token_domain='domain.com',
    access_token_secure=True,
    access_token_httponly=True,
    access_token_samesite='lax',
)

Note

You can override the refresh behavior of the token by the same principle.

Additional variables for the encode function

When encrypting tokens, additional parameters can be thrown in:

import json

from quick_jwt import QuickJWTConfig

config = QuickJWTConfig(
    encode_algorithm='HS256',
    encode_headers={'X-Custom-Header': 'Value'},
    encode_json_encoder=json.JSONEncoder,
    encode_sort_headers=False,
)

Additional variables for the decode function

from quick_jwt import QuickJWTConfig

config = QuickJWTConfig(
    decode_algorithms=['HS256'],
    decode_options={'sub': False},
    decode_verify=False,
)

Custom driver

The default driver for the library is PyJWT, but you can also override it with the driver variable:

from quick_jwt import QuickJWTConfig

config = QuickJWTConfig(driver=AnotherDriver())

Note

For a custom driver to work correctly, it must have encode and decode functions.