Auth¶
Auth Backend¶
fastapi_admin_kit.auth.backend.AuthBackend
¶
Bases: ABC
Abstract authentication backend — verify credentials & load users.
Source code in fastapi_admin_kit/auth/backend.py
authenticate(credential, password, session, login_field='email')
abstractmethod
async
¶
Verify credentials. Return user object if valid, None otherwise.
Source code in fastapi_admin_kit/auth/backend.py
get_user(user_id, session)
abstractmethod
async
¶
on_logout(user_id=None)
async
¶
Called after a user logs out. Override to perform cleanup.
Permission Checker¶
fastapi_admin_kit.auth.permissions.PermissionChecker
¶
Per-request permission checker.
Instantiated once per request via Depends(get_permission_checker).
Caches permission results in-memory for the lifetime of the request.
Merges permissions from: 1. All assigned roles (M2M via admin_user_roles) 2. Direct per-user overrides (UserPermission)
Role permissions are OR'd together, then direct overrides are OR'd on top.
Source code in fastapi_admin_kit/auth/permissions.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has_permission(table_name, action)
async
¶
Return True if the current user may perform action on table_name.
Actions: "view" | "create" | "edit" | "delete"
Superusers always return True. Results are cached per-request.
Source code in fastapi_admin_kit/auth/permissions.py
get_allowed_fields(table_name, mode)
async
¶
Return the set of field names the user may access, or None.
mode: "view" | "edit"
Semantics:
- None → no field-level restrictions exist → all fields allowed.
- Empty set() → restriction rows exist but none grant access → no fields.
- Non-empty set() → only those field names are permitted.
Source code in fastapi_admin_kit/auth/permissions.py
permission_set(table_name)
¶
Return a :class:PermissionSet for convenient template / UI use.
Note: This is a sync convenience wrapper. For async contexts, use the individual async methods directly.
Source code in fastapi_admin_kit/auth/permissions.py
load_permissions(table_name)
async
¶
Async method to load and cache all permissions for a table.
Call this before using permission_set() to ensure the cache is populated.
Source code in fastapi_admin_kit/auth/permissions.py
Models¶
User¶
fastapi_admin_kit.auth.models.User
¶
Bases: AuthModelMixin, Base
Admin user model with authentication support via AuthModelMixin.
The mixin provides: hashed_password, is_active, is_superuser columns, role_ids property, verify_password() and hash_password() methods.
Source code in fastapi_admin_kit/auth/models.py
Role¶
fastapi_admin_kit.auth.models.Role
¶
Bases: Base
Source code in fastapi_admin_kit/auth/models.py
Permission¶
fastapi_admin_kit.auth.models.Permission
¶
Bases: Base
Permission matrix per model — shared across roles via M2M.
Source code in fastapi_admin_kit/auth/models.py
UserPermission¶
fastapi_admin_kit.auth.models.UserPermission
¶
Bases: Base
Direct per-user permission overrides — merged with role permissions.
Source code in fastapi_admin_kit/auth/models.py
Dependencies¶
fastapi_admin_kit.auth.dependencies
¶
FastAPI dependencies — session, current user, permission checker.
get_session(request)
¶
Read and decode the admin session cookie from the current request.
Source code in fastapi_admin_kit/auth/dependencies.py
get_current_admin_user(request, session_payload=Depends(get_session))
async
¶
Resolve the logged-in admin user from the session cookie.
Raises 401 if the session is missing, invalid, the user is
no longer active, or the password was changed after the session was issued.
Source code in fastapi_admin_kit/auth/dependencies.py
get_permission_checker(request, user=Depends(get_current_admin_user), session=Depends(_get_db_session))
async
¶
Build a PermissionChecker for the current user.
The concrete PermissionChecker class is imported here to avoid
circular imports.
Source code in fastapi_admin_kit/auth/dependencies.py
require_permission(table_name, action)
¶
Return a FastAPI dependency that enforces a permission check.
Usage::
@router.get("/")
async def list_view(_=Depends(require_permission("products", "view"))):
...
Source code in fastapi_admin_kit/auth/dependencies.py
require_superuser(user=Depends(get_current_admin_user))
async
¶
FastAPI dependency that enforces superuser access.
The single source of the "must be superuser" rule, shared by the roles and audit views (previously copy-pasted in each).
Source code in fastapi_admin_kit/auth/dependencies.py
Session¶
fastapi_admin_kit.auth.session
¶
Session backend — ABC + signed-cookie implementation.
SessionBackend
¶
Bases: ABC
Abstract session backend — encode/decode session payloads.
Source code in fastapi_admin_kit/auth/session.py
SignedCookieSessionBackend
¶
Bases: SessionBackend
Session backend that signs a JSON payload using itsdangerous.
The cookie value is a URLSafeTimedSerializer-serialized string — the JSON
payload is base64-encoded so its output is safe for Cookie headers (unlike
raw JSON, which contains {, }, " and other characters that break
the http.cookies.SimpleCookie parser).
Source code in fastapi_admin_kit/auth/session.py
secret_key
property
¶
The signing key used by this backend.
Public accessor so CSRF / JWT signing can share the same key without reaching into the (private) itsdangerous signer. Swapping the session backend no longer silently breaks CSRF.
encode(payload)
¶
decode(token)
¶
Verify token and return the decoded payload, or None.