ToolJet/server/src/services/credentials.service.ts
2024-06-27 13:25:53 +05:30

43 lines
1.6 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Credential } from '../../src/entities/credential.entity';
import { DataSource, Repository } from 'typeorm';
import { EncryptionService } from './encryption.service';
@Injectable()
export class CredentialsService {
constructor(
private encryptionService: EncryptionService,
@InjectRepository(Credential)
private credentialsRepository: Repository<Credential>,
private readonly _dataSource: DataSource
) {}
async create(value: string, entityManager = this._dataSource.manager): Promise<Credential> {
const credentialRepository = entityManager.getRepository(Credential);
const newCredential = credentialRepository.create({
valueCiphertext: await this.encryptionService.encryptColumnValue('credentials', 'value', value),
createdAt: new Date(),
updatedAt: new Date(),
});
const credential = await credentialRepository.save(newCredential);
return credential;
}
async update(id: string, value: string) {
const valueCiphertext = await this.encryptionService.encryptColumnValue('credentials', 'value', value);
const params = { valueCiphertext, updatedAt: new Date() };
return await this.credentialsRepository.update(id, params);
}
async getValue(credentialId: string): Promise<string> {
const credential = await this.credentialsRepository.findOne({ where: { id: credentialId } });
const decryptedValue = await this.encryptionService.decryptColumnValue(
'credentials',
'value',
credential.valueCiphertext
);
return decryptedValue;
}
}