@trv-hq/trv-paystack-utils 0.0.0-stage → 1.0.4
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/.idea/modules.xml +8 -0
- package/.idea/trv-paystack-utils.iml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +1 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/src/index.js +2 -0
- package/package.json +31 -4
- package/src/index.ts +55 -0
- package/tsconfig.json +44 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/trv-paystack-utils.iml" filepath="$PROJECT_DIR$/.idea/trv-paystack-utils.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="WEB_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$" />
|
|
5
|
+
<orderEntry type="inheritedJdk" />
|
|
6
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
7
|
+
</component>
|
|
8
|
+
</module>
|
package/.idea/vcs.xml
ADDED
package/README.md
CHANGED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA0CA,wBAAsB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,oBAYlG"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
const TOLERANCE_SECONDS = 300;
|
|
3
|
+
// Returns the key id (kid) of the signature that matched, or null.
|
|
4
|
+
function verifyPaystackSignature(rawBody, header, secrets, now = Math.floor(Date.now() / 1000)) {
|
|
5
|
+
if (!Buffer.isBuffer(rawBody) || typeof header !== 'string')
|
|
6
|
+
return null;
|
|
7
|
+
let t = null;
|
|
8
|
+
const entries = [];
|
|
9
|
+
for (const part of header.split(',')) {
|
|
10
|
+
const i = part.indexOf('=');
|
|
11
|
+
if (i === -1)
|
|
12
|
+
continue;
|
|
13
|
+
const key = part.slice(0, i).trim();
|
|
14
|
+
const value = part.slice(i + 1).trim();
|
|
15
|
+
if (key === 't') {
|
|
16
|
+
if (t !== null || !/^[0-9]+$/.test(value))
|
|
17
|
+
return null;
|
|
18
|
+
t = Number(value);
|
|
19
|
+
}
|
|
20
|
+
else if (key === 'v1') {
|
|
21
|
+
const j = value.indexOf(':');
|
|
22
|
+
if (j !== -1)
|
|
23
|
+
entries.push({ kid: value.slice(0, j), hex: value.slice(j + 1) });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (t === null || entries.length === 0)
|
|
27
|
+
return null;
|
|
28
|
+
if (Math.abs(now - t) > TOLERANCE_SECONDS)
|
|
29
|
+
return null;
|
|
30
|
+
for (const secret of secrets) {
|
|
31
|
+
const expected = crypto.createHmac('sha512', secret).update(t + '.').update(rawBody).digest();
|
|
32
|
+
for (const { kid, hex } of entries) {
|
|
33
|
+
if (!/^[0-9a-f]{128}$/.test(hex))
|
|
34
|
+
continue;
|
|
35
|
+
if (crypto.timingSafeEqual(expected, Buffer.from(hex, 'hex')))
|
|
36
|
+
return kid;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function verifyXPaystackSignature(rawBody, signature = '', secret) {
|
|
42
|
+
const hash = crypto.createHmac('sha512', secret).update(rawBody).digest();
|
|
43
|
+
return !(!/^[0-9a-f]{128}$/.test(signature) || !crypto.timingSafeEqual(hash, Buffer.from(signature, 'hex')));
|
|
44
|
+
}
|
|
45
|
+
export async function isPayloadValid(request, rawBody, secret) {
|
|
46
|
+
const paystackSignature = request.headers.get('paystack-signature');
|
|
47
|
+
const xpaystackSignature = request.headers.get('x-paystack-signature');
|
|
48
|
+
if (paystackSignature) {
|
|
49
|
+
return !!verifyPaystackSignature(rawBody, paystackSignature, [secret]);
|
|
50
|
+
}
|
|
51
|
+
else if (xpaystackSignature) {
|
|
52
|
+
return verifyXPaystackSignature(rawBody, xpaystackSignature, secret);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAE3B,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAE9B,mEAAmE;AACnE,SAAS,uBAAuB,CAAC,OAA4B,EAAE,MAAqB,EAAE,OAAiB,EAAE,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;IACxI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAEzE,IAAI,CAAC,GAAG,IAAI,CAAC;IACb,MAAM,OAAO,GAAG,EAAE,CAAC;IACnB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACnC,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,CAAC,KAAK,CAAC,CAAC;YAAE,SAAS;QACvB,MAAM,GAAG,GAAG,IAAI,CA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|
2
2
|
"name": "@trv-hq/trv-paystack-utils",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "paystack helpers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"paystack",
|
|
7
|
+
"webhook"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/shiro-zoro/trv-paystack-utils#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/shiro-zoro/trv-paystack-utils/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/shiro-zoro/trv-paystack-utils.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"author": "Uchenna Nwaorgu",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish --access public"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"undici-types": "^8.9.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^26.6.11",
|
|
31
|
+
"typescript": "^6.0.3"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import crypto from "crypto"
|
|
2
|
+
|
|
3
|
+
const TOLERANCE_SECONDS = 300;
|
|
4
|
+
|
|
5
|
+
// Returns the key id (kid) of the signature that matched, or null.
|
|
6
|
+
function verifyPaystackSignature(rawBody: Buffer<ArrayBuffer>, header: string | null, secrets: string[], now = Math.floor(Date.now() / 1000)) {
|
|
7
|
+
if (!Buffer.isBuffer(rawBody) || typeof header !== 'string') return null;
|
|
8
|
+
|
|
9
|
+
let t = null;
|
|
10
|
+
const entries = [];
|
|
11
|
+
for (const part of header.split(',')) {
|
|
12
|
+
const i = part.indexOf('=');
|
|
13
|
+
if (i === -1) continue;
|
|
14
|
+
const key = part.slice(0, i).trim();
|
|
15
|
+
const value = part.slice(i + 1).trim();
|
|
16
|
+
if (key === 't') {
|
|
17
|
+
if (t !== null || !/^[0-9]+$/.test(value)) return null;
|
|
18
|
+
t = Number(value);
|
|
19
|
+
} else if (key === 'v1') {
|
|
20
|
+
const j = value.indexOf(':');
|
|
21
|
+
if (j !== -1) entries.push({kid: value.slice(0, j), hex: value.slice(j + 1)});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (t === null || entries.length === 0) return null;
|
|
25
|
+
if (Math.abs(now - t) > TOLERANCE_SECONDS) return null;
|
|
26
|
+
|
|
27
|
+
for (const secret of secrets) {
|
|
28
|
+
const expected = crypto.createHmac('sha512', secret).update(t + '.').update(rawBody).digest();
|
|
29
|
+
for (const {kid, hex} of entries) {
|
|
30
|
+
if (!/^[0-9a-f]{128}$/.test(hex)) continue;
|
|
31
|
+
if (crypto.timingSafeEqual(expected, Buffer.from(hex, 'hex'))) return kid;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function verifyXPaystackSignature(rawBody: Buffer<ArrayBuffer>, signature: string = '', secret: string) {
|
|
38
|
+
const hash = crypto.createHmac('sha512', secret).update(rawBody).digest();
|
|
39
|
+
return !(!/^[0-9a-f]{128}$/.test(signature) || !crypto.timingSafeEqual(hash, Buffer.from(signature, 'hex')));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
export async function isPayloadValid(request: Request, rawBody: Buffer<ArrayBuffer>, secret: string) {
|
|
44
|
+
const paystackSignature = request.headers.get('paystack-signature')
|
|
45
|
+
const xpaystackSignature = request.headers.get('x-paystack-signature')
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if (paystackSignature) {
|
|
49
|
+
return !!verifyPaystackSignature(rawBody, paystackSignature, [secret])
|
|
50
|
+
} else if (xpaystackSignature) {
|
|
51
|
+
return verifyXPaystackSignature(rawBody, xpaystackSignature, secret)
|
|
52
|
+
} else {
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
"types": ["node"],
|
|
13
|
+
// For nodejs:
|
|
14
|
+
"lib": ["esnext"],
|
|
15
|
+
// "types": ["node"],
|
|
16
|
+
// and npm install -D @types/node
|
|
17
|
+
|
|
18
|
+
// Other Outputs
|
|
19
|
+
"sourceMap": true,
|
|
20
|
+
"declaration": true,
|
|
21
|
+
"declarationMap": true,
|
|
22
|
+
|
|
23
|
+
// Stricter Typechecking Options
|
|
24
|
+
"noUncheckedIndexedAccess": true,
|
|
25
|
+
"exactOptionalPropertyTypes": true,
|
|
26
|
+
|
|
27
|
+
// Style Options
|
|
28
|
+
// "noImplicitReturns": true,
|
|
29
|
+
// "noImplicitOverride": true,
|
|
30
|
+
// "noUnusedLocals": true,
|
|
31
|
+
// "noUnusedParameters": true,
|
|
32
|
+
// "noFallthroughCasesInSwitch": true,
|
|
33
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
34
|
+
|
|
35
|
+
// Recommended Options
|
|
36
|
+
"strict": true,
|
|
37
|
+
"jsx": "react-jsx",
|
|
38
|
+
"verbatimModuleSyntax": true,
|
|
39
|
+
"isolatedModules": true,
|
|
40
|
+
"noUncheckedSideEffectImports": true,
|
|
41
|
+
"moduleDetection": "force",
|
|
42
|
+
"skipLibCheck": true,
|
|
43
|
+
}
|
|
44
|
+
}
|