Commit e289fd1
feat: per-client API keys with per-key rate limiting (sustainability-software-lab#248)
* feat: add per-client API keys with rate limiting
Introduces X-API-Key authentication as a third auth path alongside
Bearer JWT and HTTP-only cookie, so each consumer (frontend, external
researcher, partner) gets an individually revocable key with per-key
sliding-window rate limiting.
Key design choices:
- Store only Argon2 hash + 8-char prefix; raw key returned once at
creation and never persisted
- Prefix-based lookup narrows Argon2 candidates before expensive verify
- Rate limit state (window_start/count) stored on the api_key row with
SELECT FOR UPDATE to handle concurrent requests
- rate_limit_per_minute=0 means unlimited (for trusted internal callers)
- Existing JWT/cookie auth unchanged — no regression for browser clients
New endpoints (admin-only):
POST /v1/auth/api-keys
GET /v1/auth/api-keys
DELETE /v1/auth/api-keys/{id}
PATCH /v1/auth/api-keys/{id}
Includes Alembic migration (down_revision=eacbc6544a10) and 14-test
suite covering create/list/revoke/update/auth/rate-limit scenarios.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Update src/ca_biositing/webservice/ca_biositing/webservice/services/auth_service.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update src/ca_biositing/webservice/ca_biositing/webservice/services/auth_service.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update alembic/versions/b3f2d1c8e9a0_add_api_key_table.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update src/ca_biositing/datamodels/ca_biositing/datamodels/models/auth/api_key.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* fix: address security review findings from PR sustainability-software-lab#248
Resolves all 'before merge' items from @lsetiawan's audit plus two
correctness bugs flagged by Copilot:
FINDING-01 (HIGH): Add constant-time dummy Argon2 verify when no prefix
candidates exist, preventing prefix enumeration via timing side-channel.
Mirrors the existing authenticate_user / DUMMY_HASH pattern.
FINDING-03 (MEDIUM): Admin endpoints now require JWT-only auth via a new
get_jwt_user() dependency. X-API-Key is intentionally excluded from
AdminUserDep so a leaked key can never manage other keys regardless of
the linked account's role. Returns 401 (not 403) on missing JWT.
FINDING-05 (LOW): Rename 'sliding window' to 'fixed window' throughout
— comments, docstrings, and model class docstring. Field names unchanged
to avoid a migration. Added burst-tolerance note.
FINDING-07 (LOW): GET /v1/auth/api-keys now supports optional api_user_id
filter and limit/offset pagination. Covered by a new test.
FINDING-12 (MEDIUM): validate_api_key early-exits for keys longer than
128 chars before touching the DB or running Argon2.
FINDING-02 (MEDIUM): logger.warning on auth failure and rate limit hit;
logger.info on key creation and revocation. Raw key/hash never logged.
Copilot: rollback before return False when rate limit is exceeded, so the
SELECT FOR UPDATE row lock is released immediately rather than held until
session teardown.
Copilot: re-check ApiKey.is_active in the FOR UPDATE query to handle
concurrent revocation between validate_api_key and the counter update.
Copilot: fix timezone mismatch — rate_window_start and last_used_at now
use an explicit sa.DateTime(timezone=True) column to match the migration
and avoid naive/aware subtraction errors on Postgres.
All 168 unit tests pass (3 pre-existing integration failures unchanged).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: rebase alembic migration down_revision to current main head
The api_user migration pointed to an older revision (97a23076c0d9),
creating multiple Alembic heads and failing CI. Updated to point to
60b08397200f (current main head).
* fix: resolve circular dependency in migration chain
eacbc6544a10 was incorrectly rebased onto 60b08397200f, which is a
descendant of eacbc6544a10 itself, creating a cycle that caused
alembic upgrade head to hang indefinitely.
Restore eacbc6544a10 to its original down_revision (97a23076c0d9)
and point the new b3f2d1c8e9a0 (api_key table) at 60b08397200f,
the actual current chain head.
* fix: use timezone-aware DateTime in api_key migration
Align migration column types with the model's sa.DateTime(timezone=True)
for rate_window_start and last_used_at to fix alembic check drift.
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Landung 'Don' Setiawan <landungs@uw.edu>1 parent 9e86f2a commit e289fd1
9 files changed
Lines changed: 928 additions & 30 deletions
File tree
- alembic/versions
- src/ca_biositing
- datamodels/ca_biositing/datamodels/models
- auth
- webservice/ca_biositing/webservice
- services
- v1/auth
- tests/webservice/v1
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
Lines changed: 56 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
Lines changed: 88 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
38 | 42 | | |
39 | 43 | | |
40 | 44 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 45 | + | |
| 46 | + | |
47 | 47 | | |
48 | | - | |
49 | | - | |
| 48 | + | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
58 | 56 | | |
59 | 57 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 58 | + | |
| 59 | + | |
67 | 60 | | |
68 | 61 | | |
69 | | - | |
70 | 62 | | |
71 | 63 | | |
72 | 64 | | |
| |||
78 | 70 | | |
79 | 71 | | |
80 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
81 | 141 | | |
82 | 142 | | |
| 143 | + | |
83 | 144 | | |
84 | 145 | | |
85 | | - | |
86 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
87 | 153 | | |
88 | 154 | | |
89 | 155 | | |
| |||
0 commit comments