Skip to main content

Quick JWT library for authorization in FastAPI applications

Project description

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quick_jwt-0.0.4.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quick_jwt-0.0.4-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file quick_jwt-0.0.4.tar.gz.

File metadata

  • Download URL: quick_jwt-0.0.4.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for quick_jwt-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2cba74f337a5f95cfb7351a7483fcbe0e89856faf8d0381fa92c81016d14f934
MD5 32457913fdddfa7614b132952668ca70
BLAKE2b-256 8f41fbfe263390bb4bec0dfecfe59d74359f24c6ed5a177b3a0c2b3b7dcc930c

See more details on using hashes here.

File details

Details for the file quick_jwt-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: quick_jwt-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for quick_jwt-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4cdcc76340a7821befb2603561556f80e9863a0eac5e8cd7dd40e593d08bfec1
MD5 672727b8ad08495015360735b3f05679
BLAKE2b-256 e68ab42e0ccaa81a5724154335dd835f7a82337450ffcc64779bd0a8f225f11c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page