mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-22 09:02:42 +00:00
feat(api): add a read-only API key and a /api/stats endpoint (#1419)
This is the read-only API key from #1410, where you said to go ahead. I run Shelfmark behind Homepage and wanted more on the dashboard tile than up or down, without putting an admin API key in the dashboard's config. This adds a second key, `SHELFMARK_API_KEY_READONLY`, that can read one new endpoint. `GET /api/stats` returns counts only: books added over the last 7 and 30 days by format, the queue, requests by outcome, and download failures over the last 7 days. No titles and no user names. The read-only key, the admin key or an admin session can read it. The read-only key gets a 403 on every other path and on any write, including a POST to `/api/stats` itself, and it never gets a session or a cookie. If a request carries both keys, the admin one wins, so a proxy's own `Authorization` header can't downgrade a correct `X-Api-Key`. Unset means off, like the existing key. The endpoint exists either way, but without a key only an admin session can read it. It also adds a `db_path` property on `UserDB`, so the stats module can open the database read-only. Tests cover the key scope, the 403s for the read-only key, both keys at once, who can reach the endpoint, and the counters. The full suite passes, and I broke each rule on purpose to check a test catches it. A version of this has been running on my own install behind Homepage. If you'd rather have the stats in a different shape or under a different path, I'm happy to change it.
This commit is contained in:
1 parent
32abaff21e
commit
755f28a6a4
9 files changed
+496
-3
No files matched your search
@@ -70,3 +70,16 @@ curl -s -H "Authorization: Bearer $SHELFMARK_API_KEY" https://shelfmark.example.
|
||||
- The key is a root-equivalent credential: an admin can configure a custom
|
||||
post-download script that the server executes, so treat it like a root
|
||||
password and send it only over HTTPS.
|
||||
|
||||
## Read-only key and dashboard stats
|
||||
|
||||
Set `SHELFMARK_API_KEY_READONLY` to a second secret for dashboards and monitors. It can read one endpoint,
|
||||
`GET /api/stats`, and nothing else. Writes and every other path return 403, and the key never gets a session.
|
||||
The admin key reaches `/api/stats` too.
|
||||
|
||||
```bash
|
||||
curl -s -H "X-Api-Key: $SHELFMARK_API_KEY_READONLY" https://shelfmark.example.com/api/stats
|
||||
```
|
||||
|
||||
The response has counts only: books added in the last 7 and 30 days by format, the queue, requests by outcome,
|
||||
and download failures in the last 7 days. No titles and no user names.
|
||||
Reference in new issue
Block a user