UNPKG

will-core

Version:
198 lines (197 loc) 7.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiffieHandler = void 0; const will_dh_1 = require("will-dh"); const will_sql_1 = require("will-sql"); const SchemeHandler_1 = require("./SchemeHandler"); const AuthorizeHandler_1 = require("./AuthorizeHandler"); class DiffieHandler extends SchemeHandler_1.SchemeHandler { constructor() { super(...arguments); this.model = { name: "tusertoken", alias: { privateAlias: this.section } }; //declared addon actions name this.handlers = [{ name: "dh" }, { name: "encrypt" }, { name: "decrypt" }]; } dh(context) { return this.diffie(context); } async diffie(context) { return this.callFunctional(context, { operate: "diffie", raw: false }, this.doDiffie); } async doDiffie(context, model) { let dh = await this.createDiffie(context); this.logger.debug(this.constructor.name + ".doDiffie: dh", dh); let info = this.createDiffieInfo(dh); let body = { info: info }; this.doSaveDiffie(context, model, dh).then(rs => { this.logger.debug(this.constructor.name + ".doSaveDiffie", rs); }); return Promise.resolve(body); } async createDiffie(context) { let session = context.meta?.session; let dh = new will_dh_1.DH(); if (session && session.hasOwnProperty("dh")) { let diffie = session.dh; dh.prime = diffie.prime; dh.generator = diffie.generator; dh.privateKey = diffie.privateKey; dh.publicKey = diffie.publicKey; dh.sharedKey = diffie.sharedKey; dh.otherPublicKey = diffie.otherPublicKey; } else { await dh.init(); } let publickey = context.params.publickey; if (publickey) { dh.otherPublicKey = publickey; dh.computeSharedKey(); } if (session) session.dh = dh; return Promise.resolve(dh); } createDiffieInfo(dh) { return { prime: dh.prime, generator: dh.generator, publickey: dh.publicKey, }; } async doSaveDiffie(context, model, dh) { let userInfo = await this.getUserTokenInfo(context); this.logger.debug(this.constructor.name + ".doSaveDiffie: userInfo", userInfo); if (userInfo) { let db = this.getPrivateConnector(model); try { return await this.saveDiffie(db, userInfo, dh, context); } catch (ex) { this.logger.error(this.constructor.name, ex); return Promise.reject(this.getDBError(ex)); } finally { if (db) db.close(); } } return Promise.resolve(undefined); } async saveDiffie(db, userInfo, dh, context) { let sql = new will_sql_1.KnSQL(); sql.append("update tusertoken set prime=?prime, generator=?generator, privatekey=?privatekey, "); sql.append("publickey=?publickey, sharedkey=?sharedkey, otherkey=?otherkey, tokenstatus=?tokenstatus "); sql.append("where useruuid=?useruuid "); sql.set("prime", dh.prime); sql.set("generator", dh.generator); sql.set("privatekey", dh.privateKey); sql.set("publickey", dh.publicKey); sql.set("sharedkey", dh.sharedKey); sql.set("otherkey", dh.otherPublicKey); sql.set("tokenstatus", userInfo.tokenstatus); sql.set("useruuid", userInfo.useruuid); this.logger.info(this.constructor.name + ".saveDiffie", sql); let rs = await sql.executeUpdate(db, context); return Promise.resolve(rs); } async doUpdate(context, model) { let result = { rows: null, columns: null }; let userInfo = null; let token = this.getTokenKey(context); this.logger.debug(this.constructor.name + ".doUpdate: token", token); let db = this.getPrivateConnector(model); try { if (token != undefined) { let alib = new AuthorizeHandler_1.AuthorizeHandler(); userInfo = await alib.getAuthorizeTokenInfo(db, token); } if (userInfo) { let dh = await this.getUserDiffie(userInfo, false, true); if (dh) { let publickey = context.params.publickey; if (publickey) { dh.otherPublicKey = publickey; dh.computeSharedKey(); } userInfo.prime = dh.prime; userInfo.generator = dh.generator; userInfo.privatekey = dh.privateKey; userInfo.publickey = dh.publicKey; userInfo.otherkey = dh.otherPublicKey; userInfo.sharedkey = dh.sharedKey; userInfo.tokenstatus = "C"; let session = context.meta?.session; if (session) { //session.dh = dh; session.user = userInfo; } context.meta.user = userInfo; result = await this.saveDiffie(db, userInfo, dh, context); } } } catch (ex) { this.logger.error(this.constructor.name, ex); return Promise.reject(this.getDBError(ex)); } finally { if (db) db.close(); } return this.createRecordSet(result); } async encrypt(context) { let body = {}; let plaintext = context.params.plaintext; let session = context.meta?.session; let dh = null; if (session && session.hasOwnProperty("dh")) { dh = new will_dh_1.DH(); let diffie = session.dh; dh.prime = diffie.prime; dh.generator = diffie.generator; dh.privateKey = diffie.privateKey; dh.publicKey = diffie.publicKey; dh.sharedKey = diffie.sharedKey; dh.otherPublicKey = diffie.otherPublicKey; } else { dh = await this.getUserDH(context); } if (dh && plaintext) { body.plaintext = plaintext; let enctext = dh.encrypt(plaintext); console.log("encrypt text", enctext); body.ciphertext = enctext; return Promise.resolve(body); } return Promise.reject("Diffie info not found"); } async decrypt(context) { let body = {}; let ciphertext = context.params.ciphertext; let session = context.meta?.session; let dh = null; if (session && session.hasOwnProperty("dh")) { dh = new will_dh_1.DH(); let diffie = session.dh; dh.prime = diffie.prime; dh.generator = diffie.generator; dh.privateKey = diffie.privateKey; dh.publicKey = diffie.publicKey; dh.sharedKey = diffie.sharedKey; dh.otherPublicKey = diffie.otherPublicKey; } else { dh = await this.getUserDH(context); } if (dh && ciphertext) { let dectext = dh.decrypt(ciphertext); console.log("decrypt text", dectext); body.plaintext = dectext; body.ciphertext = dh.encrypt(dectext); return Promise.resolve(body); } return Promise.reject("Diffie info not found"); } } exports.DiffieHandler = DiffieHandler;