Metadata-Version: 2.4
Name: fastapi-jwt
Version: 0.4.0
Summary: `FastAPI` extension for JTW Auth
Author: Konstantin Chernyshev
License-Expression: MIT
Project-URL: homepage, https://github.com/k4black/fastapi-jwt
Project-URL: documentation, https://k4black.github.io/fastapi-jwt/
Project-URL: Security advisories, https://github.com/k4black/fastapi-jwt/security/advisories
Project-URL: Report a vulnerability, https://github.com/k4black/fastapi-jwt/security/advisories/new
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.162.2
Provides-Extra: authlib
Requires-Dist: joserfc<2.0.0,>=1.8.0; extra == "authlib"
Provides-Extra: test
Requires-Dist: joserfc<2.0.0,>=1.8.0; extra == "test"
Requires-Dist: httpx2<3.0.0,>=2.16.0; extra == "test"
Requires-Dist: pytest<10.0.0,>=9.2.0; extra == "test"
Requires-Dist: pytest-cov<8.0.0,>=7.1.0; extra == "test"
Requires-Dist: pytest-mock<4.0.0,>=3.17.0; extra == "test"
Requires-Dist: requests<3.0.0,>=2.34.2; extra == "test"
Requires-Dist: ruff<1.0.0,>=0.17.4; extra == "test"
Requires-Dist: ty<0.1.0,>=0.0.100; extra == "test"
Provides-Extra: docs
Requires-Dist: mkdocs<2.0.0,>=1.6.1; extra == "docs"
Requires-Dist: mkdocs-material<10.0.0,>=9.8.1; extra == "docs"
Requires-Dist: MkAutoDoc<1.0.0,>=0.2.0; extra == "docs"
Requires-Dist: lazydocs<1.0.0,>=0.4.8; extra == "docs"
Requires-Dist: mkdocs-include-markdown-plugin<8.0.0,>=7.3.1; extra == "docs"
Requires-Dist: mkdocs-awesome-pages-plugin<3.0.0,>=2.10.1; extra == "docs"
Requires-Dist: mike<3.0.0,>=2.2.0; extra == "docs"
Dynamic: license-file

# fastapi-jwt

[![Test](https://github.com/k4black/fastapi-jwt/actions/workflows/test.yml/badge.svg)](https://github.com/k4black/fastapi-jwt/actions/workflows/test.yml)
[![Publish](https://github.com/k4black/fastapi-jwt/actions/workflows/publish.yml/badge.svg)](https://github.com/k4black/fastapi-jwt/actions/workflows/publish.yml)
[![codecov](https://codecov.io/gh/k4black/fastapi-jwt/branch/main/graph/badge.svg?token=3F9J850FX2)](https://codecov.io/gh/k4black/fastapi-jwt)
[![pypi](https://img.shields.io/pypi/v/fastapi-jwt)](https://pypi.org/project/fastapi-jwt/)

FastAPI native extension, easy and simple JWT auth

---


**Documentation:** [k4black.github.io/fastapi-jwt](https://k4black.github.io/fastapi-jwt/)  
**Source Code:** [github.com/k4black/fastapi-jwt](https://github.com/k4black/fastapi-jwt/)


## Security

Report vulnerabilities through [GitHub private reporting](https://github.com/k4black/fastapi-jwt/security/advisories/new).
See [security advisories](https://github.com/k4black/fastapi-jwt/security/advisories) for affected versions and fixes.

## Features
* OpenAPI schema generation 
* Native integration with FastAPI
* Access/Refresh JWT
* JTI and expiration metadata
* Optional audience and issuer validation
* Cookie setting


## Development and releases

Run `python -m ruff check fastapi_jwt tests`, `python -m ruff format --check fastapi_jwt tests`, and `ty check fastapi_jwt`.
Release Please manages version and changelog PRs. See [the release setup](docs/docs/developing.md#publishing).

## Installation
You can access package [fastapi-jwt in pypi](https://pypi.org/project/fastapi-jwt/)
```shell
pip install fastapi-jwt[authlib]
```

python-jose support was removed due to [GHSA-3qf3-8w2g-rqmx](https://github.com/advisories/GHSA-3qf3-8w2g-rqmx).

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

```python
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](https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/), but
  * code duplication  
  * opportunity for bugs

* There is nice [fastapi-jwt-auth](https://github.com/IndominusByte/fastapi-jwt-auth/), but
  * poorly supported  
  * not "FastAPI-style" (not native functions parameters)

## FastAPI Integration 

[FastAPI PR #3305](https://github.com/fastapi/fastapi/pull/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](https://github.com/fastapi/fastapi/pull/3305#issuecomment-3121608562).
This project remains a separate FastAPI extension.

## Requirements 

* Python 3.11 or later
* `fastapi >=0.162.2`
* `joserfc >=1.8.0,<2.0.0`

## Security

Access dependencies reject refresh tokens. Invalid signatures and malformed credential payloads return authentication errors.
`auto_error=False` returns `None` for invalid credentials.
Tokens must contain an object in `subject`, the expected `type`, and a finite numeric `exp`.

Cookie helpers now default to `secure=True`, `httponly=True`, and `samesite="lax"`.
Use HTTPS. For local HTTP development, pass `secure=False` explicitly.
The keyword-only options preserve positional `expires_delta` arguments.
Use the same cookie options when you unset a cookie.
SameSite and HttpOnly do not provide complete CSRF protection.
Applications that accept cookies must validate Origin or use CSRF tokens for state-changing requests.
Do not change state through GET requests.

The `authlib` installation extra and `AuthlibJWTBackend` class now use joserfc.
Their public names remain available for compatibility. The implementation does not import deprecated `authlib.jose`.

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