merge dev in staging (#41)

This commit is contained in:
Arnaud D. Natali 2023-05-11 14:30:40 +02:00 committed by GitHub
commit 462de3a08f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1575 additions and 92 deletions

35
package-lock.json generated
View File

@ -17,7 +17,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.44", "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.47",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"next": "^13.1.5", "next": "^13.1.5",
@ -29,7 +29,8 @@
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typedi": "^0.10.0", "typedi": "^0.10.0",
"typescript": "^4.9.4", "typescript": "^4.9.4",
"uuid": "^9.0.0" "uuid": "^9.0.0",
"uuidv4": "^6.2.13"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.13", "@types/cors": "^2.8.13",
@ -2371,9 +2372,9 @@
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.4.389", "version": "1.4.391",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.389.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.391.tgz",
"integrity": "sha512-WDgWUOK8ROR7sDFyYmxCUOoDc50lPgYAHAHwnnD1iN3SKO/mpqftb9iIPiEkMKmqYdkrR0j3N/O+YB/U7lSxwg==", "integrity": "sha512-GqydVV1+kUWY5qlEzaw34/hyWTApuQrHiGrcGA2Kk/56nEK44i+YUW45VH43JuZT0Oo7uY8aVtpPhBBZXEWtSA==",
"dev": true "dev": true
}, },
"node_modules/emittery": { "node_modules/emittery": {
@ -3861,7 +3862,7 @@
} }
}, },
"node_modules/le-coffre-resources": { "node_modules/le-coffre-resources": {
"resolved": "git+ssh://git@github.com/smart-chain-fr/leCoffre-resources.git#30c8ee50b872a8bc6bec0f5d8c4626d8f4490177", "resolved": "git+ssh://git@github.com/smart-chain-fr/leCoffre-resources.git#467b34a484adbd6dfa3fd6082bb7677f6178da51",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
@ -5666,6 +5667,28 @@
"uuid": "dist/bin/uuid" "uuid": "dist/bin/uuid"
} }
}, },
"node_modules/uuidv4": {
"version": "6.2.13",
"resolved": "https://registry.npmjs.org/uuidv4/-/uuidv4-6.2.13.tgz",
"integrity": "sha512-AXyzMjazYB3ovL3q051VLH06Ixj//Knx7QnUSi1T//Ie3io6CpsPu9nVMOx5MoLWh6xV0B9J0hIaxungxXUbPQ==",
"dependencies": {
"@types/uuid": "8.3.4",
"uuid": "8.3.2"
}
},
"node_modules/uuidv4/node_modules/@types/uuid": {
"version": "8.3.4",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz",
"integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw=="
},
"node_modules/uuidv4/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/v8-compile-cache-lib": { "node_modules/v8-compile-cache-lib": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",

View File

@ -48,7 +48,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.44", "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.47",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"next": "^13.1.5", "next": "^13.1.5",
@ -60,7 +60,8 @@
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typedi": "^0.10.0", "typedi": "^0.10.0",
"typescript": "^4.9.4", "typescript": "^4.9.4",
"uuid": "^9.0.0" "uuid": "^9.0.0",
"uuidv4": "^6.2.13"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.13", "@types/cors": "^2.8.13",
@ -80,6 +81,6 @@
}, },
"prisma": { "prisma": {
"schema": "src/common/databases/schema.prisma", "schema": "src/common/databases/schema.prisma",
"seed": "ts-node src/common/databases/seeders/seeder.ts" "seed": "ts-node src/common/databases/seeders/seeder2.ts"
} }
} }

View File

@ -39,6 +39,26 @@ export default class FilesController extends ApiController {
} }
} }
/**
* @description Get a specific File by uid
*/
@Get("/api/v1/super-admin/files/upload/:uid")
protected async getFileData(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
const file = await this.filesService.updload(uid);
this.httpSuccess(response, file);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/** /**
* @description Create a new File * @description Create a new File
* @returns File created * @returns File created
@ -135,7 +155,7 @@ export default class FilesController extends ApiController {
/** /**
* @description Get a specific File by uid * @description Get a specific File by uid
*/ */
@Get("/api/v1/super-admin/Files/:uid") @Get("/api/v1/super-admin/files/:uid")
protected async getOneByUid(req: Request, response: Response) { protected async getOneByUid(req: Request, response: Response) {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];

View File

@ -54,9 +54,6 @@ export class BackendVariables {
@IsNotEmpty() @IsNotEmpty()
public readonly PINATA_GATEWAY!: string; public readonly PINATA_GATEWAY!: string;
@IsNotEmpty()
public readonly KEY_DATA!: string;
public constructor() { public constructor() {
dotenv.config(); dotenv.config();
this.DATABASE_PORT = process.env["DATABASE_PORT"]!; this.DATABASE_PORT = process.env["DATABASE_PORT"]!;
@ -75,7 +72,6 @@ export class BackendVariables {
this.PINATA_API_KEY = process.env["PINATA_API_KEY"]!; this.PINATA_API_KEY = process.env["PINATA_API_KEY"]!;
this.PINATA_API_SECRET = process.env["PINATA_API_SECRET"]!; this.PINATA_API_SECRET = process.env["PINATA_API_SECRET"]!;
this.PINATA_GATEWAY = process.env["PINATA_GATEWAY"]!; this.PINATA_GATEWAY = process.env["PINATA_GATEWAY"]!;
this.KEY_DATA = process.env["KEY_DATA"]!;
} }
public async validate() { public async validate() {
await validateOrReject(this); await validateOrReject(this);

View File

@ -0,0 +1,9 @@
/*
Warnings:
- You are about to drop the column `iv` on the `files` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "files" DROP COLUMN "iv",
ADD COLUMN "key" VARCHAR(255);

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "files" ADD COLUMN "archived_at" TIMESTAMP(3);

View File

@ -39,7 +39,7 @@ model Contacts {
cell_phone_number String @unique @db.VarChar(50) cell_phone_number String @unique @db.VarChar(50)
civility ECivility @default(MALE) civility ECivility @default(MALE)
address Addresses? @relation(fields: [address_uid], references: [uid], onDelete: Cascade) address Addresses? @relation(fields: [address_uid], references: [uid], onDelete: Cascade)
address_uid String? @unique @db.VarChar(255) address_uid String? @unique @db.VarChar(255)
birthdate DateTime? birthdate DateTime?
created_at DateTime? @default(now()) created_at DateTime? @default(now())
updated_at DateTime? @updatedAt updated_at DateTime? @updatedAt
@ -204,7 +204,8 @@ model Files {
document_uid String @db.VarChar(255) document_uid String @db.VarChar(255)
file_path String @unique @db.VarChar(255) file_path String @unique @db.VarChar(255)
file_name String @db.VarChar(255) file_name String @db.VarChar(255)
iv String @db.VarChar(255) archived_at DateTime?
key String? @db.VarChar(255)
created_at DateTime? @default(now()) created_at DateTime? @default(now())
updated_at DateTime? @updatedAt updated_at DateTime? @updatedAt

View File

@ -25,12 +25,6 @@ import {
(async () => { (async () => {
const prisma = new PrismaClient(); const prisma = new PrismaClient();
const existingData = await prisma.contacts.findFirst({ where: { email: "john.doe@example.com" } });
if (existingData) {
console.log("Seed data already exists. Skipping seeding process.");
return;
}
const randomString = () => { const randomString = () => {
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let result = ""; let result = "";
@ -73,9 +67,13 @@ import {
const uidDocument1: string = randomString(); const uidDocument1: string = randomString();
const uidDocument2: string = randomString(); const uidDocument2: string = randomString();
const uidDocument3: string = randomString();
const uidDocument4: string = randomString();
const uidDocument5: string = randomString();
const uidDocumentType1: string = randomString(); const uidDocumentType1: string = randomString();
const uidDocumentType2: string = randomString(); const uidDocumentType2: string = randomString();
const uidDocumentType3: string = randomString();
const uidOfficeFolderHasCustomer1: string = randomString(); const uidOfficeFolderHasCustomer1: string = randomString();
const uidOfficeFolderHasCustomer2: string = randomString(); const uidOfficeFolderHasCustomer2: string = randomString();
@ -92,6 +90,13 @@ import {
const uidDocumentHistory1: string = randomString(); const uidDocumentHistory1: string = randomString();
const uidDocumentHistory2: string = randomString(); const uidDocumentHistory2: string = randomString();
// const existingData = await prisma.contacts.findFirst({ where: { uid: uidContact4 } });
// if (existingData) {
// console.log("Seed data already exists. Skipping seeding process.");
// return;
// }
const customers: Customers[] = [ const customers: Customers[] = [
{ {
uid: uidCustomer1, uid: uidCustomer1,
@ -379,6 +384,36 @@ import {
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
}, },
{
uid: uidDocument3,
blockchain_anchor_uid: null,
depositor_uid: uidCustomer1,
document_status: EDocumentStatus.ASKED,
folder_uid: uidOfficeFolder1,
document_type_uid: uidDocumentType3,
created_at: new Date(),
updated_at: new Date(),
},
{
uid: uidDocument4,
blockchain_anchor_uid: null,
depositor_uid: uidCustomer1,
document_status: EDocumentStatus.ASKED,
folder_uid: uidOfficeFolder1,
document_type_uid: uidDocumentType2,
created_at: new Date(),
updated_at: new Date(),
},
{
uid: uidDocument5,
blockchain_anchor_uid: null,
depositor_uid: uidCustomer1,
document_status: EDocumentStatus.ASKED,
folder_uid: uidOfficeFolder1,
document_type_uid: uidDocumentType1,
created_at: new Date(),
updated_at: new Date(),
},
]; ];
const documentTypes: DocumentTypes[] = [ const documentTypes: DocumentTypes[] = [
@ -396,7 +431,17 @@ import {
uid: uidDocumentType2, uid: uidDocumentType2,
archived_at: null, archived_at: null,
name: "Carte d'identité", name: "Carte d'identité",
office_uid: uidOffice2, office_uid: uidOffice1,
private_description: "Ce document est confidentiel, demander un recto-verso au client",
public_description: "Carte d'identité est un document officiel qui atteste de l'identité d'une personne",
created_at: new Date(),
updated_at: new Date(),
},
{
uid: uidDocumentType3,
archived_at: null,
name: "Autres documents",
office_uid: uidOffice1,
private_description: "Ce document est confidentiel, demander un recto-verso au client", private_description: "Ce document est confidentiel, demander un recto-verso au client",
public_description: "Carte d'identité est un document officiel qui atteste de l'identité d'une personne", public_description: "Carte d'identité est un document officiel qui atteste de l'identité d'une personne",
created_at: new Date(), created_at: new Date(),
@ -427,7 +472,8 @@ import {
document_uid: uidDocument1, document_uid: uidDocument1,
file_name: "fileName1", file_name: "fileName1",
file_path: "https://www.google1.com", file_path: "https://www.google1.com",
iv: "randomIv1", key: '',
archived_at: null,
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
}, },
@ -436,7 +482,8 @@ import {
document_uid: uidDocument1, document_uid: uidDocument1,
file_name: "fileName2", file_name: "fileName2",
file_path: "https://www.google2.com", file_path: "https://www.google2.com",
iv: "randomIv2", key: '',
archived_at: null,
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
}, },

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@ export default class FilesRepository extends BaseRepository {
/** /**
* @description : Create a file linked to a document * @description : Create a file linked to a document
*/ */
public async create(file: File): Promise<Files> { public async create(file: File, key: string): Promise<Files> {
return this.model.create({ return this.model.create({
data: { data: {
document: { document: {
@ -37,7 +37,7 @@ export default class FilesRepository extends BaseRepository {
}, },
file_name: file.file_name, file_name: file.file_name,
file_path: file.file_path, file_path: file.file_path,
iv: file.iv key: key
}, },
include: { document: true } include: { document: true }
}); });
@ -61,10 +61,14 @@ export default class FilesRepository extends BaseRepository {
* @description : Delete a file * @description : Delete a file
*/ */
public async delete(uid: string): Promise<Files> { public async delete(uid: string): Promise<Files> {
return this.model.delete({ return this.model.update({
where: { where: {
uid: uid, uid: uid,
}, },
data: {
key: null,
archived_at: new Date(Date.now())
}
}); });
} }

View File

@ -5,62 +5,44 @@ import crypto from "crypto";
@Service() @Service()
export default class CryptoService extends BaseService { export default class CryptoService extends BaseService {
private jwkKey: JsonWebKey;
private subtle: SubtleCrypto = crypto.webcrypto.subtle private static readonly CRYPTO_ALGORITHM = "aes-256-ctr";
constructor(protected variables: BackendVariables) { constructor(protected variables: BackendVariables) {
super(); super();
this.jwkKey = {
kty: "oct",
k: variables.KEY_DATA,
alg: "A256GCM",
ext: true,
};
} }
private async getKey() { private getKey(key: string) {
return await this.subtle.importKey("jwk", this.jwkKey, {name: "AES-GCM"}, false, ["encrypt", "decrypt"]); return crypto.createHash('sha256').update(String(key)).digest('base64').slice(0, 32);
} }
/** /**
* @description : encrypt data * @description : encrypt data
* @throws {Error} If data cannot be encrypted * @throws {Error} If data cannot be encrypted
*/ */
public async encrypt(data: string) { public async encrypt(buffer: Buffer, key: string): Promise<Buffer> {
const encodedData = Buffer.from(data); // Create an initialization vector
const iv = crypto.webcrypto.getRandomValues(new Uint8Array(16)) const iv = crypto.randomBytes(16);
const key = await this.getKey(); // Create a new cipher using the algorithm, key, and iv
const cipherData = await this.subtle.encrypt( const cipher = crypto.createCipheriv(CryptoService.CRYPTO_ALGORITHM, this.getKey(key), iv);
{ // Create the new (encrypted) buffer
name: "AES-GCM", const result = Buffer.concat([iv, cipher.update(buffer), cipher.final()]);
iv, return result;
},
key,
encodedData,
);
const cipherText = Buffer.from(cipherData).toString('base64');
const ivStringified = Buffer.from(iv).toString('base64');
return { cipherText, ivStringified };
} }
/** /**
* @description : decrypt data with an initialization vector * @description : decrypt data with an initialization vector
* @throws {Error} If data cannot be decrypted * @throws {Error} If data cannot be decrypted
*/ */
public async decrypt(cipherText: string, ivStringified: string): Promise<string> { public async decrypt(encrypted: Buffer, key: string): Promise<Buffer> {
const cipherData = Buffer.from(cipherText, 'base64'); // Get the iv: the first 16 bytes
const iv = Buffer.from(ivStringified, 'base64'); const iv = encrypted.subarray(0, 16);
const key = await this.getKey(); // Get the rest
const decryptedData = await this.subtle.decrypt( encrypted = encrypted.subarray(16);
{ // Create a decipher
name: "AES-GCM", const decipher = crypto.createDecipheriv(CryptoService.CRYPTO_ALGORITHM, this.getKey(key), iv);
iv, // Actually decrypt it
}, const result = Buffer.concat([decipher.update(encrypted), decipher.final()]);
key, return result;
cipherData,
);
return Buffer.from(decryptedData).toString('utf-8');
} }
} }

View File

@ -1,16 +1,21 @@
import FilesRepository from "@Repositories/FilesRepository"; import FilesRepository from "@Repositories/FilesRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Service } from "typedi"; import { Service } from "typedi";
import { File } from "le-coffre-resources/dist/SuperAdmin" import { File } from "le-coffre-resources/dist/SuperAdmin";
import CryptoService from "../CryptoService/CryptoService"; import CryptoService from "../CryptoService/CryptoService";
import IpfsService from "../IpfsService/IpfsService"; import IpfsService from "../IpfsService/IpfsService";
//import fs from "fs";
import { BackendVariables } from "@Common/config/variables/Variables"; import { BackendVariables } from "@Common/config/variables/Variables";
import { Readable } from "stream"; import { Readable } from "stream";
import { v4 } from "uuid";
@Service() @Service()
export default class FilesService extends BaseService { export default class FilesService extends BaseService {
constructor(private filesRepository: FilesRepository, private ipfsService: IpfsService, private variables: BackendVariables, private cryptoService: CryptoService) { constructor(
private filesRepository: FilesRepository,
private ipfsService: IpfsService,
private variables: BackendVariables,
private cryptoService: CryptoService,
) {
super(); super();
} }
@ -22,17 +27,39 @@ export default class FilesService extends BaseService {
return this.filesRepository.findMany(query); return this.filesRepository.findMany(query);
} }
/**
* @description : Get a file by uid
* @throws {Error} If project cannot be created
*/
public async getByUid(uid: string) {
return this.filesRepository.findOneByUid(uid);
}
/**
* @description : view a file
* @throws {Error} If file cannot be deleted
*/
public async updload(uid: string) {
const file = await this.filesRepository.findOneByUid(uid);
if (!file.key) throw new Error("file deleted");
const fileResult = await fetch(file.file_path);
const fileContent = await fileResult.arrayBuffer();
return await this.cryptoService.decrypt(Buffer.from(fileContent), file.key);
}
/** /**
* @description : Create a new file * @description : Create a new file
* @throws {Error} If file cannot be created * @throws {Error} If file cannot be created
*/ */
public async create(file: File, fileData: Express.Multer.File) { public async create(file: File, fileData: Express.Multer.File) {
const upload = await this.ipfsService.pinFile(Readable.from(fileData.buffer), fileData.originalname); const key = v4();
const encryptedPath = await this.cryptoService.encrypt(this.variables.PINATA_GATEWAY.concat(upload.IpfsHash)); const encryptedFile = await this.cryptoService.encrypt(fileData.buffer, key);
file.file_name = fileData.originalname; //const encryptedFileName = await this.cryptoService.encrypt(Buffer.from(fileData.originalname, 'utf-8'), key);
file.file_path = encryptedPath.cipherText; const upload = await this.ipfsService.pinFile(Readable.from(encryptedFile), fileData.originalname);
file.iv = encryptedPath.ivStringified; file.file_name = fileData.originalname; //encryptedFileName.toString('utf-8')
return this.filesRepository.create(file); file.file_path = this.variables.PINATA_GATEWAY.concat(upload.IpfsHash);
return this.filesRepository.create(file, key);
} }
/** /**
@ -50,21 +77,12 @@ export default class FilesService extends BaseService {
public async delete(uid: string) { public async delete(uid: string) {
try { try {
const fileToUnpin = await this.filesRepository.findOneByUid(uid); const fileToUnpin = await this.filesRepository.findOneByUid(uid);
const decryptedFilePath = await this.cryptoService.decrypt(fileToUnpin.file_path, fileToUnpin.iv); const fileHash = fileToUnpin.file_path.substring(this.variables.PINATA_GATEWAY.length);
const fileHash= decryptedFilePath.substring(this.variables.PINATA_GATEWAY.length); await this.ipfsService.unpinFile(fileHash);
await this.ipfsService.unpinFile(fileHash) } catch(error) {
} catch (error) { console.error(error);
console.log(error);
} }
return this.filesRepository.delete(uid); return this.filesRepository.delete(uid);
} }
/**
* @description : Get a file by uid
* @throws {Error} If project cannot be created
*/
public async getByUid(uid: string) {
return this.filesRepository.findOneByUid(uid);
}
} }