mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
@@ -1,7 +1,7 @@
|
||||
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Put } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { Endpoint, HistoryBuilder } from 'src/decorators';
|
||||
import { APIKeyCreateDto, APIKeyCreateResponseDto, APIKeyResponseDto, APIKeyUpdateDto } from 'src/dtos/api-key.dto';
|
||||
import { ApiKeyCreateDto, ApiKeyCreateResponseDto, ApiKeyResponseDto, ApiKeyUpdateDto } from 'src/dtos/api-key.dto';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
import { ApiTag, Permission } from 'src/enum';
|
||||
import { Auth, Authenticated } from 'src/middleware/auth.guard';
|
||||
@@ -20,7 +20,7 @@ export class ApiKeyController {
|
||||
description: 'Creates a new API key. It will be limited to the permissions specified.',
|
||||
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
||||
})
|
||||
createApiKey(@Auth() auth: AuthDto, @Body() dto: APIKeyCreateDto): Promise<APIKeyCreateResponseDto> {
|
||||
createApiKey(@Auth() auth: AuthDto, @Body() dto: ApiKeyCreateDto): Promise<ApiKeyCreateResponseDto> {
|
||||
return this.service.create(auth, dto);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export class ApiKeyController {
|
||||
description: 'Retrieve all API keys of the current user.',
|
||||
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
||||
})
|
||||
getApiKeys(@Auth() auth: AuthDto): Promise<APIKeyResponseDto[]> {
|
||||
getApiKeys(@Auth() auth: AuthDto): Promise<ApiKeyResponseDto[]> {
|
||||
return this.service.getAll(auth);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export class ApiKeyController {
|
||||
description: 'Retrieve the API key that is used to access this endpoint.',
|
||||
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
||||
})
|
||||
async getMyApiKey(@Auth() auth: AuthDto): Promise<APIKeyResponseDto> {
|
||||
async getMyApiKey(@Auth() auth: AuthDto): Promise<ApiKeyResponseDto> {
|
||||
return this.service.getMine(auth);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ export class ApiKeyController {
|
||||
description: 'Retrieve an API key by its ID. The current user must own this API key.',
|
||||
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
||||
})
|
||||
getApiKey(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<APIKeyResponseDto> {
|
||||
getApiKey(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<ApiKeyResponseDto> {
|
||||
return this.service.getById(auth, id);
|
||||
}
|
||||
|
||||
@@ -67,8 +67,8 @@ export class ApiKeyController {
|
||||
updateApiKey(
|
||||
@Auth() auth: AuthDto,
|
||||
@Param() { id }: UUIDParamDto,
|
||||
@Body() dto: APIKeyUpdateDto,
|
||||
): Promise<APIKeyResponseDto> {
|
||||
@Body() dto: ApiKeyUpdateDto,
|
||||
): Promise<ApiKeyResponseDto> {
|
||||
return this.service.update(auth, id, dto);
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ export class UserController {
|
||||
@Authenticated({ permission: Permission.UserUpdate })
|
||||
@Endpoint({
|
||||
summary: 'Update current user',
|
||||
description: 'Update the current user making teh API request.',
|
||||
description: 'Update the current user making the API request.',
|
||||
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
|
||||
})
|
||||
updateMyUser(@Auth() auth: AuthDto, @Body() dto: UserUpdateMeDto): Promise<UserAdminResponseDto> {
|
||||
|
||||
@@ -5,21 +5,21 @@ import z from 'zod';
|
||||
|
||||
const PermissionSchema = z.enum(Permission).describe('List of permissions').meta({ id: 'Permission' });
|
||||
|
||||
const APIKeyCreateSchema = z
|
||||
const ApiKeyCreateSchema = z
|
||||
.object({
|
||||
name: z.string().optional().describe('API key name'),
|
||||
permissions: z.array(PermissionSchema).min(1).describe('List of permissions'),
|
||||
})
|
||||
.meta({ id: 'APIKeyCreateDto' });
|
||||
.meta({ id: 'ApiKeyCreateDto' });
|
||||
|
||||
const APIKeyUpdateSchema = z
|
||||
const ApiKeyUpdateSchema = z
|
||||
.object({
|
||||
name: z.string().optional().describe('API key name'),
|
||||
permissions: z.array(PermissionSchema).min(1).optional().describe('List of permissions'),
|
||||
})
|
||||
.meta({ id: 'APIKeyUpdateDto' });
|
||||
.meta({ id: 'ApiKeyUpdateDto' });
|
||||
|
||||
const APIKeyResponseSchema = z
|
||||
const ApiKeyResponseSchema = z
|
||||
.object({
|
||||
id: z.string().describe('API key ID'),
|
||||
name: z.string().describe('API key name'),
|
||||
@@ -27,16 +27,16 @@ const APIKeyResponseSchema = z
|
||||
updatedAt: isoDatetimeToDate.describe('Last update date'),
|
||||
permissions: z.array(PermissionSchema).describe('List of permissions'),
|
||||
})
|
||||
.meta({ id: 'APIKeyResponseDto' });
|
||||
.meta({ id: 'ApiKeyResponseDto' });
|
||||
|
||||
const APIKeyCreateResponseSchema = z
|
||||
const ApiKeyCreateResponseSchema = z
|
||||
.object({
|
||||
secret: z.string().describe('API key secret (only shown once)'),
|
||||
apiKey: APIKeyResponseSchema,
|
||||
apiKey: ApiKeyResponseSchema,
|
||||
})
|
||||
.meta({ id: 'APIKeyCreateResponseDto' });
|
||||
.meta({ id: 'ApiKeyCreateResponseDto' });
|
||||
|
||||
export class APIKeyCreateDto extends createZodDto(APIKeyCreateSchema) {}
|
||||
export class APIKeyUpdateDto extends createZodDto(APIKeyUpdateSchema) {}
|
||||
export class APIKeyResponseDto extends createZodDto(APIKeyResponseSchema) {}
|
||||
export class APIKeyCreateResponseDto extends createZodDto(APIKeyCreateResponseSchema) {}
|
||||
export class ApiKeyCreateDto extends createZodDto(ApiKeyCreateSchema) {}
|
||||
export class ApiKeyUpdateDto extends createZodDto(ApiKeyUpdateSchema) {}
|
||||
export class ApiKeyResponseDto extends createZodDto(ApiKeyResponseSchema) {}
|
||||
export class ApiKeyCreateResponseDto extends createZodDto(ApiKeyCreateResponseSchema) {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { BadRequestException, ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import { ApiKey } from 'src/database';
|
||||
import { APIKeyCreateDto, APIKeyCreateResponseDto, APIKeyResponseDto, APIKeyUpdateDto } from 'src/dtos/api-key.dto';
|
||||
import { ApiKeyCreateDto, ApiKeyCreateResponseDto, ApiKeyResponseDto, ApiKeyUpdateDto } from 'src/dtos/api-key.dto';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
import { Permission } from 'src/enum';
|
||||
import { BaseService } from 'src/services/base.service';
|
||||
@@ -8,7 +8,7 @@ import { isGranted } from 'src/utils/access';
|
||||
|
||||
@Injectable()
|
||||
export class ApiKeyService extends BaseService {
|
||||
async create(auth: AuthDto, dto: APIKeyCreateDto): Promise<APIKeyCreateResponseDto> {
|
||||
async create(auth: AuthDto, dto: ApiKeyCreateDto): Promise<ApiKeyCreateResponseDto> {
|
||||
const token = this.cryptoRepository.randomBytesAsText(32);
|
||||
const hashed = this.cryptoRepository.hashSha256(token);
|
||||
|
||||
@@ -26,7 +26,7 @@ export class ApiKeyService extends BaseService {
|
||||
return { secret: token, apiKey: this.map(entity) };
|
||||
}
|
||||
|
||||
async update(auth: AuthDto, id: string, dto: APIKeyUpdateDto): Promise<APIKeyResponseDto> {
|
||||
async update(auth: AuthDto, id: string, dto: ApiKeyUpdateDto): Promise<ApiKeyResponseDto> {
|
||||
const exists = await this.apiKeyRepository.getById(auth.user.id, id);
|
||||
if (!exists) {
|
||||
throw new BadRequestException('API Key not found');
|
||||
@@ -54,7 +54,7 @@ export class ApiKeyService extends BaseService {
|
||||
await this.apiKeyRepository.delete(auth.user.id, id);
|
||||
}
|
||||
|
||||
async getMine(auth: AuthDto): Promise<APIKeyResponseDto> {
|
||||
async getMine(auth: AuthDto): Promise<ApiKeyResponseDto> {
|
||||
if (!auth.apiKey) {
|
||||
throw new ForbiddenException('Not authenticated with an API Key');
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export class ApiKeyService extends BaseService {
|
||||
return this.map(key);
|
||||
}
|
||||
|
||||
async getById(auth: AuthDto, id: string): Promise<APIKeyResponseDto> {
|
||||
async getById(auth: AuthDto, id: string): Promise<ApiKeyResponseDto> {
|
||||
const key = await this.apiKeyRepository.getById(auth.user.id, id);
|
||||
if (!key) {
|
||||
throw new BadRequestException('API Key not found');
|
||||
@@ -75,12 +75,12 @@ export class ApiKeyService extends BaseService {
|
||||
return this.map(key);
|
||||
}
|
||||
|
||||
async getAll(auth: AuthDto): Promise<APIKeyResponseDto[]> {
|
||||
async getAll(auth: AuthDto): Promise<ApiKeyResponseDto[]> {
|
||||
const keys = await this.apiKeyRepository.getByUserId(auth.user.id);
|
||||
return keys.map((key) => this.map(key));
|
||||
}
|
||||
|
||||
private map(entity: ApiKey): APIKeyResponseDto {
|
||||
private map(entity: ApiKey): ApiKeyResponseDto {
|
||||
return {
|
||||
id: entity.id,
|
||||
name: entity.name,
|
||||
|
||||
Reference in New Issue
Block a user