Skip to main content

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

Release files for casbin-fastapi-decorator-jwt 1.3.0

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

Source distribution (sdist)

Source distribution for casbin-fastapi-decorator-jwt 1.3.0
File Size Uploaded
casbin_fastapi_decorator_jwt-1.3.0.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for casbin-fastapi-decorator-jwt 1.3.0
File Interpreter ABI Platform
casbin_fastapi_decorator_jwt-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.9 kB

Release files / casbin_fastapi_decorator_jwt-1.3.0.tar.gz

Download URL casbin_fastapi_decorator_jwt-1.3.0.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9783002cc5e86206f017d7efa0e6c6fe01a32259b1563ae6d1b64493d1717fd7
BLAKE2b-256 checksum
How to use checksums
a7c81eae661d4b0b7597ca14fb2102ae7ac666c35aab8ab513545e5c038f0fea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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}

Release files / casbin_fastapi_decorator_jwt-1.3.0-py3-none-any.whl

Download URL casbin_fastapi_decorator_jwt-1.3.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ef9c0b9ddca22cca3b06be767fb8cd652140fb7c9299d1929850e31b944ec79
BLAKE2b-256 checksum
How to use checksums
6196ff37c3ea0a86027453ae11ab3e0a3795e4cfc4c2440989004d0374a1dc2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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}

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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