This commit is contained in:
David Arranz 2024-05-20 09:47:44 +02:00
parent 9d96c76fa4
commit e350e65f37
9 changed files with 28 additions and 42 deletions

View File

@ -16,7 +16,7 @@
"author": "Rodax Software <dev@rodax-software.com>", "author": "Rodax Software <dev@rodax-software.com>",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/bcryptjs": "^2.4.6", "@types/bcrypt": "^5.0.2",
"@types/cors": "^2.8.13", "@types/cors": "^2.8.13",
"@types/dinero.js": "^1.9.1", "@types/dinero.js": "^1.9.1",
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
@ -57,7 +57,7 @@
"dependencies": { "dependencies": {
"@joi/date": "^2.1.0", "@joi/date": "^2.1.0",
"@reis/joi-luxon": "^3.0.0", "@reis/joi-luxon": "^3.0.0",
"bcryptjs": "^2.4.3", "bcrypt": "^5.1.1",
"cls-rtracer": "^2.6.3", "cls-rtracer": "^2.6.3",
"cors": "^2.8.5", "cors": "^2.8.5",
"cross-env": "5.0.5", "cross-env": "5.0.5",

View File

@ -1,9 +1,9 @@
import { Password } from "@/contexts/common/domain";
import { import {
AggregateRoot, AggregateRoot,
Email, Email,
IDomainError, IDomainError,
Name, Name,
Password,
Result, Result,
UniqueID, UniqueID,
} from "@shared/contexts"; } from "@shared/contexts";

View File

@ -1,8 +1,9 @@
import { Password } from "@/contexts/common/domain";
import { import {
ISequelizeMapper, ISequelizeMapper,
SequelizeMapper, SequelizeMapper,
} from "@/contexts/common/infrastructure"; } from "@/contexts/common/infrastructure";
import { Email, Name, Password, UniqueID } from "@shared/contexts"; import { Email, Name, UniqueID } from "@shared/contexts";
import { AuthUser, IAuthUserProps } from "../../domain/entities"; import { AuthUser, IAuthUserProps } from "../../domain/entities";
import { IAuthContext } from "../Auth.context"; import { IAuthContext } from "../Auth.context";
import { import {
@ -32,7 +33,7 @@ class AuthUserMapper
password: this.mapsValue( password: this.mapsValue(
source, source,
"password", "password",
Password.createFromHashedText, Password.createFromHashedTextPassword,
), ),
}; };

View File

@ -1,14 +1,15 @@
import bCrypt from "bcryptjs"; import {
DomainError,
IStringValueObjectOptions,
Result,
RuleValidator,
StringValueObject,
handleDomainError,
} from "@shared/contexts";
import { UndefinedOr } from "@shared/utilities";
import bCrypt from "bcrypt";
import Joi from "joi"; import Joi from "joi";
import { UndefinedOr } from "../../../../utilities";
import { RuleValidator } from "../RuleValidator";
import { DomainError, handleDomainError } from "../errors";
import { Result } from "./Result";
import {
IStringValueObjectOptions,
StringValueObject,
} from "./StringValueObject";
export interface IPasswordOptions extends IStringValueObjectOptions {} export interface IPasswordOptions extends IStringValueObjectOptions {}
@ -32,7 +33,7 @@ export class Password extends StringValueObject {
return RuleValidator.validate<string>(rule, value); return RuleValidator.validate<string>(rule, value);
} }
public static createFromHashedText( public static createFromHashedTextPassword(
value: UndefinedOr<string>, value: UndefinedOr<string>,
options: IPasswordOptions = {}, options: IPasswordOptions = {},
) { ) {
@ -56,7 +57,7 @@ export class Password extends StringValueObject {
return Result.ok(new Password(validationResult.object)); return Result.ok(new Password(validationResult.object));
} }
public static async createFromPlainText( public static async createFromPlainTextPassword(
value: UndefinedOr<string>, value: UndefinedOr<string>,
options: IPasswordOptions = {}, options: IPasswordOptions = {},
) { ) {
@ -82,29 +83,11 @@ export class Password extends StringValueObject {
); );
} }
public static async hashPassword(plainText: string): Promise<string> { public static hashPassword(plainTextPassword: string): string {
return hashPassword(plainText, await genSalt()); return bCrypt.hashSync(plainTextPassword, 10);
} }
public verifyPassword(candidatePassword: string): boolean { public verifyPassword(plainTextPassword: string): boolean {
return bCrypt.compareSync(candidatePassword, this.value!); return bCrypt.compareSync(plainTextPassword, this.value!);
} }
} }
async function genSalt(rounds = 10): Promise<string> {
return new Promise((resolve, reject) => {
bCrypt.genSalt(rounds, function (err, salt) {
if (err) return reject(err);
return resolve(salt);
});
});
}
async function hashPassword(password: string, salt: string): Promise<string> {
return new Promise((resolve, reject) => {
bCrypt.hash(password, salt, function (err, hash) {
if (err) return reject(err);
return resolve(hash);
});
});
}

View File

@ -0,0 +1 @@
export * from "./Password";

View File

@ -1,3 +1,4 @@
export * from "./Mapper.interface"; export * from "./Mapper.interface";
export * from "./Specification"; export * from "./Specification";
export * from "./entities";
export * from "./repositories"; export * from "./repositories";

View File

@ -1,9 +1,9 @@
import { Password } from "@/contexts/common/domain";
import { import {
AggregateRoot, AggregateRoot,
Email, Email,
IDomainError, IDomainError,
Name, Name,
Password,
Result, Result,
UniqueID, UniqueID,
handleDomainError, handleDomainError,

View File

@ -1,8 +1,9 @@
import { Password } from "@/contexts/common/domain";
import { import {
ISequelizeMapper, ISequelizeMapper,
SequelizeMapper, SequelizeMapper,
} from "@/contexts/common/infrastructure"; } from "@/contexts/common/infrastructure";
import { Email, Name, Password, UniqueID } from "@shared/contexts"; import { Email, Name, UniqueID } from "@shared/contexts";
import { IUserProps, User } from "../../domain"; import { IUserProps, User } from "../../domain";
import { IUserContext } from "../User.context"; import { IUserContext } from "../User.context";
import { TCreationUser_Attributes, User_Model } from "../sequelize/user.model"; import { TCreationUser_Attributes, User_Model } from "../sequelize/user.model";
@ -25,7 +26,7 @@ class UserMapper
password: this.mapsValue( password: this.mapsValue(
source, source,
"password", "password",
Password.createFromHashedText, Password.createFromHashedTextPassword,
), ),
}; };

View File

@ -11,7 +11,6 @@ export * from "./MoneyValue";
export * from "./Name"; export * from "./Name";
export * from "./Note"; export * from "./Note";
export * from "./NullableValueObject"; export * from "./NullableValueObject";
export * from "./Password";
export * from "./Percentage"; export * from "./Percentage";
export * from "./Phone"; export * from "./Phone";
export * from "./Quantity"; export * from "./Quantity";