Authorization and Administration
This document explains Auth's two-tier authorization model, how super admins and realm admins differ, how to bootstrap the first administrator, and how to delegate administration of individual realms.
Overview
Every Admin record in the database represents an administrator โ either a super admin or a realm admin. There are no purely client-facing account types: if a Admin record exists it has administrative authority over at least one realm. Clients authenticate against /login and receive a session cookie, but the associated Admin record determines what administrative operations they may perform.
The two tiers are:
| Tier | Sentinel | Condition |
|---|---|---|
| Super Admin | Admin.realms contains "_" | Can administer any realm that has no admin of its own yet (plus always retains GET/DELETE on any realm โ see below) |
| Realm Admin | Admin.realms contains one or more realm IDs | Can administer only the listed realms (and their admins) |
graph TD
SA["Super Admin<br/>realms: [#quot;_#quot;]"]
RA1["Realm Admin A<br/>realms: [#quot;finance#quot;]"]
RA2["Realm Admin B<br/>realms: [#quot;hr#quot;, #quot;payroll#quot;]"]
NEW["Realm: new-service<br/>(just created, unclaimed)"]
SA -->|direct member| ADMIN_REALM["Realm: _"]
SA -->|"bootstrap: unclaimed"| NEW
SA -.->|"GET / DELETE only\n(permanent exemption)"| FINANCE["Realm: finance"]
SA -.->|"GET / DELETE only\n(permanent exemption)"| HR["Realm: hr"]
SA -.->|"GET / DELETE only\n(permanent exemption)"| PAYROLL["Realm: payroll"]
RA1 -->|direct member| FINANCE
RA2 -->|direct member| HR
RA2 -->|direct member| PAYROLL
Once new-service gets its first admin, it moves from the super admin's solid-line bootstrap access to the same dashed, GET/DELETE-only relationship shown for finance, hr, and payroll โ see Realm-Claim Lifecycle.
The _ (Admin) Realm
The string "_" is the ADMIN_REALM constant. It is a real realm stored in the database and has two purposes:
- Authentication domain โ all administrator clients log in via
POST /login?realm=_. The resulting_ea_session cookie is scoped to_and authorises all admin API calls. - Super-admin sentinel โ a
Adminrecord whoserealmsfield contains"_"is recognised as a super admin.
Realm-Claim Lifecycle
A realm is unclaimed from the moment it is created until it gets its first admin, and claimed from that point on. This is not a stored flag โ it is recomputed on every request by checking whether any Admin record's realms list already contains the realm.
| State | Who may manage its admins, credentials, and config (PUT on the realm) |
|---|---|
| Unclaimed | The super admin (bootstrap access) โ this is how the very first admin of a new realm gets created |
| Claimed | Only that realm's own admin(s). The super admin loses access to its credentials and can no longer change its admins, but can still read them (GET /admins, GET /admins/{id}) to contact them |
Two operations are exempt from this rule and stay available to the super admin permanently, regardless of claim status:
GET /admins/realms/{id}andGET /admins/realmsโ visibility is never revoked, so an operator can always see that a realm exists.DELETE /admins/realms/{id}โ a safety net so an abandoned realm (its admins gone, or nobody ever cleaning it up) can always be removed, even though the super admin can no longer configure or administer it day to day.
The claim is re-evaluated on every request rather than recorded once: if a realm's last admin is removed, the realm reverts to unclaimed and the super admin regains bootstrap access automatically โ no manual reset needed.
stateDiagram-v2
[*] --> Unclaimed: POST /admins/realms
Unclaimed --> Claimed: first admin added
Claimed --> Unclaimed: last admin removed
Claimed --> [*]: DELETE (always available\nto the super admin)
Unclaimed --> [*]: DELETE
Authorization Helper Methods
The Admin struct provides two static, database-less methods:
#![allow(unused)] fn main() { /// Returns true if this Admin record represents a super admin. pub fn is_super_admin(&self) -> bool { self.realms.contains(&ADMIN_REALM.to_string()) // "_" } /// Returns true if this Admin record directly belongs to the given realm /// (or is a super admin). A static check only โ see below. pub fn can_administer_realm(&self, realm: &str) -> bool { self.realms.contains(&ADMIN_REALM.to_string()) || self.realms.contains(&realm.to_string()) } }
can_administer_realm alone only answers "does this admin directly belong to this realm?" โ it has no way to know whether the realm has since been claimed by someone else, so it is not sufficient on its own for most endpoints. It remains exactly right for the two permanently-exempt operations above (GET/DELETE on a single realm), and it stays in the public client SDK as a lightweight, database-less hint (e.g. "should the UI show this button?").
Everywhere else, the server pairs it with two async, database-backed helpers that implement the realm-claim rule:
#![allow(unused)] fn main() { /// Whether `requester` may administer `realm_id` right now. Direct /// membership always grants access; a super admin who is not a direct /// member gets access only while the realm has no admin of its own yet. pub async fn can_manage_realm(requester: &Admin, realm_id: &str, database: &Data<Arc<dyn Database>>) -> Result<bool, AuthError>; /// Same, but for every realm an `Admin` record (or a create/update request /// body) lists โ the claim-aware form of the exclusive-ownership rule below. pub async fn can_manage_admin_realms(requester: &Admin, realms: &[String], database: &Data<Arc<dyn Database>>) -> Result<bool, AuthError>; }
Authorization Decision Flow
flowchart TD
A[Incoming request] --> B{Authenticated?\nUserAuth middleware}
B -- No --> Z[HTTP 401 Unauthorized]
B -- Yes --> C{Endpoint category}
C -- "Realm creation\nPOST /admins/realms" --> D{is_super_admin?}
D -- No --> E[HTTP 403 Forbidden]
D -- Yes --> F[Proceed]
C -- "Permanently exempt\nGET /admins/realms{,/{id}}\nDELETE /admins/realms/{id}" --> G{"can_administer_realm(realm)?\n(static โ super admin always true)"}
G -- No --> E
G -- Yes --> F
C -- "Everything else scoped to a realm\nPUT /admins/realms/{id}\n/realms/{realm}/โฆ, TOTP, /sessions/โฆ\nPUT|DELETE /admins/{id}/realms/{realm_id}" --> H{"Direct member\nof realm?"}
H -- Yes --> F
H -- No --> I{"is_super_admin AND\nrealm has no admin yet?"}
I -- No --> E
I -- Yes --> F
C -- "Admin CRUD\nPOST /admins,\nGET|PUT|DELETE /admins/{id}" --> OWN{"target.realms empty?\n(unaffiliated admin)"}
OWN -- Yes --> SAONLY{is_super_admin?}
SAONLY -- No --> E
SAONLY -- Yes --> F
OWN -- No --> CLAIM{"every realm in target.realms\npasses the check above?"}
CLAIM -- No --> E
CLAIM -- Yes --> PUTCHECK{"PUT only: every realm in\nthe new body also passes it?"}
PUTCHECK -- No --> E
PUTCHECK -- Yes --> F
C -- "GET /admins/realms\n(list)" --> LH{is_super_admin?}
LH -- Yes --> LI[Return all realms]
LH -- No --> LJ["Return only realms\nrequester administers"]
GET /admins returns every admin to a super admin, and GET /admins/{id} is readable by any super admin: seeing a claimed realm's admins is how the super admin contacts them. A realm admin gets from GET /admins the admins whose realms it all administers, itself included. GET /admins/userpass is filtered by the login rule.
Endpoint Authorization Matrix
Realm Management
| Method | Endpoint | Super Admin | Realm Admin |
|---|---|---|---|
POST | /admins/realms | โ | โ |
GET | /admins/realms/{id} | โ always ยน | โ
if can_administer_realm(id) |
PUT | /admins/realms/{id} | โ if unclaimed | โ if administers realm (once claimed) |
DELETE | /admins/realms/{id} | โ always ยน | โ if administers realm |
GET | /admins/realms | โ all ยน | โ filtered |
ยน GET and DELETE on a single realm, and GET on the list, are the operations permanently exempt from the realm-claim rule โ see Realm-Claim Lifecycle. They still use the static can_administer_realm, not the claim-aware checks below.
Admin Management
| Method | Endpoint | Super Admin | Realm Admin |
|---|---|---|---|
POST | /admins | โ ยฒ | โ ยฒ |
GET | /admins/{id} | โ always (read-only) | โ ยฒ |
PUT | /admins/{id} | โ ยฒ (current AND new) | โ ยฒ (current AND new) |
DELETE | /admins/{id} | โ ยฒ | โ ยฒ |
GET | /admins | โ all admins | โ the admins it owns ยฒ |
PUT | /admins/{id}/realms/{realm_id} | โ if realm unclaimed | โ if administers realm_id, and owns target ยณ |
DELETE | /admins/{id}/realms/{realm_id} | โ if realm unclaimed | โ if administers realm_id, and owns target ยณ |
ยฒ Subject to the claim-aware exclusive-ownership rule โ see below. Super admins and realm admins are now treated symmetrically here: a super admin qualifies for a non-empty realm list only while every realm in it is unclaimed, exactly like a realm admin needs to already administer it. ยณ Additionally requires the requester to already exclusively own every realm the target admin currently belongs to (skipped only if the target is unaffiliated) โ see below.
Credential Management
In a regular realm, all /realms/{realm}/userpass and /realms/{realm}/totp/* endpoints require can_manage_realm(realm) โ the claim-aware check: a super admin qualifies only while the realm has no admin of its own yet. In the admin realm _, the credentials are the admins' logins and follow the login rule instead, per login. 2FA exists only for admin logins in _; the TOTP endpoints answer 400 in any other realm.
| Method | Endpoint | Super Admin | Realm Admin |
|---|---|---|---|
POST | /realms/{realm}/userpass | โ if unclaimed | โ
if can_manage_realm(realm) |
GET | /realms/{realm}/userpass/{username} | โ if unclaimed | โ
if can_manage_realm(realm) |
PUT | /realms/{realm}/userpass/{username} | โ if unclaimed | โ
if can_manage_realm(realm) |
DELETE | /realms/{realm}/userpass/{username} | โ if unclaimed | โ
if can_manage_realm(realm) |
GET | /realms/{realm}/userpass | โ if unclaimed | โ
if can_manage_realm(realm) |
GET | /admins/userpass | โ (results filtered) | โ |
In realm _ every row above applies per login: โ
only for a login the caller manages (see the rule), and GET /realms/_/userpass is open to every admin but lists only the logins it manages. Password hashes are never returned in lists.
Session Management
All /sessions calls need a valid session cookie, but not an admin one: a caller always reaches its own sessions (same realm and username, whatever the login scheme), which is what a backend API server needs to log a user out. Admins reach more:
| Operation | Any client | Realm admin | Super admin |
|---|---|---|---|
GET/POST /sessions/{session_id} | โ own sessions | โ sessions of its realms | โ
realms without an admin of their own, and _ |
POST /sessions/realms/{realm}/clients | โ own username | โ any client of its realms | โ same |
DELETE /sessions (by ID) | โ own sessions | โ sessions of its realms | โ same |
DELETE /sessions/realms/{realm} | โ | โ its realms | โ
realms without an admin of their own, and _ |
DELETE /sessions/expired | โ | โ | โ |
The realm-claim rule applies as everywhere else: once a realm has its own admin, the super admin no longer reaches its sessions. A session the caller may not access is reported as not found, and a DELETE naming one skips it like a missing session. Session responses never include the cookie value, which is the session credential. Sessions cannot be created through the API.
Machine Authentication
/auth/approle/* and /auth/kubernetes/* role management (everything except the unauthenticated login endpoints) is for super admins only: the roles are global, not tied to a realm.
Public / unauthenticated
| Method | Endpoint | Anyone |
|---|---|---|
GET | /public/version | โ |
GET | /.well-known/jwks.json | โ |
POST | /login | โ |
GET | /whoami | โ
(no AdminAuth) |
The Exclusive-Ownership Rule
The most important protection on Admin records is the exclusive-ownership rule: nobody may CRUD an Admin record unless every realm in that record's realms list is one they administer. "Administer" here is the claim-aware check (Realm-Claim Lifecycle), not the static can_administer_realm โ this applies symmetrically to realm admins and super admins. A super admin only qualifies for a non-empty realm list while every realm in it is still unclaimed.
allowed iff:
target.realms.is_empty() // unaffiliated: super admin only
|| target.realms.iter().all(|r| requester_can_manage(r)) // claim-aware, see can_manage_admin_realms
The intent is:
- Nobody can view or modify an
Adminrecord that spans multiple realms unless they administer all of them โ including a super admin, once any of those realms has been claimed. - A realm admin cannot delete a super admin (
Adminrecords for super admins have"_"in theirrealmslist, and a realm admin never administers"_"). - An
Adminrecord with an emptyrealmslist is unaffiliated โ not yet claimed by anyone โ and only a super admin may create or manage it, regardless of what's claimed elsewhere.
PUT /admins/{id} runs the rule twice
Check 1 (current state): requester can own the user as it is now.
Check 2 (incoming body): requester can own the user as it would become.
This prevents privilege escalation: a realm admin cannot silently add "_" or a foreign/claimed realm to a user's realms list by providing it in the update body.
PUT/DELETE /admins/{id}/realms/{realm_id} also run it against the target
Granting or revoking realm membership (add_admin_to_realm / remove_admin_from_realm) additionally requires the requester to already exclusively own every realm the target admin currently belongs to โ skipped only when the target is still unaffiliated. Without this, a realm admin could unilaterally extend their realm onto an admin who also serves a foreign realm they don't control โ which, per the rule above, would then lock that foreign realm's own admin out of managing their own colleague.
Admin Sessions Come From the _ Realm
Admin endpoints (/admins/*, /realms/*, /auth/approle/*, /auth/kubernetes/*) only accept a session opened on the _ realm by an admin. A session of any other realm is rejected with 401, even when its username equals an admin's login: the admin lookup uses the username and login scheme of the session, and only the _ realm holds admin logins.
A _ session is not tied to /realms/_/โฆ: it authorises calls on /realms/{realm}/โฆ for every realm the admin may manage (see Realm-Claim Lifecycle).
Bootstrapping the First Super Admin
At startup the server creates whichever of these is missing:
- The
_realm, with username/password authentication andallow_expired_passwords: true. - A
UserPassentryadminin realm_, with the passwordchange_meandchange_password: true. - An
Adminrecordadminwithrealms: ["_"]anduserpass: "admin".
The username and initial password are fixed. Because _ accepts expired passwords, the
change_password flag does not block the login: change the password right after the first
start, as shown in Installation.
Creating a Realm Admin
Below is the step-by-step process for creating a realm admin for a realm named my_realm.
sequenceDiagram
autonumber
participant SA as Super Admin
participant EA as Authentication Verifier
SA->>EA: POST /admins/realms<br/>{"id":"my_realm","name":"My Realm",โฆ}
EA-->>SA: 201 Created
SA->>EA: POST /realms/_/userpass<br/>{"realm":"_","username":"alice","password":"<hashed>","change_password":false}
note over EA: Stores Argon2id hash of alice's password in realm _
EA-->>SA: 201 Created
SA->>EA: POST /admins<br/>{"id":"alice_user","realms":["my_realm"],"userpass":"alice"}
note over EA: Creates Admin record\nuserpass โ foreign key into userpass table
EA-->>SA: 201 Created
note over SA: Realm admin alice is ready.
SA->>EA: POST /login?realm=_<br/>{"username":"alice","password":"<plain>"}
EA-->>SA: 200 OK + Set-Cookie: _ea_=โฆ<br/>{"next_step":"Authenticated","session_id":"โฆ"}
SA->>EA: GET /admins/realms/my_realm<br/>Cookie: _ea_=โฆ
EA-->>SA: 200 OK โ realm details
What alice can now do
Alice's cookie (from POST /login?realm=_) authorises:
GET/POST/PUT/DELETE /realms/_/userpass/*for her own login and her colleagues' (the logins of admins whose realms she administers), and/realms/_/totp/*for the same loginsGET /admins/realms/my_realmPOST /adminswithrealms: ["my_realm"]GET /admins, listing the admins whose realms are allmy_realm(herself and her colleagues)- CRUD on any user whose
realmsis a subset of["my_realm"]
Alice cannot:
- Create realms, or delete a realm she doesn't administer
- Access
/admins/userpass(list all credentials), or see in/adminsany admin outsidemy_realm - Manage any other realm, or any other admin's login or 2FA (the super admin's included)
- Manage users whose
realmsincludes something other thanmy_realm
Note:
my_realmis now claimed. From this point on, the super admin who set Alice up also loses the ability to create further admins, manage credentials (Alice's own login and 2FA included), or update the config inmy_realmโ only Alice (and any admin she adds) can. The super admin can still read Alice's record and permanently retainsGET/DELETEonmy_realmitself (see Realm-Claim Lifecycle).
Promoting an Admin to Super Admin
Only a super admin can promote another admin to super admin, and only while that admin is still unaffiliated (empty realms) โ assign "_" to its realms list:
PUT /admins/{new_admin_id}
Content-Type: application/json
Cookie: _ea_=<super_admin_cookie>
{
"id": "new_admin_id",
"realms": ["_"],
"userpass": "new_admin_login"
}
Warning: This grants full administrative access to every realm and every user. Only perform this operation when necessary, and audit the super admin list regularly.
Limitation: this does not work for an admin that already exclusively belongs to a claimed realm (e.g. Alice above, once
my_realmhas been claimed) โ see Limitations and Known Caveats.
Credentials and the userpass Link
The Admin.userpass field is a username (not a password) naming the admin's login: a credential of the admin realm _. The record and the credential are linked only by that name, and the userpass table is keyed by (realm, username).
The _ Login Rule
An admin record uses the _ login u when its userpass is u or its id is u. To create, read, change or delete the login u, or its 2FA, the caller must be able to manage every admin record using u (the exclusive-ownership rule); when no record uses u, only a super admin may.
- A realm admin manages its own login and its colleagues' (they all belong exclusively to its realm), so realm admins are self-sufficient.
- The super admin manages the logins of super admins and of unaffiliated admins, but not those of the admins of a claimed realm.
- A decoy record does not help: the real owner of a login is in the set too.
The same rule decides what GET /realms/_/userpass and GET /admins/userpass list.
Linking an admin to a login
- Creating a
_credential links it to the admin record of the same id, unless that record already has a login. A link is never overwritten. POST/PUT /adminsmay set auserpassonly if the caller manages every other record using that login (a super admin when none does: it adopts the existing credential), and only if no other record is linked to it (409). Ajwt,fido2orclient_certificatealready used by another admin is refused the same way (403if the caller does not manage that admin, else409): a shared login would lock its admins out.- Deleting a
_credential clears the link of the records that used it.
Deleting an admin
Deleting an admin record deletes its own _ login, unless another record still uses it. Credentials with the same name in other realms are never touched.
Limitations and Known Caveats
GET /whoami has no AdminAuth
GET /whoami returns the caller's identity from the session cookie but does not use the AdminAuth middleware. It cannot return a full Admin record โ only the session claims (realm, username, and any custom claims) belonging to the authenticated client are available. It is not subject to realm-admin authorization checks.
No non-admin client accounts
There is no built-in concept of a client whose presence in the database does not confer administrative rights. Any Admin record that exists with at least one realm in its realms list is a realm admin for that realm. Applications that need non-admin client accounts should model that distinction at the application level, outside the Authentication Verifier.
Concurrent realm admin creation
Linking two admin records to the same login is refused with 409. The check is not transactional, but the database backs it: admin.userpass and admin.jwt have unique indexes, so of two simultaneous requests one fails with 409. At startup the server creates each index only if no two admins already share a value (a MySQL jwt index covers its first 255 characters). Otherwise it logs a warning and the column stays non-unique until the duplicates are fixed; a login shared by several admin records is refused at login and by the admin lookup (the admins using it cannot log in).
Realm-claim check is not transactional
can_manage_realm and can_manage_admin_realms read the current admin list, decide, and only then let the caller's write proceed โ there is no database transaction or lock tying the two together. Two concurrent super-admin requests can both observe an unclaimed realm and both act on it (e.g. both create an admin, or both mutate its credentials) before either one's write has made the realm visibly claimed to the other. The "only that realm's own admin(s) may act on a claimed realm" rule is therefore not strictly enforced under concurrent bootstrap. This matters only during the narrow window before a realm gets its first admin; mitigate by having a single operator bootstrap each realm rather than racing two super-admin sessions against the same new realm.
Promoting an existing realm-scoped admin to super admin is not possible via the API
PUT /admins/{id} with realms: ["_"] runs the exclusive-ownership check against both the admin's current realms and the new ones (see The Exclusive-Ownership Rule). If the target already exclusively belongs to a claimed realm (the normal state of any realm admin), the super admin no longer administers that realm and fails the first check; the target itself cannot pass the second check (administering "_") without already being a super admin. PUT/DELETE /admins/{id}/realms/{realm_id} are blocked the same way. In practice, promotion to super admin only works for an admin that is still unaffiliated (empty realms) โ see Promoting an Admin to Super Admin. There is currently no supported way to promote an existing realm admin directly; create a fresh unaffiliated Admin record and promote that one instead.