Skip to main content

JWT user provider for casbin-fastapi-decorator

Project description

casbin-fastapi-decorator-jwt

JWT user provider for casbin-fastapi-decorator.

Extracts and validates a JWT from the Bearer header and/or a cookie, returning the payload as the current user.

Installation

pip install casbin-fastapi-decorator-jwt

Or via the core package extra:

pip install "casbin-fastapi-decorator[jwt]"

Usage

from casbin_fastapi_decorator_jwt import JWTUserProvider

user_provider = JWTUserProvider(
    secret_key="your-secret",
    algorithm="HS256",             # default
    cookie_name="access_token",    # optional, enables reading from cookie
    user_model=UserSchema,         # optional, Pydantic model for payload validation
)

Pass it to PermissionGuard as the user_provider:

import casbin
from fastapi import FastAPI, HTTPException
from casbin_fastapi_decorator import PermissionGuard

async def get_enforcer() -> casbin.Enforcer:
    return casbin.Enforcer("model.conf", "policy.csv")

guard = PermissionGuard(
    user_provider=user_provider,
    enforcer_provider=get_enforcer,
    error_factory=lambda user, *rv: HTTPException(403, "Forbidden"),
)

app = FastAPI()

@app.get("/articles")
@guard.require_permission("articles", "read")
async def list_articles():
    return []

API

JWTUserProvider

JWTUserProvider(
    secret_key: str,
    algorithm: str = "HS256",
    cookie_name: str | None = None,
    user_model: type[BaseModel] | None = None,
)
Parameter Description
secret_key Secret used to verify the JWT signature
algorithm JWT algorithm (default: "HS256")
cookie_name If set, also reads the token from this cookie name
user_model Pydantic model — if provided, the payload is validated via model_validate()

When called as a FastAPI dependency, the provider reads the token from:

  1. Authorization: Bearer <token> header (always)
  2. Cookie <cookie_name> (if cookie_name is set)

Development

See the workspace README for setup instructions.

task jwt:lint    # ruff + bandit + ty
task jwt:test    # pytest

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

casbin_fastapi_decorator_jwt-0.2.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

casbin_fastapi_decorator_jwt-0.2.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file casbin_fastapi_decorator_jwt-0.2.0.tar.gz.

File metadata

  • Download URL: casbin_fastapi_decorator_jwt-0.2.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","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 casbin_fastapi_decorator_jwt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 878ae84d9b64c02166fbc84a26192a40d76aa11a60047415f2b76cbb47b1b296
MD5 0a8744c6fd91bd53642b2c540dd93055
BLAKE2b-256 5de5b676f9fa8b526c0bbfd349e8c4ca440c523016a6e9dfc71276d041dfdaa9

See more details on using hashes here.

File details

Details for the file casbin_fastapi_decorator_jwt-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: casbin_fastapi_decorator_jwt-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","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 casbin_fastapi_decorator_jwt-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e520a2c79f169a9aa2f406a966940ffc8b47ec9718ac358fc72a06043db4910
MD5 c80b84ef806af48045f19aececb50313
BLAKE2b-256 491c729f69275d4707148e7b9c9ad187fbed4f8cafb6972fe8218ca78828cbfb

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