@opengovsg/formsg-sdk 0.10.0 → 0.12.0-alpha.1
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.
- package/.github/workflows/ci.yml +6 -4
- package/LICENSE.md +21 -0
- package/README.md +8 -1
- package/dist/crypto-base.d.ts +25 -0
- package/dist/crypto-base.js +93 -0
- package/dist/crypto-v3.d.ts +37 -0
- package/dist/crypto-v3.js +134 -0
- package/dist/crypto.d.ts +4 -25
- package/dist/crypto.js +43 -64
- package/dist/errors.js +4 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +4 -2
- package/dist/resource/signing-keys.js +1 -0
- package/dist/resource/verification-keys.js +1 -0
- package/dist/types.d.ts +37 -18
- package/dist/util/crypto.d.ts +3 -3
- package/dist/util/crypto.js +22 -15
- package/dist/util/parser.d.ts +2 -2
- package/dist/util/parser.js +5 -2
- package/dist/util/publicKey.js +1 -0
- package/dist/util/signature.js +21 -4
- package/dist/util/validate.d.ts +3 -2
- package/dist/util/validate.js +13 -0
- package/dist/util/webhooks.js +21 -4
- package/dist/verification/index.js +9 -9
- package/dist/verification/utils.js +6 -3
- package/dist/webhooks.js +27 -11
- package/package.json +9 -9
package/dist/webhooks.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
2
18
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
19
|
if (mod && mod.__esModule) return mod;
|
|
4
20
|
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result
|
|
6
|
-
result
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
7
23
|
return result;
|
|
8
24
|
};
|
|
9
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -14,8 +30,8 @@ var webhooks_1 = require("./util/webhooks");
|
|
|
14
30
|
var errors_1 = require("./errors");
|
|
15
31
|
var Webhooks = /** @class */ (function () {
|
|
16
32
|
function Webhooks(_a) {
|
|
17
|
-
var _this = this;
|
|
18
33
|
var publicKey = _a.publicKey, secretKey = _a.secretKey;
|
|
34
|
+
var _this = this;
|
|
19
35
|
/**
|
|
20
36
|
* Injects the webhook public key for authentication
|
|
21
37
|
* @param header X-FormSG-Signature header
|
|
@@ -25,15 +41,15 @@ var Webhooks = /** @class */ (function () {
|
|
|
25
41
|
*/
|
|
26
42
|
this.authenticate = function (header, uri) {
|
|
27
43
|
// Parse the header
|
|
28
|
-
var signatureHeader = parser_1.parseSignatureHeader(header);
|
|
44
|
+
var signatureHeader = (0, parser_1.parseSignatureHeader)(header);
|
|
29
45
|
var signature = signatureHeader.v1, epoch = signatureHeader.t, submissionId = signatureHeader.s, formId = signatureHeader.f;
|
|
30
46
|
// Verify signature authenticity
|
|
31
|
-
if (!webhooks_1.isSignatureHeaderValid(uri, signatureHeader, _this.publicKey)) {
|
|
32
|
-
throw new errors_1.WebhookAuthenticateError("Signature could not be verified for uri="
|
|
47
|
+
if (!(0, webhooks_1.isSignatureHeaderValid)(uri, signatureHeader, _this.publicKey)) {
|
|
48
|
+
throw new errors_1.WebhookAuthenticateError("Signature could not be verified for uri=".concat(uri, " submissionId=").concat(submissionId, " formId=").concat(formId, " epoch=").concat(epoch, " signature=").concat(signature));
|
|
33
49
|
}
|
|
34
50
|
// Verify epoch recency
|
|
35
|
-
if (webhooks_1.hasEpochExpired(epoch)) {
|
|
36
|
-
throw new errors_1.WebhookAuthenticateError("Signature is not recent for uri="
|
|
51
|
+
if ((0, webhooks_1.hasEpochExpired)(epoch)) {
|
|
52
|
+
throw new errors_1.WebhookAuthenticateError("Signature is not recent for uri=".concat(uri, " submissionId=").concat(submissionId, " formId=").concat(formId, " epoch=").concat(epoch, " signature=").concat(signature));
|
|
37
53
|
}
|
|
38
54
|
// All checks pass.
|
|
39
55
|
return true;
|
|
@@ -56,8 +72,8 @@ var Webhooks = /** @class */ (function () {
|
|
|
56
72
|
if (!submissionId || !uri || !formId || !epoch) {
|
|
57
73
|
throw new TypeError('submissionId, uri, formId, or epoch must be provided to generate a webhook signature');
|
|
58
74
|
}
|
|
59
|
-
var baseString = url.parse(uri).href
|
|
60
|
-
return signature_1.sign(baseString, _this.secretKey);
|
|
75
|
+
var baseString = "".concat(url.parse(uri).href, ".").concat(submissionId, ".").concat(formId, ".").concat(epoch);
|
|
76
|
+
return (0, signature_1.sign)(baseString, _this.secretKey);
|
|
61
77
|
};
|
|
62
78
|
/**
|
|
63
79
|
* Constructs the `X-FormSG-Signature` header
|
|
@@ -74,7 +90,7 @@ var Webhooks = /** @class */ (function () {
|
|
|
74
90
|
if (!_this.secretKey) {
|
|
75
91
|
throw new errors_1.MissingSecretKeyError();
|
|
76
92
|
}
|
|
77
|
-
return "t="
|
|
93
|
+
return "t=".concat(epoch, ",s=").concat(submissionId, ",f=").concat(formId, ",v1=").concat(signature);
|
|
78
94
|
};
|
|
79
95
|
this.publicKey = publicKey;
|
|
80
96
|
this.secretKey = secretKey;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengovsg/formsg-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0-alpha.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/opengovsg/formsg-javascript-sdk.git"
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"test": "jest",
|
|
12
|
+
"test": "NODE_OPTIONS=\"--max-old-space-size=8192\" jest",
|
|
13
13
|
"test-ci": "jest --coverage",
|
|
14
14
|
"test-watch": "jest --watch",
|
|
15
15
|
"build": "tsc",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"author": "Open Government Products (FormSG)",
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"axios": "^
|
|
27
|
+
"axios": "^1.6.2",
|
|
28
28
|
"tweetnacl": "^1.0.3",
|
|
29
29
|
"tweetnacl-util": "^0.15.1"
|
|
30
30
|
},
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"@babel/cli": "^7.8.4",
|
|
33
33
|
"@babel/core": "^7.9.0",
|
|
34
34
|
"@babel/preset-env": "^7.9.0",
|
|
35
|
-
"@types/jest": "^
|
|
36
|
-
"@types/node": "^
|
|
35
|
+
"@types/jest": "^29.5.8",
|
|
36
|
+
"@types/node": "^18.18.9",
|
|
37
37
|
"@typescript-eslint/eslint-plugin": "^4.25.0",
|
|
38
38
|
"auto-changelog": "^2.4.0",
|
|
39
39
|
"coveralls": "^3.1.1",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"eslint-plugin-prettier": "^3.4.0",
|
|
44
44
|
"eslint-plugin-simple-import-sort": "^7.0.0",
|
|
45
45
|
"eslint-plugin-typesafe": "^0.5.2",
|
|
46
|
-
"jest": "^
|
|
47
|
-
"jest-mock-axios": "^4.
|
|
48
|
-
"ts-jest": "^
|
|
49
|
-
"typescript": "^
|
|
46
|
+
"jest": "^29.7.0",
|
|
47
|
+
"jest-mock-axios": "^4.7.3",
|
|
48
|
+
"ts-jest": "^29.1.1",
|
|
49
|
+
"typescript": "^4.9.5"
|
|
50
50
|
}
|
|
51
51
|
}
|