Repository navigation
feat(auth): record and throttle key lastUsedAt on successful authentication (#500) - #570
sparkysolutions10-pixel wants to merge 1 commit into
Conversation
f2938e8 to
34d0c4f
Compare
|
Thanks for this — and sorry, it has been overtaken. Issue #500 was assigned to another contributor, and their PR (#587) is merged as Your implementation and theirs landed on the same design independently — in-memory per-key window, configurable throttle, best-effort write that never fails authentication — which is a decent sign it was the right shape. Two things in yours I want to name, because they are worth carrying into your next PR rather than losing: Fire-and-forget was arguably the better call on latency. You did not await the update: const updatePromise = prisma.apiKey.update({ ... })
if (updatePromise && typeof updatePromise.catch === 'function') {
updatePromise.catch(() => {})
}#587 awaits it, so once per throttle window a request pays the write round-trip. At a 60-second window that is negligible, and awaiting is what let me log the failure in one place, so I kept it — but your instinct that an audit timestamp should not sit in the request path is sound. You guarded against a synchronous throw from the Prisma mock. That Three things that would have needed changing before it could merge, in case any of them show up again:
Also a stray whitespace change — There are unclaimed issues open if you want another; comment on one to get it assigned before starting, so you are not racing an assignee again. |
|
@Miracle656 Thank you for the thoughtful, detailed review and constructive architectural feedback! Delighted to see the feature landed on main, and I appreciate the notes on Prisma migration workflows and scope ordering — taking those forward into upcoming PRs. Closing this out now! |
Description
Fixes #500.
Summary of Changes
Operators inspecting API keys previously had no visibility into which keys were still actively in use versus stale and safe to revoke. Updating the database on every authenticated request would turn a high-throughput read-only auth check into a write amplifier.
This PR implements
lastUsedAttracking with robust rate throttling and admin visibility:lastUsedAt DateTime? @map("last_used_at")toApiKeyinprisma/schema.prisma.recordKeyLastUsed) insrc/api/auth.ts.lastUsedAtat most once per throttle window per key.API_KEY_LAST_USED_THROTTLE_SECONDS(default: 60s), documented in.env.example.lastUsedAtinPOST /admin/keysandGET /admin/keysso operators can inspect key activity.src/__tests__/keyLastUsedAt.test.tsverifyinglastUsedAtrecording, throttle enforcement across 10 repeated requests, configurable intervals, and visibility onGET /admin/keys.Testing
npx vitest run src/__tests__/keyLastUsedAt.test.ts(4/4 passed)npx vitest run src/__tests__/auth.test.ts(22/22 passed)npx vitest run tests/openapi.test.ts(8/8 passed)