Spaces:
Sleeping
Sleeping
Download src/modules/auth/auth.controller.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 4.47 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/modules/auth/auth.controller.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/src/modules/auth/auth.controller.ts
-
curl -L -o auth.controller.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/modules/auth/auth.controller.ts
4.47 kB
| import { Controller, Get, Post, Put, Delete, Body, Param, HttpCode, HttpStatus } from '@nestjs/common'; | |
| import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; | |
| import { AuthService } from './auth.service'; | |
| import { CreateApiKeyDto, UpdateApiKeyDto, ApiKeyResponseDto, ApiKeyCreatedResponseDto } from './dto'; | |
| import { RequireRole } from './decorators/auth.decorators'; | |
| import { ApiKeyRole } from './entities/api-key.entity'; | |
| ('auth') | |
| ('auth/api-keys') | |
| export class AuthController { | |
| constructor(private readonly authService: AuthService) {} | |
| () | |
| (ApiKeyRole.ADMIN) | |
| ({ summary: 'Create a new API key (admin only)' }) | |
| ({ | |
| status: 201, | |
| description: 'API key created', | |
| type: ApiKeyCreatedResponseDto, | |
| }) | |
| async create(() dto: CreateApiKeyDto): Promise<ApiKeyCreatedResponseDto> { | |
| const { apiKey, rawKey } = await this.authService.createApiKey(dto); | |
| return { | |
| id: apiKey.id, | |
| name: apiKey.name, | |
| keyPrefix: apiKey.keyPrefix, | |
| role: apiKey.role, | |
| allowedIps: apiKey.allowedIps || undefined, | |
| allowedSessions: apiKey.allowedSessions || undefined, | |
| isActive: apiKey.isActive, | |
| expiresAt: apiKey.expiresAt || undefined, | |
| lastUsedAt: apiKey.lastUsedAt || undefined, | |
| usageCount: apiKey.usageCount, | |
| createdAt: apiKey.createdAt, | |
| apiKey: rawKey, | |
| }; | |
| } | |
| () | |
| (ApiKeyRole.ADMIN) | |
| ({ summary: 'List all API keys (admin only)' }) | |
| ({ status: 200, type: [ApiKeyResponseDto] }) | |
| async findAll(): Promise<ApiKeyResponseDto[]> { | |
| const keys = await this.authService.findAll(); | |
| return keys.map(k => ({ | |
| id: k.id, | |
| name: k.name, | |
| keyPrefix: k.keyPrefix, | |
| role: k.role, | |
| allowedIps: k.allowedIps || undefined, | |
| allowedSessions: k.allowedSessions || undefined, | |
| isActive: k.isActive, | |
| expiresAt: k.expiresAt || undefined, | |
| lastUsedAt: k.lastUsedAt || undefined, | |
| usageCount: k.usageCount, | |
| createdAt: k.createdAt, | |
| })); | |
| } | |
| (':id') | |
| (ApiKeyRole.ADMIN) | |
| ({ summary: 'Get API key details (admin only)' }) | |
| ({ status: 200, type: ApiKeyResponseDto }) | |
| async findOne(('id') id: string): Promise<ApiKeyResponseDto> { | |
| const k = await this.authService.findOne(id); | |
| return { | |
| id: k.id, | |
| name: k.name, | |
| keyPrefix: k.keyPrefix, | |
| role: k.role, | |
| allowedIps: k.allowedIps || undefined, | |
| allowedSessions: k.allowedSessions || undefined, | |
| isActive: k.isActive, | |
| expiresAt: k.expiresAt || undefined, | |
| lastUsedAt: k.lastUsedAt || undefined, | |
| usageCount: k.usageCount, | |
| createdAt: k.createdAt, | |
| }; | |
| } | |
| (':id') | |
| (ApiKeyRole.ADMIN) | |
| ({ summary: 'Update API key (admin only)' }) | |
| ({ status: 200, type: ApiKeyResponseDto }) | |
| async update(('id') id: string, () dto: UpdateApiKeyDto): Promise<ApiKeyResponseDto> { | |
| const k = await this.authService.update(id, dto); | |
| return { | |
| id: k.id, | |
| name: k.name, | |
| keyPrefix: k.keyPrefix, | |
| role: k.role, | |
| allowedIps: k.allowedIps || undefined, | |
| allowedSessions: k.allowedSessions || undefined, | |
| isActive: k.isActive, | |
| expiresAt: k.expiresAt || undefined, | |
| lastUsedAt: k.lastUsedAt || undefined, | |
| usageCount: k.usageCount, | |
| createdAt: k.createdAt, | |
| }; | |
| } | |
| (':id') | |
| (ApiKeyRole.ADMIN) | |
| (HttpStatus.NO_CONTENT) | |
| ({ summary: 'Delete API key (admin only)' }) | |
| ({ status: 204, description: 'API key deleted' }) | |
| async delete(('id') id: string): Promise<void> { | |
| await this.authService.delete(id); | |
| } | |
| (':id/revoke') | |
| (ApiKeyRole.ADMIN) | |
| ({ summary: 'Revoke API key (admin only)' }) | |
| ({ status: 200, type: ApiKeyResponseDto }) | |
| async revoke(('id') id: string): Promise<ApiKeyResponseDto> { | |
| const k = await this.authService.revoke(id); | |
| return { | |
| id: k.id, | |
| name: k.name, | |
| keyPrefix: k.keyPrefix, | |
| role: k.role, | |
| allowedIps: k.allowedIps || undefined, | |
| allowedSessions: k.allowedSessions || undefined, | |
| isActive: k.isActive, | |
| expiresAt: k.expiresAt || undefined, | |
| lastUsedAt: k.lastUsedAt || undefined, | |
| usageCount: k.usageCount, | |
| createdAt: k.createdAt, | |
| }; | |
| } | |
| } | |