Renaming smsCode into TotpCode

This commit is contained in:
Maxime Lalo 2023-11-27 15:32:52 +01:00
parent 3835127d63
commit 6c00162544
5 changed files with 22 additions and 30 deletions

View File

@ -56,7 +56,7 @@
"file-type-checker": "^1.0.8", "file-type-checker": "^1.0.8",
"fp-ts": "^2.16.1", "fp-ts": "^2.16.1",
"jsonwebtoken": "^9.0.0", "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", "module-alias": "^2.2.2",
"monocle-ts": "^2.3.13", "monocle-ts": "^2.3.13",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",

View File

@ -1,6 +1,4 @@
-- AlterTable -- AlterTable
ALTER TABLE "customers" ADD COLUMN "password" VARCHAR(255), ALTER TABLE "customers" ADD COLUMN "password" VARCHAR(255),
ADD COLUMN "passwordCode" VARCHAR(255), ADD COLUMN "totpCode" VARCHAR(255),
ADD COLUMN "passwordcodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP, ADD COLUMN "totpCodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
ADD COLUMN "smsCode" VARCHAR(255),
ADD COLUMN "smsCodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;

View File

@ -102,10 +102,8 @@ model Customers {
office_folders OfficeFolders[] @relation("OfficeFolderHasCustomers") office_folders OfficeFolders[] @relation("OfficeFolderHasCustomers")
documents Documents[] documents Documents[]
password String? @db.VarChar(255) password String? @db.VarChar(255)
smsCode String? @db.VarChar(255) totpCode String? @db.VarChar(255)
smsCodeExpire DateTime? @default(now()) totpCodeExpire DateTime? @default(now())
passwordCode String? @db.VarChar(255)
passwordcodeExpire DateTime? @default(now())
@@map("customers") @@map("customers")

View File

@ -5,10 +5,8 @@ import { Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client";
import { Customer } from "le-coffre-resources/dist/SuperAdmin"; import { Customer } from "le-coffre-resources/dist/SuperAdmin";
type IExcludedCustomerVars = { type IExcludedCustomerVars = {
smsCode?: string; totpCode?: string;
smsCodeExpire?: Date; totpCodeExpire?: Date;
passwordCode?: string;
passwordcodeExpire?: Date;
password?: string; password?: string;
}; };
@Service() @Service()
@ -95,10 +93,8 @@ export default class CustomersRepository extends BaseRepository {
address: {}, address: {},
}, },
}, },
smsCode: excludedVars && excludedVars.smsCode, totpCode: excludedVars && excludedVars.totpCode,
smsCodeExpire: excludedVars && excludedVars.smsCodeExpire, totpCodeExpire: excludedVars && excludedVars.totpCodeExpire,
passwordCode: excludedVars && excludedVars.passwordCode,
passwordcodeExpire: excludedVars && excludedVars.passwordcodeExpire,
password: excludedVars && excludedVars.password, password: excludedVars && excludedVars.password,
}, },
}; };

View File

@ -75,7 +75,7 @@ export default class CustomersService extends BaseService {
if (!customer) return null; if (!customer) return null;
const now = new Date().getTime(); const now = new Date().getTime();
// Check if the SMS code is still valid // 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(); const totpPin = this.generateTotp();
@ -96,11 +96,11 @@ export default class CustomersService extends BaseService {
* 6: Set the password in database * 6: Set the password in database
* 7: Returns the customer * 7: Returns the customer
* @param email * @param email
* @param smsCode * @param totpCode
* @param password * @param password
* @returns * @returns
*/ */
public async setFirstPassword(email: string, smsCode: string, password: string): Promise<Customer | null> { public async setFirstPassword(email: string, totpCode: string, password: string): Promise<Customer | null> {
// 1: Check if the customer exists // 1: Check if the customer exists
const customer = await this.getByEmail(email); const customer = await this.getByEmail(email);
if (!customer) return null; if (!customer) return null;
@ -109,11 +109,11 @@ export default class CustomersService extends BaseService {
if (customer.password) throw new PasswordAlreadySetError(); if (customer.password) throw new PasswordAlreadySetError();
// 3: Check if the SMS code is existing and is not expired // 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(); throw new TotpCodeExpiredError();
// 4: Check if the SMS code is valid // 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 // 5: Hash the password
const hashedPassword = await this.authService.hashPassword(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 * 5: Check if the password is valid
* 6: Return the customer * 6: Return the customer
* @param email * @param email
* @param smsCode * @param totpCode
* @param password * @param password
* @returns Customer | null * @returns Customer | null
*/ */
public async login(email: string, smsCode: string, password: string): Promise<Customer | null> { public async login(email: string, totpCode: string, password: string): Promise<Customer | null> {
// 1: Check if the customer exists // 1: Check if the customer exists
const customer = await this.getByEmail(email); const customer = await this.getByEmail(email);
if (!customer) return null; if (!customer) return null;
// 2: Check if the SMS code is existing and is not expired // 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(); throw new TotpCodeExpiredError();
// 3: Check if the SMS code is valid // 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 // 4: Check if the user has a password or it's their first login
if (!customer.password) throw new NotRegisteredCustomerError(); if (!customer.password) throw new NotRegisteredCustomerError();
@ -201,8 +201,8 @@ export default class CustomersService extends BaseService {
...customer, ...customer,
}), }),
{ {
smsCode: totpPin.toString(), totpCode: totpPin.toString(),
smsCodeExpire: expireAt, totpCodeExpire: expireAt,
}, },
); );
} }
@ -226,11 +226,11 @@ export default class CustomersService extends BaseService {
if (!customer) return null; if (!customer) return null;
// 2: Check if the SMS code is existing and is not expired // 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(); throw new TotpCodeExpiredError();
// 3: Check if the SMS code is valid // 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 // 4: Return the customer
return customer; return customer;