Skip to content

feat(api-keys): wire up API key management with real authentication#211

Merged
mijinummi merged 1 commit into
MD-Creative-Production:mainfrom
devwums:feat/issue-109-api-key-management
Jul 22, 2026
Merged

feat(api-keys): wire up API key management with real authentication#211
mijinummi merged 1 commit into
MD-Creative-Production:mainfrom
devwums:feat/issue-109-api-key-management

Conversation

@devwums

@devwums devwums commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements CRUD API key management (create, list, revoke, usage lookup) for the Sentinel API.

The api-keys module already existed on disk (apps/backend/src/modules/api-keys/) but had two blocking problems:

  1. It was never registered in AppModule, so none of its routes were actually reachable.
  2. It derived the target userId from 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

  • 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/ownership via a different error.
  • Added CreateApiKeyDto validation (class-validator) and Swagger decorators (@ApiTags, @ApiBearerAuth, @ApiOperation) consistent with other modules — required since the app has a global ValidationPipe({ forbidNonWhitelisted: true }).
  • 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, never a client-supplied one).

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)

  • CRUD endpoints implemented (create, list, revoke, usage)
  • Secure key generation
  • Revocation supported

Closes #109

Test plan

  • npm run build — clean
  • npx jest --config jest.backend.config.js apps/backend/src/modules/api-keys — 15/15 passing
  • npx eslint on all changed files — clean (only pre-existing CRLF checkout noise on an untouched file, not part of this diff)

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
@mijinummi
mijinummi merged commit ee1fd7c into MD-Creative-Production:main Jul 22, 2026
12 checks passed
@grantfox-oss grantfox-oss Bot mentioned this pull request Jul 22, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create API Key Management API

2 participants