feat(api-keys): wire up API key management with real authentication#211
Merged
mijinummi merged 1 commit intoJul 22, 2026
Merged
Conversation
Implements CRUD API key management (create, list, revoke, usage lookup) for the Sentinel API, addressing MD-Creative-Production#109. The api-keys module already existed on disk but was never registered in AppModule, so none of its routes were reachable. It also derived the target userId from an unauthenticated URL path parameter (explicitly flagged in a comment as "for demo purposes"), meaning anyone could create, list, or revoke another user's keys by guessing their id. Changes: - Register ApiKeysModule in AppModule. - Controller now requires JwtAuthGuard and derives the acting user from the verified token via CurrentUser(), matching the pattern already used by the invitations module. userId is no longer client-supplied. - Service now injects the shared PrismaService instead of constructing its own PrismaClient. - revokeApiKey/getApiKeyUsage scope their lookups by (id, userId) together, so a key ID belonging to another user reports as not-found instead of leaking its existence or ownership. - Added CreateApiKeyDto validation (class-validator) and Swagger decorators consistent with other modules. - Added unit tests for the service (key generation/uniqueness, hashing, ownership scoping, revoke idempotency, usage validation) and controller (verifying every route uses the authenticated user's id). Key generation and hashing (sk_-prefixed random key, SHA-256 hash at rest, only the hash ever persisted) were already implemented correctly and are unchanged. Closes MD-Creative-Production#109
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements CRUD API key management (create, list, revoke, usage lookup) for the Sentinel API.
The
api-keysmodule already existed on disk (apps/backend/src/modules/api-keys/) but had two blocking problems:AppModule, so none of its routes were actually reachable.userIdfrom an unauthenticated URL path parameter, explicitly flagged in a comment as "for demo purposes" — meaning any caller could create, list, or revoke another user's keys just by guessing their id.Changes
ApiKeysModuleinAppModule.JwtAuthGuardand derives the acting user from the verified token via@CurrentUser(), matching the pattern already used by theinvitationsmodule.userIdis no longer client-supplied.PrismaServiceinstead of constructing its ownPrismaClient.revokeApiKey/getApiKeyUsagescope their lookups by(id, userId)together, so a key ID belonging to another user reports as not-found instead of leaking its existence/ownership via a different error.CreateApiKeyDtovalidation (class-validator) and Swagger decorators (@ApiTags,@ApiBearerAuth,@ApiOperation) consistent with other modules — required since the app has a globalValidationPipe({ forbidNonWhitelisted: true }).Key generation and hashing (
sk_-prefixed random key, SHA-256 hash at rest, only the hash ever persisted, last-4-chars preview) were already implemented correctly and are unchanged.Acceptance criteria (from #109)
Closes #109
Test plan
npm run build— cleannpx jest --config jest.backend.config.js apps/backend/src/modules/api-keys— 15/15 passingnpx eslinton all changed files — clean (only pre-existing CRLF checkout noise on an untouched file, not part of this diff)