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

Security: Published advisories · Report a vulnerability privately

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

FastAPI PR #3305 is closed. FastAPI declined built-in JWT support because token handling belongs in an external package. The maintainer also declined the additional python-jose dependency. See the maintainer response.

Requirements

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

License

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

Metadata

Release files for fastapi-jwt 0.3.1

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

Source distribution (sdist)

Source distribution for fastapi-jwt 0.3.1
File Size Uploaded Provenance Actions
fastapi_jwt-0.3.1.tar.gz 14.1 kB Without provenance Details for fastapi_jwt-0.3.1.tar.gz

Built distribution (wheel)

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

Total release size: 23.4 kB

Release files / fastapi_jwt-0.3.1.tar.gz

Download URL fastapi_jwt-0.3.1.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
38a71ce3b376e890e95cdffdca63c82c6c582ff6bdda688bc6d894e223f5c75f
BLAKE2b-256 checksum
How to use checksums
3291bd8c9f7657a88100353e3fc1b9e783161f0d87846e052974e35d8b22dcee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL fastapi_jwt-0.3.1-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bcb8ad643b997308e8f0fbcb0d028c1f0ac3d4582622df71b63f02e95c5aa588
BLAKE2b-256 checksum
How to use checksums
9eb481216614e263031f320de39d474ffe0d448d6a2454ee31c241ff2be8482d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for fastapi-jwt 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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