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
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
has_permission(table_name, action)
async
¶
Return True if the current user may perform action on table_name.
Actions: "view" | "create" | "edit" | "delete" | "export" | "import"
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.
Warning: This reads from the async-populated cache. If load_permissions()
was not called first, all values will be False (except for superusers).
Always call await load_permissions(table_name) before using this method.
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¶
The built-in admin models are materialized at runtime from the schemas in
fastapi_admin_kit.schemas.builtin. They live in fastapi_admin_kit.migrations.models
(the deprecated fastapi_admin_kit.auth.models module re-exports the same classes
and will be removed in v3.0).
User¶
fastapi_admin_kit.migrations.models.User = _backend.materialize(USER_SCHEMA, base=Base, schemas=BUILTIN_SCHEMAS)
module-attribute
¶
Role¶
fastapi_admin_kit.migrations.models.Role = _backend.materialize(ROLE_SCHEMA, base=Base, schemas=BUILTIN_SCHEMAS)
module-attribute
¶
Permission¶
fastapi_admin_kit.migrations.models.Permission = _backend.materialize(PERMISSION_SCHEMA, base=Base, schemas=BUILTIN_SCHEMAS)
module-attribute
¶
UserPermission¶
fastapi_admin_kit.migrations.models.UserPermission = _backend.materialize(USER_PERMISSION_SCHEMA, base=Base, schemas=BUILTIN_SCHEMAS)
module-attribute
¶
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
resolve_permission_checker(request)
async
¶
Resolve a PermissionChecker for the current request.
Returns None if the user is not authenticated or the checker cannot be built.
This is the canonical implementation — import from here, not from renderers/factory.
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
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 | |
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)
¶
Sign payload and return the signed token.
decode(token)
¶
Verify token and return the decoded payload dict, or None.
Tokens without an iat claim are rejected outright (S18): the
iat timestamp powers the password-change invalidation check, and
a token minted without it would silently bypass that revocation.
Source code in fastapi_admin_kit/auth/session.py
should_secure(request)
¶
Return True only when the configured secure flag is set AND the current request arrived over HTTPS. This prevents browsers from dropping the cookie on plain-HTTP dev servers.
Source code in fastapi_admin_kit/auth/session.py
load(token)
¶
encode_pending_2fa(user_id)
¶
Sign a short-lived token proving credentials were verified but 2FA is pending.
decode_pending_2fa(token)
¶
Verify a pending-2FA token and return the user_id, or None.
Source code in fastapi_admin_kit/auth/session.py
save(response, data, *, request=None)
¶
Encode data and set it as a signed cookie on response.