2026-06-07 15:40:01 +02:00
|
|
|
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Patch, Post, Put } from '@nestjs/common';
|
|
|
|
|
import { ApiExcludeEndpoint, ApiTags } from '@nestjs/swagger';
|
2025-11-13 08:18:43 -05:00
|
|
|
import { Endpoint, HistoryBuilder } from 'src/decorators';
|
2026-04-15 15:58:26 -04:00
|
|
|
import { ApiKeyCreateDto, ApiKeyCreateResponseDto, ApiKeyResponseDto, ApiKeyUpdateDto } from 'src/dtos/api-key.dto';
|
2024-03-20 23:53:07 +01:00
|
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
2025-11-11 17:01:14 -05:00
|
|
|
import { ApiTag, Permission } from 'src/enum';
|
2024-03-20 15:15:01 -05:00
|
|
|
import { Auth, Authenticated } from 'src/middleware/auth.guard';
|
2025-03-10 12:04:35 -04:00
|
|
|
import { ApiKeyService } from 'src/services/api-key.service';
|
2024-03-20 15:04:03 -05:00
|
|
|
import { UUIDParamDto } from 'src/validation';
|
2023-01-02 15:22:33 -05:00
|
|
|
|
2025-11-11 17:01:14 -05:00
|
|
|
@ApiTags(ApiTag.ApiKeys)
|
2024-05-22 13:24:57 -04:00
|
|
|
@Controller('api-keys')
|
2025-08-18 19:15:03 -04:00
|
|
|
export class ApiKeyController {
|
2025-03-10 12:04:35 -04:00
|
|
|
constructor(private service: ApiKeyService) {}
|
2023-01-02 15:22:33 -05:00
|
|
|
|
|
|
|
|
@Post()
|
2025-07-15 14:50:13 -04:00
|
|
|
@Authenticated({ permission: Permission.ApiKeyCreate })
|
2025-11-13 08:18:43 -05:00
|
|
|
@Endpoint({
|
2025-11-11 17:01:14 -05:00
|
|
|
summary: 'Create an API key',
|
|
|
|
|
description: 'Creates a new API key. It will be limited to the permissions specified.',
|
2025-11-13 08:18:43 -05:00
|
|
|
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
2025-11-11 17:01:14 -05:00
|
|
|
})
|
2026-04-15 15:58:26 -04:00
|
|
|
createApiKey(@Auth() auth: AuthDto, @Body() dto: ApiKeyCreateDto): Promise<ApiKeyCreateResponseDto> {
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.create(auth, dto);
|
2023-01-02 15:22:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get()
|
2025-07-15 14:50:13 -04:00
|
|
|
@Authenticated({ permission: Permission.ApiKeyRead })
|
2025-11-13 08:18:43 -05:00
|
|
|
@Endpoint({
|
|
|
|
|
summary: 'List all API keys',
|
|
|
|
|
description: 'Retrieve all API keys of the current user.',
|
|
|
|
|
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
|
|
|
|
})
|
2026-04-15 15:58:26 -04:00
|
|
|
getApiKeys(@Auth() auth: AuthDto): Promise<ApiKeyResponseDto[]> {
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.getAll(auth);
|
2023-01-02 15:22:33 -05:00
|
|
|
}
|
|
|
|
|
|
2025-08-18 19:15:03 -04:00
|
|
|
@Get('me')
|
|
|
|
|
@Authenticated({ permission: false })
|
2025-11-13 08:18:43 -05:00
|
|
|
@Endpoint({
|
2025-11-11 17:01:14 -05:00
|
|
|
summary: 'Retrieve the current API key',
|
|
|
|
|
description: 'Retrieve the API key that is used to access this endpoint.',
|
2025-11-13 08:18:43 -05:00
|
|
|
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
2025-11-11 17:01:14 -05:00
|
|
|
})
|
2026-04-15 15:58:26 -04:00
|
|
|
async getMyApiKey(@Auth() auth: AuthDto): Promise<ApiKeyResponseDto> {
|
2025-08-18 19:15:03 -04:00
|
|
|
return this.service.getMine(auth);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 15:22:33 -05:00
|
|
|
@Get(':id')
|
2025-07-15 14:50:13 -04:00
|
|
|
@Authenticated({ permission: Permission.ApiKeyRead })
|
2025-11-13 08:18:43 -05:00
|
|
|
@Endpoint({
|
2025-11-11 17:01:14 -05:00
|
|
|
summary: 'Retrieve an API key',
|
|
|
|
|
description: 'Retrieve an API key by its ID. The current user must own this API key.',
|
2025-11-13 08:18:43 -05:00
|
|
|
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
2025-11-11 17:01:14 -05:00
|
|
|
})
|
2026-04-15 15:58:26 -04:00
|
|
|
getApiKey(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<ApiKeyResponseDto> {
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.getById(auth, id);
|
2023-01-02 15:22:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put(':id')
|
2025-07-15 14:50:13 -04:00
|
|
|
@Authenticated({ permission: Permission.ApiKeyUpdate })
|
2025-11-13 08:18:43 -05:00
|
|
|
@Endpoint({
|
2025-11-11 17:01:14 -05:00
|
|
|
summary: 'Update an API key',
|
|
|
|
|
description: 'Updates the name and permissions of an API key by its ID. The current user must own this API key.',
|
2026-06-07 15:40:01 +02:00
|
|
|
history: new HistoryBuilder()
|
|
|
|
|
.added('v1')
|
|
|
|
|
.beta('v1')
|
|
|
|
|
.stable('v2')
|
|
|
|
|
.deprecated('v3', { replacementId: 'updateApiKey' }),
|
2025-11-11 17:01:14 -05:00
|
|
|
})
|
2023-11-03 21:33:15 -04:00
|
|
|
updateApiKey(
|
2023-12-09 23:34:12 -05:00
|
|
|
@Auth() auth: AuthDto,
|
2023-04-05 00:24:08 +02:00
|
|
|
@Param() { id }: UUIDParamDto,
|
2026-04-15 15:58:26 -04:00
|
|
|
@Body() dto: ApiKeyUpdateDto,
|
|
|
|
|
): Promise<ApiKeyResponseDto> {
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.update(auth, id, dto);
|
2023-01-02 15:22:33 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-07 15:40:01 +02:00
|
|
|
@Patch(':id')
|
|
|
|
|
@ApiExcludeEndpoint()
|
|
|
|
|
@Authenticated({ permission: Permission.ApiKeyUpdate })
|
|
|
|
|
updateApiKeyV3(
|
|
|
|
|
@Auth() auth: AuthDto,
|
|
|
|
|
@Param() { id }: UUIDParamDto,
|
|
|
|
|
@Body() dto: ApiKeyUpdateDto,
|
|
|
|
|
): Promise<ApiKeyResponseDto> {
|
|
|
|
|
return this.service.update(auth, id, dto);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 15:22:33 -05:00
|
|
|
@Delete(':id')
|
2025-07-15 14:50:13 -04:00
|
|
|
@Authenticated({ permission: Permission.ApiKeyDelete })
|
2025-08-08 15:56:37 -04:00
|
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
2025-11-13 08:18:43 -05:00
|
|
|
@Endpoint({
|
2025-11-11 17:01:14 -05:00
|
|
|
summary: 'Delete an API key',
|
|
|
|
|
description: 'Deletes an API key identified by its ID. The current user must own this API key.',
|
2025-11-13 08:18:43 -05:00
|
|
|
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
2025-11-11 17:01:14 -05:00
|
|
|
})
|
2023-12-09 23:34:12 -05:00
|
|
|
deleteApiKey(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<void> {
|
|
|
|
|
return this.service.delete(auth, id);
|
2023-01-02 15:22:33 -05:00
|
|
|
}
|
|
|
|
|
}
|