Quick JWT library for authorization in FastAPI applications
Project description
Quick JWT
Quick JWT library for authorization in FastAPI applications
Source Code: https://github.com/maxim-f1/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:
- FastAPI - main web framework.
- PyJWT - default JWT driver.
- Pydantic Settings - extension for pydantic with .env variables.
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quick_jwt-0.0.3.tar.gz.
File metadata
- Download URL: quick_jwt-0.0.3.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e0f0cc9ac4ca5370df2f963796a48f29cf1db7d1f512a296b1efa4072987037
|
|
| MD5 |
4cf61e320a2d976c56ac7d314376aa06
|
|
| BLAKE2b-256 |
b1e839b1707573fd14c6c2f5ddf91a4254f2d3378658d554f595f9ce018076d9
|
File details
Details for the file quick_jwt-0.0.3-py3-none-any.whl.
File metadata
- Download URL: quick_jwt-0.0.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
736a8a0dac3a86fbfa596c0527578d06746b8612bcc2af7b251d30e23bedfe0a
|
|
| MD5 |
f269951124fe0f465d9897f373db649b
|
|
| BLAKE2b-256 |
5d7fd00b59df15bbc91573d5ecf27e3b84307c1560280e1eb3cf6d928f32de1b
|