|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Post, |
| 4 | + Get, |
| 5 | + Delete, |
| 6 | + Body, |
| 7 | + Param, |
| 8 | + Query, |
| 9 | + HttpCode, |
| 10 | + HttpStatus, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import { ApiKeyService } from '../services/api-key.service'; |
| 13 | +import { |
| 14 | + CreateApiKeyDto, |
| 15 | + ListApiKeysQueryDto, |
| 16 | + RevokeApiKeyDto, |
| 17 | + RotateApiKeyDto, |
| 18 | +} from '../dto/api-key.dto'; |
| 19 | + |
| 20 | +/** |
| 21 | + * API Key Management Controller |
| 22 | + * Handles creation, rotation, and revocation of API keys |
| 23 | + * Note: JWT Auth Guard should be applied at route level or globally |
| 24 | + */ |
| 25 | +@Controller('api-keys') |
| 26 | +export class ApiKeyController { |
| 27 | + constructor(private readonly apiKeyService: ApiKeyService) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * Create a new API key |
| 31 | + * POST /api-keys |
| 32 | + */ |
| 33 | + @Post() |
| 34 | + async createApiKey( |
| 35 | + @Body() createDto: CreateApiKeyDto, |
| 36 | + req: any, |
| 37 | + ) { |
| 38 | + // Get merchantId from authenticated user |
| 39 | + const merchantId = req.user?.merchantId || req.user?.sub; |
| 40 | + |
| 41 | + const result = await this.apiKeyService.createApiKey(merchantId, createDto); |
| 42 | + |
| 43 | + return { |
| 44 | + success: true, |
| 45 | + data: result, |
| 46 | + message: 'API key created successfully. Store the key securely - it will not be shown again.', |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * List all API keys for the authenticated merchant |
| 52 | + * GET /api-keys |
| 53 | + */ |
| 54 | + @Get() |
| 55 | + async listApiKeys( |
| 56 | + @Query() query: ListApiKeysQueryDto, |
| 57 | + req: any, |
| 58 | + ) { |
| 59 | + const merchantId = req.user?.merchantId || req.user?.sub; |
| 60 | + |
| 61 | + const result = await this.apiKeyService.listApiKeys( |
| 62 | + merchantId, |
| 63 | + query.limit, |
| 64 | + query.offset, |
| 65 | + query.status, |
| 66 | + ); |
| 67 | + |
| 68 | + return { |
| 69 | + success: true, |
| 70 | + data: result, |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get API key status |
| 76 | + * GET /api-keys/:id/status |
| 77 | + */ |
| 78 | + @Get(':id/status') |
| 79 | + async getApiKeyStatus( |
| 80 | + @Param('id') keyId: string, |
| 81 | + req: any, |
| 82 | + ) { |
| 83 | + const merchantId = req.user?.merchantId || req.user?.sub; |
| 84 | + |
| 85 | + const result = await this.apiKeyService.getApiKeyStatus(keyId, merchantId); |
| 86 | + |
| 87 | + return { |
| 88 | + success: true, |
| 89 | + data: result, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Rotate an API key |
| 95 | + * POST /api-keys/:id/rotate |
| 96 | + */ |
| 97 | + @Post(':id/rotate') |
| 98 | + async rotateApiKey( |
| 99 | + @Param('id') keyId: string, |
| 100 | + @Body() rotateDto: RotateApiKeyDto, |
| 101 | + req: any, |
| 102 | + ) { |
| 103 | + const merchantId = req.user?.merchantId || req.user?.sub; |
| 104 | + |
| 105 | + const result = await this.apiKeyService.rotateApiKey( |
| 106 | + keyId, |
| 107 | + merchantId, |
| 108 | + rotateDto.reason, |
| 109 | + ); |
| 110 | + |
| 111 | + return { |
| 112 | + success: true, |
| 113 | + data: result, |
| 114 | + message: 'API key rotated successfully. The old key will remain valid for 24 hours.', |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Revoke an API key |
| 120 | + * POST /api-keys/:id/revoke |
| 121 | + */ |
| 122 | + @Post(':id/revoke') |
| 123 | + @HttpCode(HttpStatus.OK) |
| 124 | + async revokeApiKey( |
| 125 | + @Param('id') keyId: string, |
| 126 | + @Body() revokeDto: RevokeApiKeyDto, |
| 127 | + req: any, |
| 128 | + ) { |
| 129 | + const merchantId = req.user?.merchantId || req.user?.sub; |
| 130 | + |
| 131 | + await this.apiKeyService.revokeApiKey(keyId, merchantId, revokeDto.reason); |
| 132 | + |
| 133 | + return { |
| 134 | + success: true, |
| 135 | + message: 'API key revoked successfully.', |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Delete (revoke) an API key |
| 141 | + * DELETE /api-keys/:id |
| 142 | + */ |
| 143 | + @Delete(':id') |
| 144 | + async deleteApiKey( |
| 145 | + @Param('id') keyId: string, |
| 146 | + req: any, |
| 147 | + ) { |
| 148 | + const merchantId = req.user?.merchantId || req.user?.sub; |
| 149 | + |
| 150 | + await this.apiKeyService.revokeApiKey(keyId, merchantId, 'deleted-via-api'); |
| 151 | + |
| 152 | + return { |
| 153 | + success: true, |
| 154 | + message: 'API key deleted successfully.', |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments