keys.controller.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * API Key 控制器 - CRUD 操作 + 轮换(需 JWT 认证)
  3. */
  4. import {
  5. Controller,
  6. Post,
  7. Get,
  8. Patch,
  9. Delete,
  10. Body,
  11. Param,
  12. UseGuards,
  13. } from '@nestjs/common';
  14. import { KeysService } from './keys.service';
  15. import { CreateKeyDto } from './dto/create-key.dto';
  16. import { UpdateKeyDto } from './dto/update-key.dto';
  17. import { JwtAuthGuard } from '../auth/jwt-auth.guard';
  18. import { CurrentUser } from '../auth/current-user.decorator';
  19. import type { JwtPayload } from '../auth/current-user.decorator';
  20. @Controller('keys')
  21. @UseGuards(JwtAuthGuard)
  22. export class KeysController {
  23. constructor(private readonly keysService: KeysService) {}
  24. @Post()
  25. create(@CurrentUser() user: JwtPayload, @Body() dto: CreateKeyDto) {
  26. return this.keysService.create(user.sub, dto);
  27. }
  28. @Get()
  29. list(@CurrentUser() user: JwtPayload) {
  30. return this.keysService.list(user.sub);
  31. }
  32. @Patch(':id')
  33. update(
  34. @CurrentUser() user: JwtPayload,
  35. @Param('id') id: string,
  36. @Body() dto: UpdateKeyDto,
  37. ) {
  38. return this.keysService.update(user.sub, id, dto);
  39. }
  40. @Delete(':id')
  41. revoke(@CurrentUser() user: JwtPayload, @Param('id') id: string) {
  42. return this.keysService.revoke(user.sub, id);
  43. }
  44. @Post(':id/rotate')
  45. rotate(@CurrentUser() user: JwtPayload, @Param('id') id: string) {
  46. return this.keysService.rotate(user.sub, id);
  47. }
  48. }