Skip to main content

fastapi-jwt

Test Publish codecov pypi

FastAPI native extension, easy and simple JWT auth


Documentation: k4black.github.io/fastapi-jwt
Source Code: github.com/k4black/fastapi-jwt

Features

  • OpenAPI schema generation
  • Native integration with FastAPI
  • Access/Refresh JWT
  • JTI
  • Cookie setting

Installation

You can access package fastapi-jwt in pypi

pip install fastapi-jwt[authlib]
# or
pip install fastapi-jwt[python_jose]

The fastapi-jwt will choose the backend automatically if library is installed with the following priority:

  1. authlib
  2. python_jose (deprecated)

Usage

This library made in fastapi style, so it can be used as standard security features

from fastapi import FastAPI, Security, Response
from fastapi_jwt import JwtAuthorizationCredentials, JwtAccessBearer


app = FastAPI()
access_security = JwtAccessBearer(secret_key="secret_key", auto_error=True)


@app.post("/auth")
def auth():
    subject = {"username": "username", "role": "user"}
    return {"access_token": access_security.create_access_token(subject=subject)}

@app.post("/auth_cookie")
def auth(response: Response):
    subject = {"username": "username", "role": "user"}
    access_token = access_security.create_access_token(subject=subject)
    access_security.set_access_cookie(response, access_token)
    return {"access_token": access_token}


@app.get("/users/me")
def read_current_user(
    credentials: JwtAuthorizationCredentials = Security(access_security),
):
    return {"username": credentials["username"], "role": credentials["role"]}

For more examples see usage docs

Alternatives

  • FastAPI docs suggest writing it manually, but

    • code duplication
    • opportunity for bugs
  • There is nice fastapi-jwt-auth, but

    • poorly supported
    • not "FastAPI-style" (not native functions parameters)

FastAPI Integration

There it is open and maintained Pull Request #3305 to the fastapi repo. Currently, not considered.

Requirements

  • fastapi
  • authlib or python-jose[cryptography] (deprecated)

License

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

Release files for fastapi-jwt 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-jwt 0.3.0
File Interpreter ABI Platform
fastapi_jwt-0.3.0-py3-none-any.whl Python 3 none any Details

Release files / fastapi_jwt-0.3.0-py3-none-any.whl

Download URL fastapi_jwt-0.3.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4255ce929f1c59c2bbcf5295b485995c8263466aa733a0dd164b052b4a476a3
BLAKE2b-256 checksum
How to use checksums
ff4a60eb509cd08a591967fbdaab7f158c18d12172336432f3634ed4068b4101
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.3.0 This release

1 release file

0.2.0

1 release file

0.1.12

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page