Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
3.0 KiB
3.0 KiB
WS-D — auth + DB
| Field | Value |
|---|---|
| Status | Backlog |
| Owner | — |
| Phase | 1 (parallel) |
| Depends on | P0 |
| Blocks | WS-E, WS-F (to finish), P3 |
| Owned files | src/db.rs, src/auth/mod.rs, src/auth/tokens.rs, migrations/ |
Goal
Provide token storage in SQLite, the slinuxm token CLI, and one bearer-auth middleware that protects both /api/v1 and /mcp.
Tasks
Tick these in TODO.md.
- D1
db::open:- Create the parent dir with mode
0700if it is missing. In production, systemdStateDirectory=already creates it. - Use
SqliteConnectOptionswithcreate_if_missing,journal_mode(Wal)andbusy_timeout(5s). - Run
sqlx::migrate!()from embeddedmigrations/.
- Create the parent dir with mode
- D2 First migration:
tokens(id INTEGER PK, name TEXT NOT NULL UNIQUE, token_hash TEXT NOT NULL UNIQUE, created_at TEXT NOT NULL, revoked_at TEXT, last_used_at TEXT). - D3
tokens.rswith runtimesqlx::query/query_as(DEC-3):create(name)generates 32 bytes from the OS RNG and formats the token asslinuxm_<base64url>. Storehex(sha256(token))and print the plaintext once with a warning.list()shows name, created, revoked and last-used, never hashes.revoke(name)setsrevoked_at. Revoking twice is a no-op with a message.
- D4 Implement
auth::tokens::run(TokenCmd, &Config)behindslinuxm token create|list|revoke. Themain.rsdispatch already exists from P0, so do not editmain.rs. - D5 Middleware:
- Extract
Authorization: Bearer <t>, hash it, and look upWHERE token_hash = ? AND revoked_at IS NULL. - On success, optionally update
last_used_at. Throttle that update to at most once a minute per token, or skip it. - On failure, return
401withWWW-Authenticate: Bearerand the standard JSON error body. Use the same response for/mcp(DEC-4). - When
auth.enabled = false, the layer is a pass-through.
- Extract
- D6 Expose the middleware so P3 applies it to the protected router only.
/healthzmust stay reachable without a token (review R3). - D7 Tests:
- Use an in-memory or tempdir DB.
- Create a token and confirm it is accepted. Revoke it and confirm it is rejected. Garbage and missing headers are rejected.
- Re-assert the P0 startup refusal for auth-off + non-loopback bind.
Done when
slinuxm token create ciprints a token.slinuxm token listshows it. A request with it passes the middleware.- After
slinuxm token revoke ci, the same request returns 401 without a server restart. - D7 passes.
Notes
- The CLI writes the same DB the server reads, so WAL mode is required. In production, run the CLI as the service user, for example
sudo -u skyg slinuxm token create …. Document this in the README (I4).
Log
- 2026-10-15: Card created.
- 2026-10-15: Implemented on branch
ws-d/auth-db. Token fns take&SqlitePool(create,list,revoke,verify);db::migrateis public for in-memory test pools.last_used_atis throttled to once a minute. Lookup is by sha256 hex hash withslinuxm_prefix pre-check.