badmfck-api-server 4.1.46 → 4.1.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { IInterceptor } from "../APIService";
2
+ import { HTTPRequestVO } from "../structures/Interfaces";
3
+ export declare class InterceptorPayAuth implements IInterceptor<any> {
4
+ intercept(req: HTTPRequestVO, authorizations: {
5
+ key: string;
6
+ kid: string;
7
+ }[]): Promise<any>;
8
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InterceptorPayAuth = void 0;
7
+ const crypto_1 = __importDefault(require("crypto"));
8
+ const Validator_1 = require("../helper/Validator");
9
+ const DefaultErrors_1 = __importDefault(require("../structures/DefaultErrors"));
10
+ const _TAuthParams = {
11
+ v: 0,
12
+ ts: "",
13
+ nonce: "",
14
+ kid: "",
15
+ alg: "",
16
+ $__alg_optional: true,
17
+ sig: ""
18
+ };
19
+ class InterceptorPayAuth {
20
+ async intercept(req, authorizations) {
21
+ const authScheme = "payauth ";
22
+ let headerData = req.headers["authorization"];
23
+ if (!headerData)
24
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No authorization header" };
25
+ if (!headerData.toLowerCase().startsWith(authScheme))
26
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "wrong authorization scheme" };
27
+ const b64 = req.data.b64;
28
+ if (req.data.b64) {
29
+ let json = null;
30
+ try {
31
+ json = Buffer.from(req.data.b64, "base64").toString("utf-8");
32
+ json = JSON.parse(json);
33
+ }
34
+ catch (e) {
35
+ throw { ...DefaultErrors_1.default.BAD_REQUEST, details: "JSON malformed" };
36
+ }
37
+ req.data = json;
38
+ }
39
+ const params = {};
40
+ headerData.substring(authScheme.length).trim().split(",").forEach(v => {
41
+ v = v.trim();
42
+ const tmp = v.split("=");
43
+ if (tmp.length === 2) {
44
+ const key = tmp[0].trim().toLowerCase();
45
+ let value = tmp[1].trim();
46
+ if (value.startsWith("'") && value.endsWith("'")) {
47
+ value = value.substring(1, value.length - 1);
48
+ }
49
+ params[key] = value;
50
+ }
51
+ });
52
+ const errors = await Validator_1.Validator.validateStructure(_TAuthParams, params);
53
+ if (errors)
54
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Malformed authorization header", stack: errors };
55
+ for (let i of authorizations) {
56
+ if (i.kid === params.kid) {
57
+ if (params.alg.toLowerCase() === "hmac-sha256") {
58
+ if (b64) {
59
+ const key = Buffer.from(i.key, "hex");
60
+ const sig = crypto_1.default.createHmac("sha256", key).update(b64 + params.ts + params.nonce).digest("hex");
61
+ if (sig === params.sig)
62
+ return i;
63
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Signature mismatch, calc:" + sig + ", got:" + params.sig, stack: params };
64
+ }
65
+ else {
66
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Can't calc signature, no b64" };
67
+ }
68
+ }
69
+ else {
70
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Unsupported alg" };
71
+ }
72
+ }
73
+ }
74
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Key not found" };
75
+ }
76
+ }
77
+ exports.InterceptorPayAuth = InterceptorPayAuth;
package/dist/index.d.ts CHANGED
@@ -18,4 +18,5 @@ import { MicroserviceHost } from "./apiServer/external/MicroserviceHost";
18
18
  import { MicroserviceClient } from "./apiServer/external/MicroserviceClient";
19
19
  import { Deploy } from "./apiServer/deployment/Deploy";
20
20
  import { Activate } from "./apiServer/deployment/Activate";
21
- export { MicroserviceHost, MicroserviceClient, Http, ZipUtils, UID, YYYYMMDDHH, JSONStableStringify, APIService, Initializer, LocalRequest, ValidationModel, MysqlService, TimeframeService, Validator, LogService, DataProvider, ErrorUtils, ExternalService, DBService, Deploy, Activate, S_MONITOR_REGISTRATE_ACTION };
21
+ import { InterceptorPayAuth } from "./apiServer/interceptors/InterceptorPayAuth";
22
+ export { MicroserviceHost, MicroserviceClient, Http, ZipUtils, UID, YYYYMMDDHH, JSONStableStringify, APIService, Initializer, LocalRequest, ValidationModel, MysqlService, TimeframeService, Validator, LogService, DataProvider, ErrorUtils, ExternalService, DBService, Deploy, Activate, InterceptorPayAuth, S_MONITOR_REGISTRATE_ACTION };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.S_MONITOR_REGISTRATE_ACTION = exports.Activate = exports.Deploy = exports.DBService = exports.ExternalService = exports.ErrorUtils = exports.DataProvider = exports.LogService = exports.Validator = exports.TimeframeService = exports.MysqlService = exports.LocalRequest = exports.Initializer = exports.APIService = exports.JSONStableStringify = exports.YYYYMMDDHH = exports.UID = exports.ZipUtils = exports.Http = exports.MicroserviceClient = exports.MicroserviceHost = void 0;
3
+ exports.S_MONITOR_REGISTRATE_ACTION = exports.InterceptorPayAuth = exports.Activate = exports.Deploy = exports.DBService = exports.ExternalService = exports.ErrorUtils = exports.DataProvider = exports.LogService = exports.Validator = exports.TimeframeService = exports.MysqlService = exports.LocalRequest = exports.Initializer = exports.APIService = exports.JSONStableStringify = exports.YYYYMMDDHH = exports.UID = exports.ZipUtils = exports.Http = exports.MicroserviceClient = exports.MicroserviceHost = void 0;
4
4
  const APIService_1 = require("./apiServer/APIService");
5
5
  Object.defineProperty(exports, "APIService", { enumerable: true, get: function () { return APIService_1.APIService; } });
6
6
  Object.defineProperty(exports, "Initializer", { enumerable: true, get: function () { return APIService_1.Initializer; } });
@@ -42,3 +42,5 @@ const Deploy_1 = require("./apiServer/deployment/Deploy");
42
42
  Object.defineProperty(exports, "Deploy", { enumerable: true, get: function () { return Deploy_1.Deploy; } });
43
43
  const Activate_1 = require("./apiServer/deployment/Activate");
44
44
  Object.defineProperty(exports, "Activate", { enumerable: true, get: function () { return Activate_1.Activate; } });
45
+ const InterceptorPayAuth_1 = require("./apiServer/interceptors/InterceptorPayAuth");
46
+ Object.defineProperty(exports, "InterceptorPayAuth", { enumerable: true, get: function () { return InterceptorPayAuth_1.InterceptorPayAuth; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "4.1.46",
3
+ "version": "4.1.47",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",