Skip to main content

A FastAPI JWT authentication library

Project description

fastapi-jwt-authlib

PyPI version


Source Code: https://github.com/kovalewvladimir/fastapi-jwt-authlib


Описание проекта

Библиотека для аутентификации JWT в FastAPI. Она предоставляет удобные инструменты для работы с JWT токенами, обеспечивая безопасность и простоту интеграции в ваши FastAPI приложения.

Установка

pip install fastapi-jwt-authlib

Пример использования

main.py:

from fastapi_jwt_authlib.auth import AuthJWT

AuthJWT.config(
    secret_key=settings.SECRET_KEY,
    cookie_access_key=settings.COOKIE_ACCESS_KEY,
    cookie_refresh_key=settings.COOKIE_REFRESH_KEY,
    cookie_refresh_path=f"{settings.ROOT_PATH}/auth/refresh",
)

auth.py:

from fastapi import APIRouter, HTTPException
from fastapi.security import HTTPBasicCredentials
from fastapi_jwt_authlib.auth import JWTUserData
from fastapi_jwt_authlib.depends import (
    AuthAccessDepends,
    AuthDepends,
    AuthRefreshDepends,
)

router = APIRouter()


@router.post("/login")
def login(user: HTTPBasicCredentials, auth: AuthDepends):
    if user.username != "admin" or user.password != "admin":
        raise HTTPException(status_code=401, detail="Incorrect username or password")

    roles = ["admin"]
    data_jwt = JWTUserData(user=user.username, roles=roles)
    auth.generate_and_store_access_token(data_jwt)
    auth.generate_and_store_refresh_token(data_jwt)

    return {"msg": "Successful login"}


@router.delete("/logout")
def logout(auth: AuthDepends):
    auth.unset_cookies()
    return {"msg": "Successful logout"}


@router.post("/refresh")
def refresh(auth: AuthRefreshDepends):
    roles = ["admin"] if is_admin(auth.user) else []
    data_jwt = JWTUserData(user=auth.user, roles=roles)
    auth.jwt.generate_and_store_access_token(data_jwt)
    return {"msg": "The token has been refresh"}


@router.get("/protected")
def protected(auth: AuthAccessDepends):
    return {"user": auth.user}

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

fastapi_jwt_authlib-0.9.6.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

fastapi_jwt_authlib-0.9.6-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_jwt_authlib-0.9.6.tar.gz.

File metadata

  • Download URL: fastapi_jwt_authlib-0.9.6.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_jwt_authlib-0.9.6.tar.gz
Algorithm Hash digest
SHA256 fec6809862e9ebf6a45f066390231f3ca005141e5721b15e9c4f86047a4efecf
MD5 1e998d9b3c9c9c5bbf098b437875244d
BLAKE2b-256 3e766ae34288ed5df6089618184016f434b5c527bbc705c95622cfae1537610c

See more details on using hashes here.

File details

Details for the file fastapi_jwt_authlib-0.9.6-py3-none-any.whl.

File metadata

  • Download URL: fastapi_jwt_authlib-0.9.6-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_jwt_authlib-0.9.6-py3-none-any.whl
Algorithm Hash digest
SHA256 827710414cf849fe76044aa92c1413ef0fd4ffee112666dccb84d31a9254356a
MD5 73abbe23b3fe059e65b5f660839ffa43
BLAKE2b-256 8b17718096472520b2a1280c68202e3b00a86dd24ea4ab5c12202e5958367686

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