UNPKG

will-lib

Version:
114 lines (113 loc) 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BasicLibrary = void 0; const will_sql_1 = require("will-sql"); const HTTP_1 = require("./HTTP"); const AuthenError_1 = require("./AuthenError"); const crypto = require("crypto"); class BasicLibrary { static getAuthorization(authorization) { if (authorization && authorization.length > 0) { let idx = authorization.indexOf(' '); if (idx > 0) { let auth = authorization.substring(0, idx); if ("basic" == auth.toLowerCase()) { return authorization.substring(idx + 1); } } } return authorization; } getBasicInfo(basic) { if (basic) { let idx = basic.indexOf(':'); if (idx > 0) { let usr = basic.substring(0, idx); let pwd = basic.substring(idx + 1); return { username: usr, password: pwd }; } } return undefined; } decodeBase64(base64) { let texts = this.fromBase64(base64); this.plainText = texts; return this.getBasicInfo(texts); } fromBase64(base64) { let buf = Buffer.from(base64, "base64"); return buf.toString("utf8"); } toBase64(plainText) { let buf = Buffer.from(plainText); return buf.toString("base64"); } decodeWithKey(cipherText, privatekeys) { if (!privatekeys.includes("BEGIN PRIVATE KEY") && !privatekeys.includes("BEGIN RSA PRIVATE KEY")) { privatekeys = "-----BEGIN PRIVATE KEY-----\n" + privatekeys + "\n-----END PRIVATE KEY-----\n"; } const decryptedData = crypto.privateDecrypt({ key: privatekeys, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: "sha256", }, Buffer.from(cipherText, "base64")); let texts = decryptedData.toString("utf8"); this.plainText = texts; return this.getBasicInfo(texts); } async decrypt(authorization, client, conn) { let info = undefined; let authorizeText = BasicLibrary.getAuthorization(authorization); if (authorizeText) { if (client && conn) { let tenant = await this.getTenantInfo(conn, client); if (!tenant) { return Promise.reject(new AuthenError_1.AuthenError("Client not found", HTTP_1.HTTP.NOT_FOUND)); } return this.decodeWithKey(authorizeText, tenant.privatekeys); } return this.decodeBase64(authorizeText); } return Promise.resolve(info); } encodeWithKey(plainText, publickeys) { if (!publickeys.includes("BEGIN PUBLIC KEY") && !publickeys.includes("BEGIN RSA PUBLIC KEY")) { publickeys = "-----BEGIN PUBLIC KEY-----\n" + publickeys + "\n-----END PUBLIC KEY-----\n"; } const encryptedData = crypto.publicEncrypt({ key: publickeys, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: "sha256", }, Buffer.from(plainText)); return encryptedData.toString("base64"); } async encrypt(plainText, client, conn) { if (client && conn) { let tenant = await this.getTenantInfo(conn, client); if (!tenant) { return Promise.reject(new AuthenError_1.AuthenError("Client not found", HTTP_1.HTTP.NOT_FOUND)); } return this.encodeWithKey(plainText, tenant.publickeys); } let buf = Buffer.from(plainText); return buf.toString("base64"); } async getTenantInfo(conn, client) { let info = undefined; let sql = new will_sql_1.KnSQL("select * from ttenant where tenantid = ?tenantid and inactive = '0' "); sql.set("tenantid", client); let rs = await sql.executeQuery(conn); if (rs.rows && rs.rows.length > 0) { let row = rs.rows[0]; info = { tenantid: row.tenantid, tenantname: row.tenantname, applicationid: row.applicationid, privatekeys: row.privatekeys, publickeys: row.publickeys, }; } return Promise.resolve(info); } } exports.BasicLibrary = BasicLibrary;