Identity and access control
RomM identity and access control links each request to a user or an anonymous kiosk visitor, then limits that request by its credentials and effective permissions. The system supports browser sessions, password and OIDC sign-in, OAuth bearer tokens, device pairing, and long-lived client tokens so both people and RomM clients can use the same protected API.
Identity model
RomM treats a user as the owner of account access, devices, client tokens, and user data. An administrator bypasses permission grants. A normal user receives access from a permission group and any user-specific changes, while kiosk mode supplies a read-only identity for an anonymous public display.
User accounts
The User identity records the account name, password verifier, enabled state, role, and permission-group link. RomM has only two roles: admin and user. Values other than an exact admin role resolve to user, so legacy or unknown role values do not gain administrator access. An enabled account can authenticate through its supported methods. A disabled account cannot create a session or use a token, even if an old credential is still present. Sensitive self-edits require proof of account ownership: password accounts must provide the current password, while passwordless OIDC accounts must use a sign-in session. Users cannot change their own role, disable themselves, or delete themselves. Credential changes revoke the affected account's sessions, and disabling or deleting an account closes its client access. Once an administrator exists, only an administrator can create another administrator or issue an administrator invite. RomM also prevents an update or deletion from removing the last administrator.
References: backend/…/user.py• L26-38 backend/…/user.py• L48-80 backend/…/hybrid_auth.py• L43-49 backend/…/user.py• L95-132 backend/…/user.py• L161-175 backend/…/user.py• L462-651 backend/…/user.py• L665-710 backend/…/users_handler.py• L14-43 backend/…/users_handler.py• L54-80 backend/…/users_handler.py• L90-127 backend/…/users_handler.py• L195-217
Devices and client identities
A device is a user-owned client record that describes where RomM access comes from. Browser sign-in creates or reuses a web device. Device authorization binds a newly approved client token to one device, which lets RomM track the client and lets the user manage that access as a unit.
References: backend/…/device.py• L18-39 backend/…/device.py• L42-72 backend/…/auth.py• L70-102 backend/…/device_auth.py• L188-240
Anonymous kiosk identity
Kiosk mode uses a synthetic user identity rather than a stored account. The authentication layer gives this identity read scopes only, and the fine-grained permission layer applies the same read-only limit so a write route cannot become public by mistake. This cap applies only to the kiosk guest; a user who signs in keeps the grants assigned to that account.
References: backend/…/user.py• L43-45 backend/…/user.py• L126-144 backend/…/hybrid_auth.py• L143-146 backend/…/permissions.py• L139-143 backend/…/permissions.py• L190-194
Authentication methods
RomM accepts more than one credential form at the request boundary. The authentication backend checks an existing session first, then Basic credentials or a Bearer token, and finally applies kiosk behavior when that mode is enabled. Invalid or unusable authorization credentials leave the request unauthenticated instead of selecting a partial identity. When a session and an authorization header appear together, the session remains the request identity; browser CSRF checks use that same session choice.
The following diagram shows how a credential becomes an authenticated request identity.
flowchart TD
Request[Incoming request] --> Session{Valid session cookie?}
Session -->|Yes| User[Enabled user]
Session -->|No| Header{Authorization header?}
Header -->|Basic| Password[Username and password]
Header -->|Bearer rmm_| Client[Client token]
Header -->|Bearer OAuth| OAuth[OAuth access token]
Password --> User
Client --> User
OAuth --> User
Header -->|No or unusable| Kiosk{Kiosk mode?}
Kiosk -->|Yes| Guest[Read-only kiosk identity]
Kiosk -->|No| Anonymous[Unauthenticated request]
Browser sessions
Password login and OIDC login both establish the same RomM browser session. The session stores the authenticated subject and a device id, while Redis stores the session data behind an HTTP-only cookie. Logout removes the session. A credential change clears all sessions for that user around the account update, and a revoked session is not written back by a request that was already in progress. Clearing a user's sessions also closes the related live login connections.
References: backend/…/auth.py• L39-73 backend/…/auth.py• L75-106 backend/…/auth.py• L242-316 backend/…/redis_session_middleware.py• L29-45 backend/…/redis_session_middleware.py• L46-116 backend/…/base_handler.py• L157-190 backend/…/base_handler.py• L312-358
Password and OIDC sign-in
The password flow verifies the account password before it creates a session or OAuth tokens. The OIDC flow requires a usable username and a verified email. When claims are missing from the ID token, RomM can read them from the provider's UserInfo endpoint only when its subject matches the ID token subject. The identity then maps to an enabled RomM user by its provider identity or email, while a changed provider subject cannot silently take over a linked account. When role claims are enabled, an allowed claim maps to admin or user; an identity with no allowed claim is rejected. Password reset links are short-lived and single-use. Invite links are also signed and consumed once when a new user registers.
References: backend/…/auth.py• L40-73 backend/…/auth.py• L109-239 backend/…/auth.py• L243-316 backend/…/base_handler.py• L192-310 backend/…/base_handler.py• L360-461 backend/…/base_handler.py• L582-801 backend/…/users_handler.py• L72-80
OAuth access and refresh tokens
The /api/token endpoint issues a short-lived access token and a longer-lived refresh token after password authentication. A refresh request consumes the old refresh token before it issues a new pair. Each token carries requested scopes, and RomM grants only the scopes that the user can hold. Resource requests accept access tokens, not refresh tokens.
References: backend/…/auth.py• L109-172 backend/…/auth.py• L174-239 backend/…/hybrid_auth.py• L118-141 backend/…/base_handler.py• L464-579
Device authorization
Device authorization lets a client with no credentials ask a user to approve access in the RomM web UI. The client receives a secret device code for polling and a shorter user code for the user-facing approval page. The pending state stays in Redis until approval, denial, or expiry. Approval creates the device and its bound client token only after the signed-in user selects scopes that fit their permissions.
The diagram shows the out-of-band approval path and the one-time token release.
sequenceDiagram
participant D as Device client
participant R as RomM API
participant U as Signed-in user
participant W as Web UI
D->>R: Start authorization with metadata and scopes
R-->>D: Device code, user code, expiry, poll interval
U->>W: Open pairing page with user code
W->>R: Read pending request
R-->>W: Device details and allowed scopes
U->>W: Approve selected scopes or deny
W->>R: Approve or deny request
D->>R: Poll with device code
R-->>D: Pending, denied, expired, or one-time client token
Pairing states
The flow has three meaningful states: pending, approved, and denied. The device endpoint is open because the client has no credential yet, so RomM rate-limits both initialization and polling. Pending requests have a hard ten-minute ceiling. An approved result is consumed once, which stops a second poll from recovering the raw token.
References: backend/…/device_auth.py• L67-106 backend/…/device_auth.py• L289-344 backend/…/device_auth.py• L19-28 backend/…/device_auth.py• L124-139 backend/…/device_auth.py• L164-227
Scope approval
The user approval screen can show only the intersection of the device's requested scopes and the user's scopes. RomM rejects an approval when the selected set is empty or exceeds that intersection. The resulting client token keeps the approved set and the device link.
References: backend/…/device_auth.py• L109-145 backend/…/device_auth.py• L148-180 backend/…/device_auth.py• L230-247
Client tokens
Client tokens provide a stable API credential for scripts, launchers, and other clients. A user creates a token with a name, an allowed scope set, and an optional expiry. RomM returns the raw token only when it is created or regenerated, stores only its hash, and records use and optional device ownership for later administration.
Token lifecycle
Users can list, regenerate, pair, and delete their own tokens. A token can expire after 30 days, 90 days, or one year, or it can have no expiry. RomM limits each user to 25 tokens. Administrators can view and remove tokens across users when their account has the required user-management access.
References: backend/…/client_tokens.py• L35-75 backend/…/client_tokens.py• L78-111 backend/…/client_tokens.py• L154-166 backend/…/client_tokens.py• L18-50 backend/…/client_token.py• L16-33
Token request use
Clients send a client token as a Bearer credential with the rmm_ prefix. RomM hashes the presented value and looks up the hash. It then checks expiry and account state before it computes effective scopes as the overlap between the token and the current user permissions. This means a later user permission change can reduce an existing token's access without issuing a new token. Successful use records token, linked-device, and user activity only when their debounce windows allow a write. This keeps access tracking current without making every authenticated request update the database.
References: backend/…/base_handler.py• L121-127 backend/…/hybrid_auth.py• L24-49 backend/…/hybrid_auth.py• L100-120 backend/…/user.py• L188-193
Pair-code exchange
The web UI can create a short-lived pairing code for an existing token. A client exchanges that code once to receive a regenerated raw token. The code is removed when exchanged and the exchange endpoint has its own rate limit, so the code is a brief handoff rather than a second credential.
References: backend/…/client_tokens.py• L114-151 backend/…/client_tokens.py• L59-70 backend/…/client_tokens.py• L116-150
Permission model
RomM uses two related permission views. Fine-grained grants decide the allowed action on an entity and whether the action is limited to owned data. Coarse OAuth scopes remain the request-level contract for existing routes and tokens. RomM derives the coarse view from the effective fine-grained grants so both layers describe the same access. The /permissions/me response exposes the same effective grants as global UI action keys and reports hidden platforms and ROMs for the client.
Roles, groups, and overrides
There are two account roles. Administrators bypass permission checks. Other users follow one permission group, or the server default group when no group is assigned. A group grants read, write, or delete actions over entities such as ROMs, platforms, collections, devices, and users. A per-user override can add or revoke one group capability, with the override taking precedence for that action. A grant override also sets whether the action is limited to owned resources. The same effective group supplies hidden-entity rules and ROM age settings when a user has no explicit group. Changes to a role, group, override, or hidden entity notify affected clients so their permission view can be refreshed.
References: backend/…/user.py• L26-38 backend/…/user.py• L75-80 backend/…/permission.py• L20-43 backend/…/permission.py• L63-79 backend/…/permission.py• L91-122 backend/…/permissions.py• L70-110 backend/…/permissions.py• L113-181 backend/…/permissions.py• L42-67 backend/…/permissions.py• L214-236 backend/…/permissions.py• L315-345 backend/…/permissions.py• L351-432
Scope vocabulary
Scopes are the coarse access labels used by route guards and bearer credentials. Read and write scopes cover the main RomM resources. User, task, and log scopes cover administrative areas. Self-service scopes for the current user's profile and per-ROM data stay available to authenticated non-admin users even when their group grants do not include broader library access. The scope projection preserves the legacy scope set for viewer and editor grants, while delete remains a fine-grained action because the coarse vocabulary has no delete scope.
References: backend/…/constants.py• L13-75 backend/…/permissions_map.py• L23-74 backend/…/permissions_map.py• L77-131 backend/…/permissions_map.py• L113-127
Permission response
The /permissions/me response gives the client a global action vocabulary that reflects backend-enforced access. Administrators receive all action keys. Other users receive action keys derived from their effective grants, including own-only grants, together with separate lists of hidden platform and ROM ids.
References: backend/…/permission.py• L1-10 backend/…/permission.py• L23-55 backend/…/permission.py• L57-149 backend/…/permissions.py• L69-77 backend/…/test_permissions_me.py• L24-96
Visibility limits
Admins can see all entities. A group or user can hide specific platforms or ROMs, set an age limit, or hide unrated ROMs. RomM applies one ROM visibility rule to both data queries and already loaded records, so platform hides, direct ROM hides, and age rules have the same result across list and detail paths. Firmware inherits the visibility of its platform. A hidden ROM or platform and its child resources appear as not found rather than forbidden, which avoids exposing that the entity exists. The visibility check also runs before a write or an outbound fetch so a hidden resource cannot trigger a side effect.
The same visibility rule also identifies hidden ROMs on visible platforms. Platform summaries and client-facing platform listings can therefore report only the ROMs the caller may see.
References: backend/…/permission.py• L140-172 backend/…/permissions.py• L58-87 backend/…/permissions.py• L145-181 backend/…/dependencies.py• L48-53 backend/…/dependencies.py• L118-166 backend/…/rom_visibility.py• L34-47 backend/…/rom_visibility.py• L55-137 backend/…/test_permissions_visibility.py• L75-331
Request enforcement
Request protection has a broad credential gate and a narrower resource decision. Authentication first attaches the user and the scopes that the presented credential may use. The route guard then returns 401 when there is no valid identity or 403 when the identity lacks a required scope. Handlers can add fine-grained action, ownership, and visibility checks after that first gate. Fine-grained permissions are resolved once per request and reused by these checks.
The enforcement layers narrow access from the request credential to the specific resource.
flowchart LR
Credential[Session or token] --> Identity[Enabled user and effective scopes]
Identity --> Route[Protected route scope check]
Route -->|Allowed| Permission[Fine-grained action and ownership check]
Permission --> Visibility[Entity visibility check]
Visibility --> Result[Resource response]
Route -->|No identity| Unauthorized[401]
Route -->|Missing scope| Forbidden[403]
Visibility -->|Hidden entity| NotFound[404]
Route and resource checks
Protected routes declare the coarse scopes needed for their operation. The request permission resolver runs once per request and supplies the fine-grained decision to handlers. Ownership can allow a user to act on their own resource, while library-wide grants allow action on resources owned by others. A write grant does not imply a delete grant. Admin checks protect permission administration and other high-impact operations.
References: backend/…/auth.py• L75-151 backend/…/dependencies.py• L1-16 backend/…/dependencies.py• L38-116 backend/…/permissions.py• L44-67 backend/…/permissions.py• L69-86 backend/…/test_permissions_visibility.py• L334-357
Session and request safeguards
Session cookies use HTTP-only and SameSite flags, with the Secure flag available for HTTPS deployments. CSRF protection covers browser requests while bearer-token exchanges bypass the browser-cookie check; the session identity takes precedence when both credential forms appear. The application also applies authentication middleware before it serves the API and WebSocket entry points. Redis tracks each user's active session ids so account changes can revoke them together, while session writes preserve a revocation made during an in-flight request.
References: backend/…/csrf_middleware.py• L19-95 backend/…/redis_session_middleware.py• L13-45 backend/…/redis_session_middleware.py• L46-116 backend/…/session_middleware.py• L17-143 backend/…/base_handler.py• L157-190 backend/…/hybrid_auth.py• L62-141
Related pages
Read Authentication and authorization for the sign-in methods, token flows, and permission checks in more detail. Read User and device administration for account, device, and token management workflows.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the fine-grained permission entities and actions aligned with the coarse scope projection. A mismatch can make route guards grant more or less access than the permission resolver.
- Keep the
/permissions/meaction-key mapping and global scope shape aligned with backend grants. The client uses this response as its view of the actions the backend enforces. - When adding or changing a credential type, update the authentication backend and the route security dependencies together. A credential that authenticates but receives no effective scopes can fail as a misleading authorization error.
- Preserve the one-time and expiry rules for password reset, invite, device authorization, and pairing codes. These values cross Redis state and API responses.
- Keep device authorization scope approval aligned with client-token creation. The token must contain only the set the approving user is allowed to grant.
- When changing user role, group, override, or hidden-entity writes, keep permission-change notifications and request-time resolution consistent so active clients do not show stale access.
- Keep ROM visibility rules aligned between query filters and loaded-resource checks. Age limits, unrated-ROM rules, platform hides, and direct ROM hides must produce the same result before a response or side effect.
- When changing session creation or invalidation, keep the browser cookie, Redis session record, and per-user session index in sync. Preserve revocation when a request writes its response after the session was cleared.
- Keep session precedence aligned between hybrid authentication and CSRF checks. A request with a browser session and an authorization header must not be authorized as one identity and CSRF-checked as another.
- When changing OIDC claim handling, keep ID-token and UserInfo subject checks aligned with email verification and account-link rules. A provider response must not change the identity linked to an existing account.
- When changing activity tracking for authenticated requests, keep token, device, and user debounce behavior aligned so access records stay useful without adding a database write to every request.
- When extending platform totals or listings, use the same visibility filter for hidden ROMs on visible platforms so counts and availability do not reveal restricted library data.
- When changing account self-service or user administration, keep proof-of-ownership checks, last-admin protection, session revocation, and client-token disconnects aligned with the account state change.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A disabled user presents a session, password, OAuth token, or client token | RomM rejects the identity instead of allowing access from an old credential | Authentication checks the current enabled state for every credential path |
| An OIDC response has an unverified email, a mismatched UserInfo subject, or no allowed application role | Sign-in is rejected instead of creating or updating an account with an untrusted identity or role | OIDC validation requires verified email, matching subjects, and approved role claims |
| A provider presents a new subject for an email already linked to another OIDC subject | Sign-in is rejected instead of silently handing the account to the new provider identity | RomM matches the stored provider identity and blocks a conflicting subject |
| A password reset or invite token is expired or used again | The account change or registration is rejected | Signed tokens carry an expiry and Redis marks each token id as valid only until its single use |
| A user changes a password, username, or email without the required account proof | The sensitive self-edit is rejected | Password accounts require the current password; passwordless OIDC accounts require a sign-in session |
| A user tries to change their own role, disable their own account, or delete their own account | The account cannot be used to remove its own access control or leave an unmanaged state | User management restricts self-role changes, self-disable, and self-delete |
| An update or deletion would remove the last administrator | The operation is rejected and administrator access remains available | The database operation checks the locked administrator set before it commits the change |
| A client polls device authorization too fast or exceeds the IP limit | The client receives slow_down or a rate-limit error and does not receive a token | Per-code pacing and per-IP rate limits protect the open endpoints |
| A user approves a scope outside the requested and allowed sets | Device approval fails and no bound token is created | Approval requires a non-empty subset of the user's effective scopes |
| A refresh token is used twice or a non-access JWT reaches a resource route | The request is rejected rather than creating or using a resource session | Refresh tokens are consumed and resource authentication requires an access-token type |
| A ROM exceeds the user's age limit or is unrated when unrated ROMs are hidden | The ROM is excluded from results and is reported as not found on protected detail paths | The shared visibility filter applies age and rating rules to queries and loaded records |
| A hidden ROM or platform is requested by an authenticated user | The entity is reported as not found, which prevents an existence leak | Visibility checks mask hidden entities as 404 responses |
| A hidden ROM child route is reached through a visible or writable parent path | The child is reported as not found and its data, file, or external operation is not reached | The visibility check validates the parent-child relationship before the route side effect |
| A user has a write grant but no delete grant | A delete request returns 403 instead of removing the entity | Delete routes require the distinct fine-grained delete action after the coarse write gate |
| A non-admin tries to create or invite an admin after an admin exists | The operation is rejected and cannot become a privilege-escalation path | User administration keeps admin creation restricted to administrators |
| A request finishes after its session was revoked | The revoked session is not restored in Redis or by a new cookie | Session refresh writes require the existing Redis record and use the per-user session index for bulk clearing |
| A request sends a browser session and a different bearer credential | The session identity is used consistently rather than allowing the header to switch the caller | Hybrid authentication resolves sessions first and CSRF skips bearer handling only when no session is active |
| A malformed or invalid authorization header is sent without a valid session | The request stays unauthenticated instead of falling through to a different credential identity | Hybrid authentication rejects unusable header credentials before kiosk fallback or route handling |
| A client makes repeated authenticated requests inside the activity debounce windows | RomM still serves the request without repeatedly writing token, device, or user activity rows | Loaded timestamps and conditional debounced updates limit activity writes |
| A platform summary uses all ROM rows instead of the caller's visibility rule | Counts or sizes expose ROMs hidden by direct, platform, or age rules | Platform totals and listings apply the shared ROM visibility filter |
Key files
| File | Why to read it |
|---|---|
| backend/main.py | Shows where authentication, sessions, CSRF protection, API routers, and WebSocket entry points meet. |
| backend/…/hybrid_auth.py | Shows how sessions, Basic credentials, OAuth tokens, client tokens, and kiosk mode become request identities. |
| backend/…/auth.py | Defines the route-level scope gate and the 401 versus 403 behavior. |
| backend/…/permissions.py | Defines effective permission resolution and the projection to the coarse scope view. |
| backend/…/permissions_map.py | Defines the mapping from fine-grained grants to legacy scopes and preserves the access model for viewer and editor groups. |
| backend/…/rom_visibility.py | Defines the shared ROM visibility rules for hidden platforms, hidden ROMs, age limits, unrated ROMs, and visible-platform totals. |
| backend/…/permission.py | Defines the client-facing action vocabulary and the /permissions/me view of effective grants and hidden entities. |
| backend/…/dependencies.py | Shows ownership, admin, action, and hidden-entity checks used after route authentication. |
| backend/…/auth.py | Covers browser login, logout, password and OIDC flows, and OAuth token issuance. |
| backend/…/device_auth.py | Covers the user-approved pairing flow from initialization through one-time token delivery. |
| backend/…/client_tokens.py | Covers token creation, listing, regeneration, pairing, exchange, and administration. |
| backend/…/permissions.py | Shows how administrators manage groups, overrides, hidden entities, and permission-change notifications. |
| backend/…/user.py | Shows account creation, invite registration, proof for sensitive self-edits, administrator safeguards, credential changes, and user self-service boundaries. |
| backend/…/users_handler.py | Shows case-insensitive account lookup, provider-identity matching, role filtering, transactional last-admin protection, account updates, deletion, and administrator queries. |
| backend/…/base_handler.py | Shows password reset and invite token rules, credential-change session revocation, OIDC claim and identity checks, and bearer-token validation. |
| backend/…/redis_session_middleware.py | Shows Redis-backed session loading, revocation, cookie refresh, and live login-session closure. |
| backend/…/csrf_middleware.py | Shows how browser state-changing requests use a CSRF token and how bearer requests avoid that cookie check. |
| backend/…/user.py | Defines account roles, role normalization, enabled state, permission-group membership, kiosk identity, and debounced activity tracking. |
| backend/…/device.py | Defines user-owned device records and the client identity used for tracking. |
| backend/…/client_token.py | Defines token ownership, hashed storage, expiry, use tracking, and device binding. |
| backend/…/permission.py | Defines the entity/action vocabulary, groups, overrides, and visibility records. |