UNPKG

will-auth

Version:

authentication and authorization module

306 lines (305 loc) 15.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiffieHandler = void 0; var will_dh_1 = require("will-dh"); var will_api_1 = require("will-api"); var will_sql_1 = require("will-sql"); var VerifyError_1 = require("../models/VerifyError"); var SchemeHandler_1 = require("./SchemeHandler"); var AuthorizeHandler_1 = require("./AuthorizeHandler"); var DiffieHandler = /** @class */ (function (_super) { __extends(DiffieHandler, _super); function DiffieHandler() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.model = { name: "tusertoken", alias: { privateAlias: _this.section } }; //declared addon actions name _this.handlers = [{ name: "dh" }, { name: "encrypt" }, { name: "decrypt" }]; return _this; } DiffieHandler.prototype.dh = function (context) { return this.diffie(context); }; DiffieHandler.prototype.diffie = function (context) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(this.model && this.isValidModelConfig("privateAlias", this.model))) return [3 /*break*/, 2]; return [4 /*yield*/, this.exposeFunctional(context, this.model, { operate: "diffie" })]; case 1: _a.sent(); return [2 /*return*/, this.doDiffie(context, this.model)]; case 2: return [2 /*return*/, Promise.reject(new VerifyError_1.VerifyError("Invalid setting", will_api_1.HTTP.NOT_ACCEPTABLE, -16006))]; } }); }); }; DiffieHandler.prototype.doDiffie = function (context, model) { return __awaiter(this, void 0, void 0, function () { var dh, info, body; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.createDiffie(context)]; case 1: dh = _a.sent(); this.logger.debug("dh", dh); info = this.createDiffieInfo(dh); body = { info: info }; this.doSaveDiffie(context, model, dh).then(function (rs) { _this.logger.debug("doSaveDiffie", rs); }); return [2 /*return*/, Promise.resolve(body)]; } }); }); }; DiffieHandler.prototype.createDiffie = function (context) { return __awaiter(this, void 0, void 0, function () { var session, dh, diffie, publickey; return __generator(this, function (_a) { switch (_a.label) { case 0: session = context.meta.session; dh = new will_dh_1.DH(); if (!session.dh) return [3 /*break*/, 1]; 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; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, dh.init()]; case 2: _a.sent(); _a.label = 3; case 3: publickey = context.params.publickey; if (publickey) { dh.otherPublicKey = publickey; dh.computeSharedKey(); } session.dh = dh; return [2 /*return*/, Promise.resolve(dh)]; } }); }); }; DiffieHandler.prototype.createDiffieInfo = function (dh) { return { prime: dh.prime, generator: dh.generator, publickey: dh.publicKey, }; }; DiffieHandler.prototype.doSaveDiffie = function (context, model, dh) { return __awaiter(this, void 0, void 0, function () { var userInfo, db; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getUserTokenInfo(context)]; case 1: userInfo = _a.sent(); //this.logger.debug("DiffieHandler.doDiffieSave: userInfo",userInfo); if (userInfo) { db = this.getPrivateConnector(model); try { return [2 /*return*/, this.saveDiffie(db, userInfo, dh)]; } finally { db.close(); } } return [2 /*return*/, Promise.resolve(undefined)]; } }); }); }; DiffieHandler.prototype.saveDiffie = function (db, userInfo, dh) { return __awaiter(this, void 0, void 0, function () { var sql, rs; return __generator(this, function (_a) { switch (_a.label) { case 0: 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 "); 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("useruuid", userInfo.useruuid); this.logger.info(sql); return [4 /*yield*/, sql.executeUpdate(db)]; case 1: rs = _a.sent(); return [2 /*return*/, Promise.resolve(rs)]; } }); }); }; DiffieHandler.prototype.doUpdate = function (context, model) { return __awaiter(this, void 0, void 0, function () { var result, userInfo, db, token, alib, dh, publickey, session; return __generator(this, function (_a) { switch (_a.label) { case 0: result = { rows: null, columns: null }; return [4 /*yield*/, this.getUserTokenInfo(context)]; case 1: userInfo = _a.sent(); db = this.getPrivateConnector(model); _a.label = 2; case 2: _a.trys.push([2, , 8, 9]); if (!!userInfo) return [3 /*break*/, 4]; token = this.getTokenKey(context); if (!(token != undefined)) return [3 /*break*/, 4]; alib = new AuthorizeHandler_1.AuthorizeHandler(); return [4 /*yield*/, alib.getAuthorizeTokenInfo(db, token)]; case 3: userInfo = _a.sent(); _a.label = 4; case 4: if (!userInfo) return [3 /*break*/, 7]; return [4 /*yield*/, this.getUserDiffie(userInfo)]; case 5: dh = _a.sent(); if (!dh) return [3 /*break*/, 7]; 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; session = context.meta.session; if (session) { session.dh = dh; session.user = userInfo; } context.meta.user = userInfo; return [4 /*yield*/, this.saveDiffie(db, userInfo, dh)]; case 6: result = _a.sent(); _a.label = 7; case 7: return [3 /*break*/, 9]; case 8: db.close(); return [7 /*endfinally*/]; case 9: return [2 /*return*/, this.createRecordSet(result)]; } }); }); }; DiffieHandler.prototype.encrypt = function (context) { return __awaiter(this, void 0, void 0, function () { var plaintext, session, dh, diffie, body, enctext; return __generator(this, function (_a) { plaintext = context.params.plaintext; session = context.meta.session; dh = new will_dh_1.DH(); diffie = session.diffie; dh.prime = diffie.prime; dh.generator = diffie.generator; dh.privateKey = diffie.privateKey; dh.publicKey = diffie.publicKey; dh.sharedKey = diffie.sharedKey; dh.otherPublicKey = diffie.otherPublicKey; body = {}; if (dh && plaintext) { body.plaintext = plaintext; enctext = dh.encrypt(plaintext); console.log("encrypt text", enctext); body.ciphertext = enctext; } return [2 /*return*/, Promise.resolve(body)]; }); }); }; DiffieHandler.prototype.decrypt = function (context) { return __awaiter(this, void 0, void 0, function () { var ciphertext, session, dh, diffie, body, dectext; return __generator(this, function (_a) { ciphertext = context.params.ciphertext; session = context.meta.session; dh = new will_dh_1.DH(); diffie = session.diffie; dh.prime = diffie.prime; dh.generator = diffie.generator; dh.privateKey = diffie.privateKey; dh.publicKey = diffie.publicKey; dh.sharedKey = diffie.sharedKey; dh.otherPublicKey = diffie.otherPublicKey; body = {}; if (dh && ciphertext) { dectext = dh.decrypt(ciphertext); console.log("decrypt text", dectext); body.plaintext = dectext; body.ciphertext = dh.encrypt(dectext); } return [2 /*return*/, Promise.resolve(body)]; }); }); }; return DiffieHandler; }(SchemeHandler_1.SchemeHandler)); exports.DiffieHandler = DiffieHandler;