2023-07-01 03:49:30 +02:00
|
|
|
import { APIKeyEntity } from '@app/infra/entities';
|
2023-01-28 06:12:11 +01:00
|
|
|
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
|
2023-01-31 19:11:49 +01:00
|
|
|
import { AuthUserDto } from '../auth';
|
|
|
|
import { ICryptoRepository } from '../crypto';
|
2023-07-01 03:49:30 +02:00
|
|
|
import { APIKeyCreateDto, APIKeyCreateResponseDto, APIKeyResponseDto } from './api-key.dto';
|
2023-01-02 21:22:33 +01:00
|
|
|
import { IKeyRepository } from './api-key.repository';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class APIKeyService {
|
2023-01-18 15:40:15 +01:00
|
|
|
constructor(
|
|
|
|
@Inject(ICryptoRepository) private crypto: ICryptoRepository,
|
|
|
|
@Inject(IKeyRepository) private repository: IKeyRepository,
|
|
|
|
) {}
|
2023-01-02 21:22:33 +01:00
|
|
|
|
|
|
|
async create(authUser: AuthUserDto, dto: APIKeyCreateDto): Promise<APIKeyCreateResponseDto> {
|
2023-01-27 21:50:07 +01:00
|
|
|
const secret = this.crypto.randomBytes(32).toString('base64').replace(/\W/g, '');
|
2023-01-02 21:22:33 +01:00
|
|
|
const entity = await this.repository.create({
|
2023-01-27 21:50:07 +01:00
|
|
|
key: this.crypto.hashSha256(secret),
|
2023-01-02 21:22:33 +01:00
|
|
|
name: dto.name || 'API Key',
|
|
|
|
userId: authUser.id,
|
|
|
|
});
|
|
|
|
|
2023-07-01 03:49:30 +02:00
|
|
|
return { secret, apiKey: this.map(entity) };
|
2023-01-02 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-18 21:58:55 +01:00
|
|
|
async update(authUser: AuthUserDto, id: string, dto: APIKeyCreateDto): Promise<APIKeyResponseDto> {
|
2023-01-02 21:22:33 +01:00
|
|
|
const exists = await this.repository.getById(authUser.id, id);
|
|
|
|
if (!exists) {
|
|
|
|
throw new BadRequestException('API Key not found');
|
|
|
|
}
|
|
|
|
|
2023-07-01 03:49:30 +02:00
|
|
|
const key = await this.repository.update(authUser.id, id, { name: dto.name });
|
|
|
|
|
|
|
|
return this.map(key);
|
2023-01-02 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-18 21:58:55 +01:00
|
|
|
async delete(authUser: AuthUserDto, id: string): Promise<void> {
|
2023-01-02 21:22:33 +01:00
|
|
|
const exists = await this.repository.getById(authUser.id, id);
|
|
|
|
if (!exists) {
|
|
|
|
throw new BadRequestException('API Key not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.repository.delete(authUser.id, id);
|
|
|
|
}
|
|
|
|
|
2023-02-18 21:58:55 +01:00
|
|
|
async getById(authUser: AuthUserDto, id: string): Promise<APIKeyResponseDto> {
|
2023-01-02 21:22:33 +01:00
|
|
|
const key = await this.repository.getById(authUser.id, id);
|
|
|
|
if (!key) {
|
|
|
|
throw new BadRequestException('API Key not found');
|
|
|
|
}
|
2023-07-01 03:49:30 +02:00
|
|
|
return this.map(key);
|
2023-01-02 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(authUser: AuthUserDto): Promise<APIKeyResponseDto[]> {
|
|
|
|
const keys = await this.repository.getByUserId(authUser.id);
|
2023-07-01 03:49:30 +02:00
|
|
|
return keys.map((key) => this.map(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
private map(entity: APIKeyEntity): APIKeyResponseDto {
|
|
|
|
return {
|
|
|
|
id: entity.id,
|
|
|
|
name: entity.name,
|
|
|
|
createdAt: entity.createdAt,
|
|
|
|
updatedAt: entity.updatedAt,
|
|
|
|
};
|
2023-01-02 21:22:33 +01:00
|
|
|
}
|
|
|
|
}
|