| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /**
- * API Key 控制器 - CRUD 操作 + 轮换(需 JWT 认证)
- */
- import {
- Controller,
- Post,
- Get,
- Patch,
- Delete,
- Body,
- Param,
- UseGuards,
- } from '@nestjs/common';
- import { KeysService } from './keys.service';
- import { CreateKeyDto } from './dto/create-key.dto';
- import { UpdateKeyDto } from './dto/update-key.dto';
- import { JwtAuthGuard } from '../auth/jwt-auth.guard';
- import { CurrentUser } from '../auth/current-user.decorator';
- import type { JwtPayload } from '../auth/current-user.decorator';
- @Controller('keys')
- @UseGuards(JwtAuthGuard)
- export class KeysController {
- constructor(private readonly keysService: KeysService) {}
- @Post()
- create(@CurrentUser() user: JwtPayload, @Body() dto: CreateKeyDto) {
- return this.keysService.create(user.sub, dto);
- }
- @Get()
- list(@CurrentUser() user: JwtPayload) {
- return this.keysService.list(user.sub);
- }
- @Patch(':id')
- update(
- @CurrentUser() user: JwtPayload,
- @Param('id') id: string,
- @Body() dto: UpdateKeyDto,
- ) {
- return this.keysService.update(user.sub, id, dto);
- }
- @Delete(':id')
- revoke(@CurrentUser() user: JwtPayload, @Param('id') id: string) {
- return this.keysService.revoke(user.sub, id);
- }
- @Post(':id/rotate')
- rotate(@CurrentUser() user: JwtPayload, @Param('id') id: string) {
- return this.keysService.rotate(user.sub, id);
- }
- }
|