From 6c001625449d2f8a6bc60b19012e78df53cb0c63 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 27 Nov 2023 15:32:52 +0100 Subject: [PATCH] :sparkles: Renaming smsCode into TotpCode --- package.json | 2 +- .../migration.sql | 6 ++--- src/common/databases/schema.prisma | 6 ++--- .../repositories/CustomersRepository.ts | 12 +++------ .../CustomersService/CustomersService.ts | 26 +++++++++---------- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 9cbd22f2..645340ef 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "file-type-checker": "^1.0.8", "fp-ts": "^2.16.1", "jsonwebtoken": "^9.0.0", - "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.97", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.98", "module-alias": "^2.2.2", "monocle-ts": "^2.3.13", "multer": "^1.4.5-lts.1", diff --git a/src/common/databases/migrations/20231123104754_customer_login/migration.sql b/src/common/databases/migrations/20231123104754_customer_login/migration.sql index 0bf90ced..db9e4228 100644 --- a/src/common/databases/migrations/20231123104754_customer_login/migration.sql +++ b/src/common/databases/migrations/20231123104754_customer_login/migration.sql @@ -1,6 +1,4 @@ -- AlterTable ALTER TABLE "customers" ADD COLUMN "password" VARCHAR(255), -ADD COLUMN "passwordCode" VARCHAR(255), -ADD COLUMN "passwordcodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP, -ADD COLUMN "smsCode" VARCHAR(255), -ADD COLUMN "smsCodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP; +ADD COLUMN "totpCode" VARCHAR(255), +ADD COLUMN "totpCodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP; diff --git a/src/common/databases/schema.prisma b/src/common/databases/schema.prisma index 74b5b9c5..1f77bd0d 100644 --- a/src/common/databases/schema.prisma +++ b/src/common/databases/schema.prisma @@ -102,10 +102,8 @@ model Customers { office_folders OfficeFolders[] @relation("OfficeFolderHasCustomers") documents Documents[] password String? @db.VarChar(255) - smsCode String? @db.VarChar(255) - smsCodeExpire DateTime? @default(now()) - passwordCode String? @db.VarChar(255) - passwordcodeExpire DateTime? @default(now()) + totpCode String? @db.VarChar(255) + totpCodeExpire DateTime? @default(now()) @@map("customers") diff --git a/src/common/repositories/CustomersRepository.ts b/src/common/repositories/CustomersRepository.ts index 57aaa67f..6ab46516 100644 --- a/src/common/repositories/CustomersRepository.ts +++ b/src/common/repositories/CustomersRepository.ts @@ -5,10 +5,8 @@ import { Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client"; import { Customer } from "le-coffre-resources/dist/SuperAdmin"; type IExcludedCustomerVars = { - smsCode?: string; - smsCodeExpire?: Date; - passwordCode?: string; - passwordcodeExpire?: Date; + totpCode?: string; + totpCodeExpire?: Date; password?: string; }; @Service() @@ -95,10 +93,8 @@ export default class CustomersRepository extends BaseRepository { address: {}, }, }, - smsCode: excludedVars && excludedVars.smsCode, - smsCodeExpire: excludedVars && excludedVars.smsCodeExpire, - passwordCode: excludedVars && excludedVars.passwordCode, - passwordcodeExpire: excludedVars && excludedVars.passwordcodeExpire, + totpCode: excludedVars && excludedVars.totpCode, + totpCodeExpire: excludedVars && excludedVars.totpCodeExpire, password: excludedVars && excludedVars.password, }, }; diff --git a/src/services/customer/CustomersService/CustomersService.ts b/src/services/customer/CustomersService/CustomersService.ts index f72ca4af..fe52345b 100644 --- a/src/services/customer/CustomersService/CustomersService.ts +++ b/src/services/customer/CustomersService/CustomersService.ts @@ -75,7 +75,7 @@ export default class CustomersService extends BaseService { if (!customer) return null; const now = new Date().getTime(); // Check if the SMS code is still valid - if (customer.smsCodeExpire && now < customer.smsCodeExpire.getTime()) throw new SmsNotExpiredError(); + if (customer.totpCodeExpire && now < customer.totpCodeExpire.getTime()) throw new SmsNotExpiredError(); const totpPin = this.generateTotp(); @@ -96,11 +96,11 @@ export default class CustomersService extends BaseService { * 6: Set the password in database * 7: Returns the customer * @param email - * @param smsCode + * @param totpCode * @param password * @returns */ - public async setFirstPassword(email: string, smsCode: string, password: string): Promise { + public async setFirstPassword(email: string, totpCode: string, password: string): Promise { // 1: Check if the customer exists const customer = await this.getByEmail(email); if (!customer) return null; @@ -109,11 +109,11 @@ export default class CustomersService extends BaseService { if (customer.password) throw new PasswordAlreadySetError(); // 3: Check if the SMS code is existing and is not expired - if (!customer.smsCode || !customer.smsCodeExpire || new Date().getTime() > customer.smsCodeExpire.getTime()) + if (!customer.totpCode || !customer.totpCodeExpire || new Date().getTime() > customer.totpCodeExpire.getTime()) throw new TotpCodeExpiredError(); // 4: Check if the SMS code is valid - if (customer.smsCode !== smsCode) throw new InvalidTotpCodeError(); + if (customer.totpCode !== totpCode) throw new InvalidTotpCodeError(); // 5: Hash the password const hashedPassword = await this.authService.hashPassword(password); @@ -135,21 +135,21 @@ export default class CustomersService extends BaseService { * 5: Check if the password is valid * 6: Return the customer * @param email - * @param smsCode + * @param totpCode * @param password * @returns Customer | null */ - public async login(email: string, smsCode: string, password: string): Promise { + public async login(email: string, totpCode: string, password: string): Promise { // 1: Check if the customer exists const customer = await this.getByEmail(email); if (!customer) return null; // 2: Check if the SMS code is existing and is not expired - if (!customer.smsCode || !customer.smsCodeExpire || new Date().getTime() > customer.smsCodeExpire.getTime()) + if (!customer.totpCode || !customer.totpCodeExpire || new Date().getTime() > customer.totpCodeExpire.getTime()) throw new TotpCodeExpiredError(); // 3: Check if the SMS code is valid - if (customer.smsCode !== smsCode) throw new InvalidTotpCodeError(); + if (customer.totpCode !== totpCode) throw new InvalidTotpCodeError(); // 4: Check if the user has a password or it's their first login if (!customer.password) throw new NotRegisteredCustomerError(); @@ -201,8 +201,8 @@ export default class CustomersService extends BaseService { ...customer, }), { - smsCode: totpPin.toString(), - smsCodeExpire: expireAt, + totpCode: totpPin.toString(), + totpCodeExpire: expireAt, }, ); } @@ -226,11 +226,11 @@ export default class CustomersService extends BaseService { if (!customer) return null; // 2: Check if the SMS code is existing and is not expired - if (!customer.smsCode || !customer.smsCodeExpire || new Date().getTime() > customer.smsCodeExpire.getTime()) + if (!customer.totpCode || !customer.totpCodeExpire || new Date().getTime() > customer.totpCodeExpire.getTime()) throw new TotpCodeExpiredError(); // 3: Check if the SMS code is valid - if (customer.smsCode !== totpCode) throw new InvalidTotpCodeError(); + if (customer.totpCode !== totpCode) throw new InvalidTotpCodeError(); // 4: Return the customer return customer;