Authentication & Permissions
HyperAdmin has two auth modes. With built-in auth the admin keeps its own users, groups and
permissions in the hyperadmin_* tables (User, Group, Permission) and signs users in with
SessionAuthBackend. With bring your own auth you pass Admin(auth=ExternalAuth(...)) and
the host application decides who the user is and what they may do; see
Bring Your Own Auth.
from hyperadmin import Admin
from hyperadmin.auth.permissions import ModelPermissionChecker, PermissionSyncService
from hyperadmin.auth.session import SessionAuthBackend
admin = Admin(
app,
engine=engine,
auth_backend=SessionAuthBackend(engine),
permission_checker=ModelPermissionChecker(engine),
permission_registry=PermissionSyncService(engine),
)
hyperadmin.auth.metadata() returns the built-in tables as an Alembic target_metadata.
Permission codenames
The admin asks for view_<model>, add_<model>, change_<model>, delete_<model>,
export_<model> (the list's Export menu, see Import and export) and, for a
custom action, action_<name>_<model> (for example action_archive_order). <model> is the
lowercased model class name, so two models with an action of the same name have separate
permissions.
A detail-page panel checks view_<model>_panel_<slug>, or the permission= it was declared with.
At startup PermissionSyncService creates, for every registered model:
- the five
view_,add_,change_,delete_andexport_codenames; - one
action_<name>_<model>codename per@actionmethod of itsModelAdmin; - the codename of every detail panel (
AdminOptions.panelsand@panelmethods); - the extra codenames listed in
ModelAdmin.permissions.
Existing rows are never changed or removed. Grant the new codenames to a group or a user like
the defaults. export_<model> is granted to no one: until you grant it, only superusers can
export a list.
Earlier docs said action_<name>
The model-level check has always been action_<name>_<model>; earlier versions of these
pages named it action_<name>. The object-level check
(AdminOptions.object_permission_checker) receives action_<name>, without the model,
because the checker is already given the object.
Extra codenames: ModelAdmin.permissions
A ModelAdmin can declare codenames of its own, for checks you make in your own code (an
action handler, a custom view, a template). Set permissions to a list of
(codename, name) pairs; PermissionSyncService creates them at startup and the permissions
matrix shows them as extra columns of the model's row:
class OrderAdmin(ModelAdmin):
permissions = [
("approve_order", "Can approve orders"),
("refund_order", "Can refund orders"),
]
Check one with the admin's permission checker, for example
await admin.permission_checker.has_permission(user, "refund_order").
Permissions matrix
With built-in auth (or bring-your-own-auth with register_auth_models=True), the "Access"
sidebar group has a permissions matrix at {prefix}/permissions-matrix/: one row per model, one
column per codename, for the group you pick. A save writes only the cells that changed. When
someone else saved the same group in the meantime, the page shows a conflict banner instead of
overwriting their change. Every change is written to the hyperadmin_permission_audit table.

Superusers can always open it; other users need the permissions_matrix_codename permission
(change_groups_permissions by default). permissions_matrix_enabled=False leaves the page
out. The tables it needs are listed in
Upgrading the built-in auth tables.
Caches
ModelPermissionChecker remembers the permission set it read on the user object for the rest of
the request; call forget_user_permissions(user) after a write that changes it. With
user_cache_ttl set, the signed-in user is also cached per worker; call
invalidate_user(user_id) after you change a user outside the admin.
API Reference
hyperadmin.auth.ExternalAuth
dataclass
Bring-your-own-auth configuration for Admin(auth=...).
Only get_user is required. Every other field has a fail-safe default;
see docs/specs/bring-your-own-app.md section B.3.
Source code in src/hyperadmin/auth/bridge.py
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 | |
hyperadmin.auth.TokenCookie
dataclass
Bridges a bearer token into an HttpOnly cookie for bearer-only hosts.
Opt-in: set ExternalAuth(token_cookie=TokenCookie()). See
docs/specs/bring-your-own-app.md section B.4.
Source code in src/hyperadmin/auth/bridge.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
hyperadmin.auth.session.SessionAuthBackend
Cookie-session authentication backed by an async engine or session factory.
Source code in src/hyperadmin/auth/session.py
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 | |
authenticate(username, password)
async
Return the user if credentials are valid, else None.
Source code in src/hyperadmin/auth/session.py
40 41 42 43 44 45 46 47 48 49 50 51 | |
get_current_user(request)
async
Resolve the current user from the session, or None.
Source code in src/hyperadmin/auth/session.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
login(request, user)
async
Start a fresh session holding only the user ID.
Pre-login state (a stale partial-auth marker, OTP records, or keys planted in the cookie) is dropped, so a privilege change never carries it into the authenticated session.
Source code in src/hyperadmin/auth/session.py
53 54 55 56 57 58 59 60 61 | |
logout(request)
async
Clear the whole session, not just the user ID.
Source code in src/hyperadmin/auth/session.py
63 64 65 | |
hyperadmin.auth.permissions.ModelPermissionChecker
Checks user permissions via direct grants and group inheritance.
Source code in src/hyperadmin/auth/permissions.py
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 | |
get_user_permissions(user)
async
Return the union of direct + group-inherited permission codenames.
The set is read once per user object and remembered on it, so every later check for the same object (one request) costs no query. The caller gets its own copy.
Source code in src/hyperadmin/auth/permissions.py
186 187 188 189 190 191 192 | |
has_permission(user, codename)
async
Return True if the user holds the given permission.
Source code in src/hyperadmin/auth/permissions.py
180 181 182 183 184 | |
hyperadmin.auth.permissions.PermissionSyncService
Syncs auto-generated and custom permissions to the database on startup.
Source code in src/hyperadmin/auth/permissions.py
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 | |
sync_codenames(permissions)
async
Create missing permissions that belong to no model (e.g. plugin codenames).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
permissions
|
Iterable[tuple[str, str, str | None]]
|
|
required |
Source code in src/hyperadmin/auth/permissions.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
sync_permissions(registered_models)
async
Create missing permissions for all registered models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registered_models
|
list[Any]
|
List of |
required |
Source code in src/hyperadmin/auth/permissions.py
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 | |
hyperadmin.auth.permissions.forget_user_permissions(user)
Drop the permission set remembered on user; the next check reads it again.
Call it after a write that may change what this user object holds while the object is still in use (the permissions matrix does, after a save).
Source code in src/hyperadmin/auth/permissions.py
56 57 58 59 60 61 62 63 64 | |
hyperadmin.auth.user_cache.invalidate_user(user_id)
Drop user_id from every cache in this process.
Source code in src/hyperadmin/auth/user_cache.py
125 126 127 128 | |
hyperadmin.auth.metadata()
Return a new MetaData holding only the built-in hyperadmin_* tables.
Intended as an Alembic target_metadata (or one of several) for hosts
that use built-in auth (Admin(auth_backend=...))::
import hyperadmin.auth
target_metadata = [Base.metadata, hyperadmin.auth.metadata()]
The returned object is a copy: host tables are never included, and changing
it does not affect SQLModel.metadata.
Note
Calling this imports hyperadmin.auth.models. SQLModel table classes
register on the global SQLModel.metadata when they are imported, so
after this call the hyperadmin_* tables are also part of that
process's SQLModel.metadata (and of any autogenerate target built
from it). Hosts that bring their own auth should not call it; doing so
after a bridge-mode Admin was built logs a WARNING.
Source code in src/hyperadmin/auth/__init__.py
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 | |
hyperadmin.auth.User
Bases: SQLModel
User model for authentication.
Source code in src/hyperadmin/auth/models.py
12 13 14 15 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 | |
oauth_only
property
True when no password can sign this user in (created by an OAuth sign-in).
hyperadmin.auth.Group
Bases: SQLModel
Group model for organizing users and assigning permissions.
Source code in src/hyperadmin/auth/models.py
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 | |
hyperadmin.auth.Permission
Bases: SQLModel
Permission model with unique codename for authorization checks.
Source code in src/hyperadmin/auth/models.py
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 | |
OAuth sign-in
These names need the oauth extra and are imported from hyperadmin on first access.
hyperadmin.auth.oauth.OAuthBackend
OAuth2/OIDC sign-in for the built-in users.
Pass it to Admin(oauth_backend=...) next to auth_backend=::
Admin(
app,
engine=engine,
auth_backend=SessionAuthBackend(engine=engine),
oauth_backend=OAuthBackend(
providers={"google": google_provider(client_id, client_secret)}
),
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
providers
|
Mapping[str, OAuthProviderConfig]
|
Provider configurations by name; each key must equal its
configuration's |
required |
session_factory
|
SessionFactory | None
|
Session factory to store users and tokens with. When
neither it nor |
None
|
engine
|
Any
|
An async SQLAlchemy engine, as an alternative to |
None
|
tenant_resolver
|
Callable[[Any], Any] | None
|
Maps the value of a provider's |
None
|
transport
|
Any
|
|
None
|
timeout
|
float
|
Seconds a provider call may take. |
10.0
|
Raises:
| Type | Description |
|---|---|
OAuthConfigError
|
No provider, a key that differs from its provider name,
or the |
Source code in src/hyperadmin/auth/oauth/backend.py
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
authorization_url(provider, redirect_uri, state, code_verifier)
async
Return the provider URL the browser is sent to, with state and PKCE.
Source code in src/hyperadmin/auth/oauth/backend.py
221 222 223 224 225 226 227 228 229 230 231 232 | |
bind(*, engine=None, session_factory=None)
Use engine / session_factory unless the backend already has its own.
Source code in src/hyperadmin/auth/oauth/backend.py
193 194 195 196 197 | |
exchange_code(provider, code, redirect_uri, code_verifier=None)
async
Exchange an authorization code for tokens.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The token response: |
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
OAuthProviderError
|
The provider rejected the code or was unreachable. |
Source code in src/hyperadmin/auth/oauth/backend.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
get_userinfo(provider, access_token)
async
Return the provider's profile of the token's user.
When the profile has no email and the provider has an emails_url
(GitHub), the primary verified address is looked up and added.
Raises:
| Type | Description |
|---|---|
OAuthProviderError
|
The provider refused the token or was unreachable. |
Source code in src/hyperadmin/auth/oauth/backend.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
identity(provider, userinfo)
Validate userinfo and return the identity it describes.
Raises:
| Type | Description |
|---|---|
OAuthProviderError
|
No stable id, no usable email, an unverified
email or a |
Source code in src/hyperadmin/auth/oauth/backend.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
link_identity(provider, user_id, userinfo, token)
async
Link this identity to the signed-in user user_id and store its tokens.
A provider the user linked before is re-pointed to the new identity.
Raises:
| Type | Description |
|---|---|
AccountLinkRequiredError
|
The identity is linked to another user. |
OAuthProviderError
|
|
Source code in src/hyperadmin/auth/oauth/backend.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
linked_providers(user_id)
async
Return the names of the providers user_id has linked.
Source code in src/hyperadmin/auth/oauth/backend.py
452 453 454 455 456 | |
provider(name)
Return the provider configured as name, or None.
Source code in src/hyperadmin/auth/oauth/backend.py
199 200 201 | |
provision_user(provider, userinfo, token)
async
Return the user of this identity, creating it on the first sign-in.
A known (provider, sub) returns its user and stores the new tokens.
An unknown identity creates a user with no usable password, unless its
email already belongs to a user: accounts are never linked by email.
Raises:
| Type | Description |
|---|---|
AccountLinkRequiredError
|
The email belongs to an existing user. |
AccountDisabledError
|
The identity's user is not active. |
OAuthSignupDisabledError
|
Unknown identity and |
OAuthProviderError
|
|
Source code in src/hyperadmin/auth/oauth/backend.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
refresh_token(provider, user_id, *, leeway)
async
Make sure the user's access token outlives leeway seconds.
Asks the provider for a new access token when the stored one expires within
leeway seconds. Concurrent calls for one user and provider run one after
another, so a rotating refresh token is used once.
Returns:
| Type | Description |
|---|---|
float | None
|
The expiry of the (possibly new) access token as epoch seconds, or |
float | None
|
|
float | None
|
refresh token. |
Raises:
| Type | Description |
|---|---|
OAuthProviderError
|
The refresh failed. |
Source code in src/hyperadmin/auth/oauth/backend.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
unlink_identity(provider, user_id)
async
Delete the user's link to provider; False when there was none.
Raises:
| Type | Description |
|---|---|
LastSignInMethodError
|
The user has no usable password and no other linked provider. |
Source code in src/hyperadmin/auth/oauth/backend.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
hyperadmin.auth.oauth.OAuthProviderConfig
dataclass
One OAuth2/OIDC identity provider.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Lower-case name used in URLs and stored on |
client_id |
str
|
OAuth client id issued by the provider. |
client_secret |
str
|
OAuth client secret (left out of |
scopes |
tuple[str, ...]
|
Scopes requested at authorization. |
authorize_url |
str
|
Authorization endpoint. |
token_url |
str
|
Token endpoint (code exchange and refresh). |
userinfo_url |
str
|
Endpoint returning the signed-in user's profile as JSON. |
sub_claim |
str
|
Userinfo key of the stable user id (OIDC |
email_claim |
str
|
Userinfo key of the email address. |
tenant_id_claim |
str | None
|
Userinfo key (Google |
button_label |
str | None
|
Text after "Sign in with" on the login page; defaults to the
capitalised |
button_icon |
str | None
|
URL of an image shown on the button, used as is. |
redirect_uri |
str | None
|
Callback URL registered at the provider. |
emails_url |
str | None
|
Endpoint listing the user's addresses, asked when userinfo carries no email (GitHub accounts with a private address). |
authorize_params |
tuple[tuple[str, str], ...]
|
Extra query parameters of the authorization request. |
required_claims |
tuple[tuple[str, str], ...]
|
Userinfo |
allow_signup |
bool
|
|
Raises:
| Type | Description |
|---|---|
OAuthConfigError
|
A required value is empty or malformed (a |
Source code in src/hyperadmin/auth/oauth/providers.py
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 | |
label
property
The provider's display name on the login button ("Google").
hyperadmin.auth.oauth.github_provider(client_id, client_secret, *, scopes=GITHUB_SCOPES, redirect_uri=None, allow_signup=True)
Build the GitHub provider (OAuth2 with GitHub's own userinfo API).
GitHub is not OIDC: the stable user id is the numeric id of /user, and
an account with a private email is resolved through /user/emails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
Client id of the GitHub OAuth app. |
required |
client_secret
|
str
|
Its client secret. |
required |
scopes
|
tuple[str, ...]
|
Scopes to request; |
GITHUB_SCOPES
|
redirect_uri
|
str | None
|
See :class: |
None
|
allow_signup
|
bool
|
See :class: |
True
|
Source code in src/hyperadmin/auth/oauth/providers.py
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 | |
hyperadmin.auth.oauth.google_provider(client_id, client_secret, *, hosted_domain=None, tenant_id_claim=None, scopes=GOOGLE_SCOPES, redirect_uri=None, allow_signup=True)
Build the Google (OIDC) provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
OAuth client id from the Google Cloud console. |
required |
client_secret
|
str
|
Its client secret. |
required |
hosted_domain
|
str | None
|
A Google Workspace domain ( |
None
|
tenant_id_claim
|
str | None
|
See :class: |
None
|
scopes
|
tuple[str, ...]
|
Scopes to request. |
GOOGLE_SCOPES
|
redirect_uri
|
str | None
|
See :class: |
None
|
allow_signup
|
bool
|
See :class: |
True
|
Source code in src/hyperadmin/auth/oauth/providers.py
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 | |
hyperadmin.auth.oauth.OAuthProviderError
Bases: HyperAdminAuthError
An identity provider refused a request, answered garbage or was unreachable.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
Name of the provider ( |
|
detail |
Human-readable description; never contains a token or a secret. |
|
error_code |
The provider's OAuth |
|
transient |
|
Source code in src/hyperadmin/auth/exceptions.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
hyperadmin.auth.oauth.AccountLinkRequiredError
Bases: HyperAdminAuthError
The identity cannot be used as is: an account conflict needs an explicit link.
HyperAdmin never links an identity to an existing account by email alone.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
Name of the provider the identity comes from. |
|
email |
The email address the provider reported. |
Source code in src/hyperadmin/auth/exceptions.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |