Files
skyg-linux-tracker/docs/workstreams/WS-D-auth.md
T
2026-10-19 14:14:29 -04:00

55 lines
3.0 KiB
Markdown

# WS-D — auth + DB
| Field | Value |
|---|---|
| Status | Done |
| 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`](../../TODO.md).
- **D1** `db::open`:
- Create the parent dir with mode `0700` if it is missing. In production, systemd `StateDirectory=` already creates it.
- Use `SqliteConnectOptions` with `create_if_missing`, `journal_mode(Wal)` and `busy_timeout(5s)`.
- Run `sqlx::migrate!()` from embedded `migrations/`.
- **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.rs` with runtime `sqlx::query` / `query_as` (DEC-3):
- `create(name)` generates 32 bytes from the OS RNG and formats the token as `slinuxm_<base64url>`. Store `hex(sha256(token))` and print the plaintext once with a warning.
- `list()` shows name, created, revoked and last-used, never hashes.
- `revoke(name)` sets `revoked_at`. Revoking twice is a no-op with a message.
- **D4** Implement `auth::tokens::run(TokenCmd, &Config)` behind `slinuxm token create|list|revoke`. The `main.rs` dispatch already exists from P0, so do not edit `main.rs`.
- **D5** Middleware:
- Extract `Authorization: Bearer <t>`, hash it, and look up `WHERE 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 `401` with `WWW-Authenticate: Bearer` and the standard JSON error body. Use the same response for `/mcp` (DEC-4).
- When `auth.enabled = false`, the layer is a pass-through.
- **D6** Expose the middleware so P3 applies it to the protected router only. `/healthz` must 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 ci` prints a token. `slinuxm token list` shows 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::migrate` is public for in-memory test pools. `last_used_at` is throttled to once a minute. Lookup is by sha256 hex hash with `slinuxm_` prefix pre-check.