UNPKG

@tomei/sso

Version:
114 lines 5.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserPasswordHistory = void 0; const general_1 = require("@tomei/general"); const config_1 = require("@tomei/config"); const user_password_history_repository_1 = require("./user-password-history.repository"); class UserPasswordHistory extends general_1.ObjectBase { get HistoryId() { return this.ObjectId; } set HistoryId(value) { this.ObjectId = value; } get CreatedAt() { return this._CreatedAt; } constructor(params) { super(); this.ObjectType = 'UserPasswordHistory'; this.TableName = 'sso_UserPasswordHistory'; if (params) { this.ObjectId = params.HistoryId; this.UserId = params.UserId; this.PasswordHash = params.PasswordHash; this._CreatedAt = params.CreatedAt; } } static init(historyId, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { if (historyId) { const data = yield UserPasswordHistory._Repo.findByPk(historyId.toString(), dbTransaction); if (!data) { throw new general_1.ClassError('UserPasswordHistory', 'UserPasswordHistoryErrMsg01', 'UserPasswordHistory not found', 'init', 400); } return new UserPasswordHistory(data.get({ plain: true })); } return new UserPasswordHistory(); } catch (error) { throw error; } }); } static validate(dbTransaction, UserId, Password, passwordHashService) { return __awaiter(this, void 0, void 0, function* () { try { const passwordHistoryPolicy = config_1.ComponentConfig.getComponentConfigValue('@tomei/sso', 'passwordHistory') || 3; let passwordHistory = yield UserPasswordHistory._Repo.findAll({ where: { UserId: UserId }, order: [['CreatedAt', 'DESC']], limit: passwordHistoryPolicy, transaction: dbTransaction, }); if ((passwordHistory === null || passwordHistory === void 0 ? void 0 : passwordHistory.length) < 1) { return null; } else { for (let index = 0; index < passwordHistory.length; index++) { const isPasswordSame = yield passwordHashService.verify(Password, passwordHistory[index].PasswordHash); if (isPasswordSame) { throw new general_1.ClassError('UserPasswordHistory', 'UserPasswordHistory01', `You cannot reuse your last ${passwordHistoryPolicy} passwords. Please choose a new and unique password.`, 'validate', 403); } } } } catch (error) { throw error; } }); } static create(dbTransaction, UserId, PasswordHash) { return __awaiter(this, void 0, void 0, function* () { try { const passwordHistoryPolicy = config_1.ComponentConfig.getComponentConfigValue('@tomei/sso', 'passwordHistory') || 3; const userPasswordHistory = new UserPasswordHistory(); let passwordHistory = yield UserPasswordHistory._Repo.create({ HistoryId: userPasswordHistory.createId(), UserId: UserId, PasswordHash: PasswordHash, }, { transaction: dbTransaction, }); if (passwordHistory) { let passwordHistoryList = yield UserPasswordHistory._Repo.findAll({ where: { UserId: UserId }, order: [['CreatedAt', 'DESC']], transaction: dbTransaction, }); if (passwordHistoryList.length > passwordHistoryPolicy) { let deleteList = passwordHistoryList.slice(passwordHistoryPolicy); let historyIdList = deleteList.map((record) => record.HistoryId); yield UserPasswordHistory._Repo.destroyMultiple(historyIdList, dbTransaction); } } } catch (error) { throw error; } }); } } exports.UserPasswordHistory = UserPasswordHistory; UserPasswordHistory._Repo = new user_password_history_repository_1.UserPasswordHistoryRepository(); //# sourceMappingURL=user-password-history.js.map