UNPKG

@tomei/sso

Version:
257 lines 11.6 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.APIKey = void 0; const general_1 = require("@tomei/general"); const api_key_enum_1 = require("../../enum/api-key.enum"); const api_key_repository_1 = require("./api-key.repository"); const config_1 = require("@tomei/config"); const crypto_1 = require("crypto"); const activity_history_1 = require("@tomei/activity-history"); const sequelize_1 = require("sequelize"); const system_1 = require("../system/system"); class APIKey extends general_1.ObjectBase { get APIKeyId() { return parseInt(this.ObjectId); } set APIKeyId(value) { this.ObjectId = value.toString(); } get RevokedById() { return this._RevokedById; } get RevokedAt() { return this._RevokedAt; } get CreatedAt() { return this._CreatedAt; } get CreatedById() { return this._CreatedById; } constructor(apiKeyAttr) { super(); if (apiKeyAttr) { this.APIKeyId = apiKeyAttr.APIKeyId; this.ApiKey = apiKeyAttr.ApiKey; this.Name = apiKeyAttr.Name; this.SystemCode = apiKeyAttr.SystemCode; this.Description = apiKeyAttr.Description; this.Status = apiKeyAttr.Status; this.ExpirationDate = apiKeyAttr.ExpirationDate; this._RevokedById = apiKeyAttr.RevokedById; this._RevokedAt = apiKeyAttr.RevokedAt; this._CreatedAt = apiKeyAttr.CreatedAt; this._CreatedById = apiKeyAttr.CreatedById; } } static init(ApiKeyId, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { if (ApiKeyId) { const apiKeyAttr = yield this._Repo.findByPk(ApiKeyId.toString(), dbTransaction); if (apiKeyAttr) { return new APIKey(apiKeyAttr); } else { throw new general_1.ClassError('APIKey', 'APIKeyErrMsgO1', 'APIKey not found', 'init'); } } return new APIKey(); } catch (error) { throw error; } }); } generate(loginUser, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'API_KEY_CREATE'); if (!isPrivileged) { throw new general_1.ClassError('APIKey', 'APIKeyErrMsgO2', 'User does not have privilege to generate API key.', 'generate'); } yield system_1.System.init(dbTransaction, this.SystemCode); this._CreatedById = loginUser.UserId; this._CreatedAt = new Date(); this.ApiKey = (0, crypto_1.randomBytes)(64).toString('hex'); const EntityValueAfter = { ApiKey: this.ApiKey, Name: this.Name, SystemCode: this.SystemCode, Status: this.Status, Description: this.Description, ExpirationDate: this.ExpirationDate, CreatedAt: this.CreatedAt, CreatedById: this.CreatedById, RevokedAt: this.RevokedAt, RevokedById: this.RevokedById, RevokedReason: this.RevokedReason, }; const data = yield APIKey._Repo.create(EntityValueAfter, { transaction: dbTransaction, }); this.APIKeyId = data.APIKeyId; EntityValueAfter.ApiKeyId = data.APIKeyId; const EntityValueBefore = {}; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Generate API key.'; activity.EntityType = 'APIKey'; activity.EntityId = this.ObjectId; activity.EntityValueBefore = JSON.stringify(EntityValueBefore); activity.EntityValueAfter = JSON.stringify(EntityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return { ApiKey: this.ApiKey, Name: this.Name, Status: this.Status, Description: this.Description, SystemCode: this.SystemCode, ExpirationDate: this.ExpirationDate, CreatedAt: this.CreatedAt, CreatedById: this.CreatedById, RevokedAt: this.RevokedAt, RevokedById: this.RevokedById, RevokedReason: this.RevokedReason, }; } catch (error) { throw error; } }); } static findAll(pagination, loginUser, dbTransaction, whereOptions, sortOptions) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'API_KEY_VIEW'); if (!isPrivileged) { throw new general_1.ClassError('APIKey', 'APIKeyErrMsgO2', 'User does not have privilege to generate API key.', 'generate'); } const where = {}; if (whereOptions) { if (whereOptions.SystemCode) { where['SystemCode'] = whereOptions.SystemCode; } if (whereOptions.Status) { where['Status'] = whereOptions.Status; } if (whereOptions.ExpirationDate) { where['ExpirationDate'] = { [sequelize_1.Op.between]: [ whereOptions.ExpirationDate.FromDate, whereOptions.ExpirationDate.ToDate, ], }; } if (whereOptions.CreatedById) { where['CreatedById'] = whereOptions.CreatedById; } } const order = []; if (sortOptions) { if (sortOptions.SortBy) { order.push([sortOptions.SortBy, sortOptions.SortOrder]); } } else { order.push(['CreatedAt', 'DESC']); } let offset = 0; if (pagination) { offset = (pagination.page - 1) * pagination.limit; } const data = yield APIKey._Repo.findAllWithPagination({ where, order, offset, limit: pagination.limit, transaction: dbTransaction, distinct: true, }); return { total: data.count, ApiKeys: data.rows.map((row) => { return { ApiKeyId: row.APIKeyId, ApiKey: row.ApiKey, Name: row.Name, SystemCode: row.SystemCode, Description: row.Description, Status: row.Status, ExpirationDate: row.ExpirationDate, CreatedAt: row.CreatedAt, CreatedById: row.CreatedById, RevokedAt: row.RevokedAt, RevokedById: row.RevokedById, RevokedReason: row.RevokedReason, }; }), page: pagination.page, limit: pagination.limit, }; } catch (error) { throw error; } }); } revoke(apiKey, loginUser, dbTransaction, reason) { return __awaiter(this, void 0, void 0, function* () { try { const apiKeyRecord = yield APIKey._Repo.findOne({ where: { ApiKey: apiKey }, transaction: dbTransaction, }); if (!apiKeyRecord) { throw new general_1.ClassError('APIKey', 'APIKeyErrMsgO3', 'API key not found.', 'revoke'); } const EntityValueBefore = Object.assign({}, apiKeyRecord.get({ plain: true })); apiKeyRecord.Status = api_key_enum_1.APIKeyStatusEnum.REVOKED; apiKeyRecord.RevokedAt = new Date(); apiKeyRecord.RevokedById = loginUser.UserId; if (reason) { apiKeyRecord.RevokedReason = reason; } yield APIKey._Repo.update(Object.assign({}, apiKeyRecord.get({ plain: true })), { where: { APIKeyId: apiKeyRecord.APIKeyId }, transaction: dbTransaction, }); const EntityValueAfter = Object.assign({}, apiKeyRecord.get({ plain: true })); const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Revoke API key.'; activity.EntityType = 'APIKey'; activity.EntityId = apiKeyRecord.APIKeyId.toString(); activity.EntityValueBefore = JSON.stringify(EntityValueBefore); activity.EntityValueAfter = JSON.stringify(EntityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return { ApiKey: apiKeyRecord.ApiKey, Status: apiKeyRecord.Status, RevokedAt: apiKeyRecord.RevokedAt, RevokedById: apiKeyRecord.RevokedById, RevokedReason: apiKeyRecord.RevokedReason, }; } catch (error) { throw error; } }); } } exports.APIKey = APIKey; APIKey._Repo = new api_key_repository_1.APIKeyRepository(); //# sourceMappingURL=api-key.js.map