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:
Authorization: Bearer <token>header (always)- Cookie
<cookie_name>(ifcookie_nameis set)
Development
See the workspace README for setup instructions.
task jwt:lint # ruff + bandit + ty
task jwt:test # pytest
Metadata
Release files for casbin-fastapi-decorator-jwt 1.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| casbin_fastapi_decorator_jwt-1.3.1.tar.gz | 2.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| casbin_fastapi_decorator_jwt-1.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.9 kB
Release files / casbin_fastapi_decorator_jwt-1.3.1.tar.gz
| Download URL | casbin_fastapi_decorator_jwt-1.3.1.tar.gz |
|---|---|
| Size | 2.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9cac2db7242e712d4fae4632c72dc59742f84cd5f5ead5e6b81c4454c194b1fc
|
|
BLAKE2b-256 checksum How to use checksums |
d6b0640193ab5a59eae25b9762116745d0c857e35e4c944ac741ee21bee3e711
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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.1-py3-none-any.whl
| Download URL | casbin_fastapi_decorator_jwt-1.3.1-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ef0cd3090e569738c0d2acff9f2a61fcf1542710c31670c322e141b64c54455
|
|
BLAKE2b-256 checksum How to use checksums |
c5e8bc1c7a42b8b7370b16a570c2fd43c95f34528d5db043e48634863fca2e11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}
|