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.8.0.tar.gz (4.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.8.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_jwt_authlib-0.8.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.1 Linux/6.11.0-1018-azure

File hashes

Hashes for fastapi_jwt_authlib-0.8.0.tar.gz
Algorithm Hash digest
SHA256 34ba8f3eb0a4741964d85db57c0390edc47a43441f05f788a039c43888461d68
MD5 4c35017d562a19639ec79fe026195a11
BLAKE2b-256 04ff969882d456fad22783edc58b06c0caabae8a3727c52cf3b1b2512d276d07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_jwt_authlib-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.1 Linux/6.11.0-1018-azure

File hashes

Hashes for fastapi_jwt_authlib-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1570f3ecac68eecbaf3a3b5d6a892cef2b6b6c2b5c8f27a1ed7ac21ddc8b21c2
MD5 a168e2b231e1f7e6c079499f9c40ec88
BLAKE2b-256 ede241c3218405f11aaaa9d554ed50d76ad6eb8e197ca12300e098bf5125317d

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