UNPKG

@type-ddd/password

Version:

Library that provides TypeScript type definitions for handling Password in Domain-Driven Design contexts. It facilitates the validation and manipulation of passwords.

122 lines (121 loc) 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Password = void 0; const bcrypt_1 = require("bcrypt"); const rich_domain_1 = require("rich-domain"); const utils_1 = require("./utils"); const regexHash = /^\$2b\$10\$.{53}$/; class Password extends rich_domain_1.ValueObject { constructor(props) { super(props); } /** * @returns value as string */ value() { return this.props; } /** * * @description compare plainText with encrypted password * @param plainText plainText not encrypted to compare with encrypted password * @returns true if match else false */ compare(plainText) { if (this.isEncrypted()) { return (0, bcrypt_1.compareSync)(plainText, this.props); } return plainText === this.props; } /** * * @returns true if instance value is encrypted else false */ isEncrypted() { const isEncrypted = this.validator .string(this.props) .match(Password.REGEX); return isEncrypted; } /** * * @returns true if provided value is encrypted else false */ static isEncrypted(value) { return this.validator.string(value).match(Password.REGEX); } /** * * @param length password length as number 8/10/12/14/16/18 * @returns PasswordValueObject * @default 12 chars or greater is recommended for strongest password */ static random(length) { const pass = (0, utils_1.default)(length ?? 12); return Password.create(pass).value(); } /** * @summary Encrypts the password. * @description This method encrypts the password using bcrypt hashing algorithm. If the password is already encrypted, it returns the encrypted password as it is. Otherwise, it generates a salt, hashes the password with the salt, and returns the encrypted password. * @returns A Password object representing the encrypted password. */ encrypt() { const isEncrypted = this.isEncrypted(); if (isEncrypted) { const encrypted = this.props; return new Password(encrypted); } const salt = (0, bcrypt_1.genSaltSync)(); const encrypted = (0, bcrypt_1.hashSync)(this.props, salt); return new Password(encrypted); } /** * * @param value check if password has a valid value length * @returns true if is all ok or false else not */ static isValid(value) { return this.isValidProps(value); } /** * * @param value check if password has a valid value length * @returns true if is all ok or false else not */ static isValidProps(value) { const { string } = this.validator; if (!Password.isEncrypted(value)) { const passwordHasRequiredLength = string(value).hasLengthBetweenOrEqual(Password.MIN_LENGTH, Password.MAX_LENGTH); return passwordHasRequiredLength; } return true; } /** * * @param value value as string * @returns instance of Password or throw an error */ static init(value) { const isValidValue = Password.isValidProps(value); if (!isValidValue) throw new Error(Password.MESSAGE); return new Password(value); } /** * * @param value password to create * @returns Result of PasswordValueObject */ static create(value) { if (!Password.isValidProps(value)) { return rich_domain_1.Result.fail(Password.MESSAGE); } return rich_domain_1.Result.Ok(new Password(value)); } } exports.Password = Password; Password.MAX_LENGTH = 22; Password.MIN_LENGTH = 5; Password.REGEX = regexHash; Password.MESSAGE = `Password must has min ${Password.MIN_LENGTH} and max ${Password.MAX_LENGTH} chars`; exports.default = Password;