Skip to content

Quick JWT

Quick JWT library for authorization in FastAPI applications

Run Tests Coverage Package version Supported Python versions


Source Code: https://github.com/maxim-f1/quick_jwt

Documentation https://maxim-f1.github.io/quick_jwt/


Quick JWT is a lightweight and convenient solution for handling JWT token-based authorization.

The key features are:

  • Full integration with FastAPI framework and Intuitive use of FastAPI style features.

  • Easy customization of environment variables via Pydantic-Settings and Middleware.

  • Convenient functions for wrapping Depends with data type persistence.

  • Many Depends functions for a large number of tasks.

  • Ability to customize standard PyJWT driver to custom solutions.

  • Support for function arguments for PyJWT driver, as well as Pydantic validation functions.

Requirements

Quick JWT stands on the shoulders of giants:

Installation

Create and activate a virtual environment and then install Quick JWT:

$ pip install quick-jwt

Example

Create it

Create a file main.py with:

from fastapi import FastAPI
from pydantic import BaseModel
from quick_jwt import (
    QuickJWTConfig,
    QuickJWTMiddleware,
    create_jwt_depends,
)

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

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


class UserScheme(BaseModel):
    sub: str


@app.get("/create-tokens")
async def create_tokens(
    sub: str, 
    jwt: create_jwt_depends(UserScheme, UserScheme)
):
    user = UserScheme(sub=sub)
    tokens = await jwt.create_jwt_tokens(user, user)
    return tokens

Run it

Run the server with:

$ fastapi dev main.py

 ╭────────── FastAPI CLI - Development mode ───────────╮
 │                                                     │
 │  Serving at: http://127.0.0.1:8000                  │
 │                                                     │
 │  API docs: http://127.0.0.1:8000/docs               │
 │                                                     │
 │  Running in development mode, for production use:   │
 │                                                     │
 │  fastapi run                                        │
 │                                                     │
 ╰─────────────────────────────────────────────────────╯

INFO:     Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [2248755] using WatchFiles
INFO:     Started server process [2248757]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Check it

Open your browser at http://127.0.0.1:8000/create-tokens?sub=some_id.

You will see the JSON response as:

{
  "access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzb21lX2lkIn0.EerZU4uZCRh7yXxOqsZKTwzls7BnL-6C8jidTTkit6k",
  "refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzb21lX2lkIn0.EerZU4uZCRh7yXxOqsZKTwzls7BnL-6C8jidTTkit6k"
}

Interactive API docs upgrade

Now go to http://127.0.0.1:8000/docs.

License

This project is licensed under the terms of the MIT license.