This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Database-Backed Users
By default, every user lives in the users array in config.json, hand-edited. If you're running more than one instance, that means keeping every instance's file in sync by hand. databases is an optional alternative (or complement): point every instance at the same PostgreSQL or MySQL/MariaDB database, and they all read and write the same user base.
This page covers the mechanics — schema, sync model, connection handling, deletion detection, the local fallback cache. For the config fields themselves, see config.json Reference § databases.
"databases": {
"type": "postgres",
"host": "127.0.0.1",
"port": 5432,
"db_name": "proxyauth",
"user": "proxyauth",
"password": "changeme",
"connect_timeout_secs": 5,
"refresh_interval_secs": 30,
"incremental_window_secs": 300,
"full_refresh_interval_secs": 300,
"deleted_retention_secs": 86400,
"purge_interval_secs": 3600
}
What happens at startup
- ProxyAuth connects and creates the schema if it doesn't exist yet (
CREATE TABLE IF NOT EXISTS, plus migrations for existing installs — see Schema below). - It loads every non-deleted user found there, and mirrors that snapshot to a local LMDB cache (see Local fallback cache below).
- Database users are combined with file-based
users: both lists are checked on login, file-basedconfig.jsonentries are matched first if the same username exists in both. - If the database is unreachable, ProxyAuth falls back to that local cache if one exists (from a previous successful connection), or otherwise starts with zero database-backed users — file-based
usersalways keep working either way. A database outage never blocks startup.
Connection lifecycle
ProxyAuth opens one database connection and reuses it for the rest of the process's lifetime, rather than connecting fresh for every scan tick, CLI command, or password-reset write. The first operation to touch the database establishes it; every operation after that reuses the same connection.
flowchart TD
A(["First DB call — any of them:<br/>scan tick, CLI command, password reset"]) --> B{"Connection<br/>already open?"}
B -->|no| C["connect() — bounded by connect_timeout_secs,<br/>capped against pile-up (see below)"]
C --> D[("Connection stored,<br/>reused by every call after this")]
B -->|yes| D
D --> E["Operation runs"]
E --> F{"Succeeded?"}
F -->|yes| G(["Connection stays open for the next call"])
F -->|no| H["Connection dropped"]
H --> I(["Next call reconnects — once,<br/>not the failing call itself"])
classDef edge fill:#eef2ff,stroke:#4f46e5,stroke-width:1px,color:#1e1b4b
classDef store fill:#fef3c7,stroke:#d97706,stroke-width:1px,color:#78350f
classDef step fill:#f8fafc,stroke:#64748b,stroke-width:1px,color:#0f172a
classDef decision fill:#fde68a,stroke:#d97706,stroke-width:1px,color:#78350f
classDef done fill:#dcfce7,stroke:#16a34a,stroke-width:1px,color:#14532d
class A edge
class B,F decision
class C,E,H step
class D store
class G,I done
A few things worth knowing about this design:
- One connection at a time, process-wide. Every database operation is serialized behind an internal lock — a deliberate trade-off for ProxyAuth's actual workload (periodic scans a few times a minute, occasional CLI/admin use, occasional password resets), in exchange for a simple, predictable connection lifecycle. It's not built for high database concurrency; if that's ever needed, a real connection pool (e.g.
diesel::r2d2) would be the way to get it. connect_timeout_secs(default5) bounds how long any single connection attempt is allowed to take before giving up, rather than hanging for however long the OS's default TCP timeout happens to be (often 30–130s+, especially against a host that accepts the TCP handshake but never actually responds). This matters beyond just "logins are slow" — an in-flight connection attempt when the process receives a stop signal is exactly the kind of thing that can make aservice proxyauth restartappear to hang.- A failed operation drops the stored connection, so the next call reconnects from scratch rather than silently reusing something broken — but only the next call, not the one that just failed, and not by retrying in a loop.
- Reconnection attempts are capped. If the database stays unreachable for a while, an internal limit prevents multiple abandoned connection attempts from piling up faster than they can naturally resolve — without this, a long outage could accumulate more and more stuck background attempts over time, degrading things further the longer the outage lasts rather than staying flat.
Schema
allow (IP/CIDR rules), roles, groups, and email are one-to-many per user, so each lives in its own table with a real foreign key back to users.id, rather than a delimited string packed into a column (groups goes one step further — user_groups.group_id is itself a foreign key into a groups table, not a free-text column, so a group is a real row with its own identity, not just a string repeated on every member; see below). users also tracks created_at/modified_at (used by the incremental scan), deleted (soft-delete flag), must_change_password, and anti_replay_secret (ciphertext HMAC secret for the anti-replay plugin, NULL until generated):
erDiagram
users ||--o{ user_allow : "1 row per rule"
users ||--o{ user_roles : "1 row per role"
users ||--o{ user_email : "1 row per address"
users {
bigint id PK
string username
string password
string otpkey
timestamp created_at
timestamp modified_at
bool deleted
bool must_change_password
string anti_replay_secret
}
user_allow {
bigint user_id FK
string cidr
}
user_roles {
bigint user_id FK
string role
}
user_email {
bigint user_id FK
string email
bool is_primary
}
groups is one step removed from user_allow/user_roles/user_email above — group membership is still a one-row-per-relationship child table, but the group name itself lives in its own parent table, referenced by a genuine foreign key (user_groups.group_id), rather than being repeated as text on every membership row:
erDiagram
users ||--o{ user_groups : "membership"
groups ||--o{ user_groups : "membership"
users {
bigint id PK
}
groups {
bigint id PK
string name "UNIQUE"
}
user_groups {
bigint id PK
bigint user_id FK
bigint group_id FK
}
Two practical reasons this matters, over just repeating the group name as text the way user_roles.role does:
- A typo can't create a phantom group. Misspelling a group name on one account doesn't silently create a group nothing — and no route — will ever match against; assigning a user to a group that doesn't exist yet creates it via an atomic upsert (
ensure_group_idindatabases/db.rs), never a separate check-then-create with a race between them. - Renaming a group is a one-row
UPDATE. With a shared parent table, renaming affects every member instantly, instead of a mass-update across every membership row.
Deleting a user cascades to their user_allow/user_roles/user_groups rows automatically; the groups row itself is untouched — a group definition outlives any one member. See Group- and Role-Based Access Control for how groups (and roles, now also usable the same way) actually gate route access, not just this storage layer.
A deleted_users_log table (username, deleted_at) sits alongside it, populated by a trigger — see Deletion detection.
Sync model: incremental scan + full scan
Two independent periodic scans keep the in-memory user list current:
- Incremental (
refresh_interval_secs, default 30s): a cheap, indexed query scoped toincremental_window_secs(default 5 min) —WHERE modified_at >= cutoff. This is what picks up a newly created/edited user quickly, and its cost scales with how much actually changed recently, not with the total number of users. - Full (
full_refresh_interval_secs, default 5 min): reads the entireuserstable. With the incremental scan and the deletion trigger (below) handling the common cases, this now mainly exists as a safety net against one specific blind spot:TRUNCATE TABLE usersdoesn't fire row-level triggers, so no incremental scan can ever see it — only a full scan notices a table that's simply gone quiet.
Keep incremental_window_secs at least as long as refresh_interval_secs, with some overlap, so a change can never fall in the gap between two scans.
Deletion detection
Deleting a user is a soft delete, not a raw SQL DELETE: it sets deleted = TRUE and bumps modified_at, rather than removing the row outright. This is deliberate — it's what lets the fast incremental scan see a deletion at all. A hard DELETE leaves nothing behind for a WHERE modified_at >= cutoff query to find; an UPDATE does.
proxyauth db-delete-user --username bob
- The user's slot in memory is revoked immediately on the next incremental scan (typically within
refresh_interval_secs, default 30s) across every connected instance — not just the instance the command was run on. - The row itself is kept (for an audit trail, and to give every instance a chance to notice) until
deleted_retention_secselapses, at which point it's permanently purged (ON DELETE CASCADEcleans up the matchinguser_allow/user_rolesrows). - Re-adding the same username with
db-add-userbefore the purge revives the account (resetsdeletedtoFALSE) rather than leaving it silently revoked underneath the "new" data.
For a raw DELETE FROM users run directly against the database (bypassing db-delete-user), a trigger logs the username into deleted_users_log, which the incremental scan also checks — so even a manual hard delete is caught within the incremental window, not just the slower full scan. The one thing that bypasses the trigger entirely is TRUNCATE TABLE users, which is why the full scan (full_refresh_interval_secs) still runs as a backstop.
Technical note: why a deleted user's slot is poisoned, not removed
ProxyAuth's session tokens embed a numeric index into the combined user list (file users first, then database users, in a stable append-only order), not a username, for fast validation. Because of that, a revoked slot is never deleted or reordered internally — removing it would shift the index every entry after it, which every other issued token still relies on, potentially making some other still-valid token resolve to the wrong user entirely. Instead, the slot's password is overwritten with a value that can never verify again, and its index is recorded as revoked so any token pointing at it is rejected outright. This is invisible in normal operation — it's what makes the near-immediate revocation described above safe rather than just fast.
Local fallback cache (when the database is down)
Every successful full read of the database is mirrored to a local LMDB store (/opt/proxyauth/db/db_users_cache/ by default, overridable via the PROXYAUTH_DB_CACHE_PATH environment variable). If the database is unreachable — including at startup, not just mid-session — ProxyAuth uses that cached snapshot instead of coming up with zero database-backed users.
The cache is otherwise read-only from the application's point of view: db-add-user/db-delete-user always require a live connection to the real database and refuse outright if it's unreachable, rather than writing to the cache. The one exception is TOTP enrollment and reset (/adm/auth/totp/get//adm/auth/totp/reset) — those patch just the otpkey field of the cached entry for that one user immediately after the real database write succeeds, specifically so a database outage occurring shortly afterward can't cause ProxyAuth to fall back to a stale, since-replaced secret; see TOTP (2FA) Enrollment & Reset § The LMDB fallback cache. Best-effort like the rest of this cache — a failure to patch it is logged, never surfaced as a failure of the enrollment/reset itself, since the real database write already succeeded by that point.
A cached snapshot is also never used to conclude "this user was deleted" — only a genuinely fresh read of the real database triggers that comparison, since the cache can lag behind a user created moments ago via the incremental scan; treating it as authoritative for deletions could revoke someone who was never actually removed.
Manage it directly from the CLI when needed — proxyauth db-sync-cache [--force] to force an immediate refresh from the database, proxyauth db-clear-cache to wipe it, and proxyauth db-restore-from-cache [--force] to repopulate an empty/broken database from it. See CLI Reference for details on each.
Both this cache's directory and the password-reset token store are created with 0700 permissions (owner-only) — see Password Reset & First-Login Forced Change § Token storage.
Managing users via the CLI
proxyauth db-add-user --username admin \
--password supersecret \
--email admin@example.com \
--primary-email admin@example.com
Creates, updates, or revives a (possibly soft-deleted) user directly in the configured database. If --password is omitted, you're prompted for it interactively (hidden input, never touches shell history); it's hashed with Argon2 before being written. --email is repeatable for more than one address; --primary-email picks which one is used for proxyauth reset-password. Add --must-change-password to force the account to change this password on its next login — see Password Reset & First-Login Forced Change.
proxyauth db-delete-user --username bob
Soft-deletes a user — see Deletion detection above.
Both db-add-user and db-delete-user require a live database connection and print a clear error (rather than falling back to the cache) if it's unreachable. This command is authoritative, not a merge — every run reflects the account's complete intended state as given, not a partial patch; re-running db-add-user without --email clears any email(s) already on file, and without --must-change-password clears a pending forced change.
Full flag reference: CLI Reference.
See also
- config.json Reference § databases — the config field table
- CLI Reference — every
db-*command in detail - Password Reset & First-Login Forced Change — the
email/must_change_passwordfields covered above, in depth - Anti-Replay Protection — the
anti_replay_secretcolumn covered above, in depth - Group- and Role-Based Access Control — the
groups/user_groupsschema covered above, and howroles(now also usable for access control, not just theX-User-Rolesheader) fits in - Installation & Deployment Guide — filesystem/permissions checklist for
/opt/proxyauth/db - TOTP (2FA) Enrollment & Reset — Admin Guide — how enrollment/reset work for a database-backed account specifically, including the one exception to this cache's read-only design
📖 Wiki Home · 💻 Source Code · 🐛 Report an Issue · 📜 License
Found an error or something outdated on this page? Open an issue or submit a PR against the wiki repository.