Files
immich/server/src/controllers/api-key.controller.ts

87 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-08-15 09:14:23 -04:00
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Put } from '@nestjs/common';
2025-11-13 08:18:43 -05:00
import { ApiTags } from '@nestjs/swagger';
import { Endpoint, HistoryBuilder } from 'src/decorators';
import { APIKeyCreateDto, APIKeyCreateResponseDto, APIKeyResponseDto, APIKeyUpdateDto } from 'src/dtos/api-key.dto';
import { AuthDto } from 'src/dtos/auth.dto';
2025-11-11 17:01:14 -05:00
import { ApiTag, Permission } from 'src/enum';
import { Auth, Authenticated } from 'src/middleware/auth.guard';
import { ApiKeyService } from 'src/services/api-key.service';
import { UUIDParamDto } from 'src/validation';
2025-11-11 17:01:14 -05:00
@ApiTags(ApiTag.ApiKeys)
@Controller('api-keys')
export class ApiKeyController {
constructor(private service: ApiKeyService) {}
@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
})
createApiKey(@Auth() auth: AuthDto, @Body() dto: APIKeyCreateDto): Promise<APIKeyCreateResponseDto> {
return this.service.create(auth, dto);
}
@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'),
})
getApiKeys(@Auth() auth: AuthDto): Promise<APIKeyResponseDto[]> {
return this.service.getAll(auth);
}
@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
})
async getMyApiKey(@Auth() auth: AuthDto): Promise<APIKeyResponseDto> {
return this.service.getMine(auth);
}
@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
})
getApiKey(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<APIKeyResponseDto> {
return this.service.getById(auth, id);
}
@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.',
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
})
updateApiKey(
@Auth() auth: AuthDto,
@Param() { id }: UUIDParamDto,
@Body() dto: APIKeyUpdateDto,
): Promise<APIKeyResponseDto> {
return this.service.update(auth, id, dto);
}
@Delete(':id')
2025-07-15 14:50:13 -04:00
@Authenticated({ permission: Permission.ApiKeyDelete })
@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
})
deleteApiKey(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<void> {
return this.service.delete(auth, id);
}
}