Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): record a new key's scope on its creation log row
Yudhi Armyndharis3bdc47830efb
diff --git a/src/modules/auth/auth.controller.spec.ts b/src/modules/auth/auth.controller.spec.tsindex a44ad9936..4e2bf41c5 100644--- a/src/modules/auth/auth.controller.spec.ts+++ b/src/modules/auth/auth.controller.spec.ts@@ -68,7 +68,13 @@ describe('AuthController — API-key lifecycle audit logging', () => {| {apiKey?: ApiKey;ipAddress?: string;- metadata?: { targetKeyId?: string; before?: { role?: string }; after?: { role?: string } };+ metadata?: {+ targetKeyId?: string;+ role?: string;+ scope?: unknown;+ before?: { role?: string };+ after?: { role?: string };+ };}| undefined => {const calls = auditService.logInfo.mock.calls as Array<@@ -77,7 +83,13 @@ describe('AuthController — API-key lifecycle audit logging', () => {{apiKey?: ApiKey;ipAddress?: string;- metadata?: { targetKeyId?: string; before?: { role?: string }; after?: { role?: string } };+ metadata?: {+ targetKeyId?: string;+ role?: string;+ scope?: unknown;+ before?: { role?: string };+ after?: { role?: string };+ };},]>;@@ -94,6 +106,32 @@ describe('AuthController — API-key lifecycle audit logging', () => {expect(JSON.stringify(auditService.logInfo.mock.calls)).not.toContain('raw-secret');});+ it("records the created key's full authorization scope on API_KEY_CREATED", async () => {+ const expiresAt = new Date('2027-01-01T00:00:00Z');+ authService.createApiKey.mockResolvedValue({+ apiKey: {+ id: 'k2',+ name: 'scoped',+ role: 'operator',+ allowedIps: ['10.0.0.1'],+ allowedSessions: ['s1'],+ allowedChats: ['[email protected]'],+ expiresAt,+ },+ rawKey: 'raw-secret',+ });+ await controller.create({ name: 'scoped' }, makeReq(), actor);+ const metadata = lastContextFor(AuditAction.API_KEY_CREATED)?.metadata;+ expect(metadata?.role).toBe('operator');+ expect(metadata?.scope).toEqual({+ role: 'operator',+ allowedIps: ['10.0.0.1'],+ allowedSessions: ['s1'],+ allowedChats: ['[email protected]'],+ expiresAt,+ });+ });+it('logs API_KEY_DELETED on delete', async () => {await controller.delete('k1', makeReq(), actor);expect(authService.delete).toHaveBeenCalledWith('k1');diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.tsindex 0f1404d4c..87edaa3c3 100644--- a/src/modules/auth/auth.controller.ts+++ b/src/modules/auth/auth.controller.ts@@ -33,6 +33,18 @@ export class AuthController {};}+ // The key's authorization scope as audited on create and update, so a key's scope can be read back+ // from the audit log whether or not it was ever edited.+ private authzSnapshot(key: ApiKey) {+ return {+ role: key.role,+ allowedIps: key.allowedIps,+ allowedSessions: key.allowedSessions,+ allowedChats: key.allowedChats,+ expiresAt: key.expiresAt,+ };+ }+@Post()@RequireRole(ApiKeyRole.ADMIN)@ApiOperation({ summary: 'Create a new API key (admin only)' })@@ -49,7 +61,12 @@ export class AuthController {const { apiKey, rawKey } = await this.authService.createApiKey(dto);await this.auditService.logInfo(AuditAction.API_KEY_CREATED, {...this.auditContext(req, actor),- metadata: { targetKeyId: apiKey.id, targetKeyName: apiKey.name, role: apiKey.role },+ metadata: {+ targetKeyId: apiKey.id,+ targetKeyName: apiKey.name,+ role: apiKey.role,+ scope: this.authzSnapshot(apiKey),+ },});return {id: apiKey.id,@@ -133,20 +150,13 @@ export class AuthController {): Promise<ApiKeyResponseDto> {const before = await this.authService.findOne(id);const k = await this.authService.update(id, dto);- const authzSnapshot = (key: ApiKey) => ({- role: key.role,- allowedIps: key.allowedIps,- allowedSessions: key.allowedSessions,- allowedChats: key.allowedChats,- expiresAt: key.expiresAt,- });await this.auditService.logInfo(AuditAction.API_KEY_UPDATED, {...this.auditContext(req, actor),metadata: {targetKeyId: k.id,targetKeyName: k.name,- before: authzSnapshot(before),- after: authzSnapshot(k),+ before: this.authzSnapshot(before),+ after: this.authzSnapshot(k),},});return {