payment-webhook-verify 0.0.0-stage → 0.1.0
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/index.d.ts +16 -0
- package/index.js +68 -0
- package/package.json +13 -4
- package/README.md +0 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function verifySignature(
|
|
2
|
+
rawBody: string | Buffer,
|
|
3
|
+
signature: string,
|
|
4
|
+
secret: string,
|
|
5
|
+
options?: {
|
|
6
|
+
encoding?: 'hex' | 'base64';
|
|
7
|
+
algorithm?: string;
|
|
8
|
+
}
|
|
9
|
+
): boolean;
|
|
10
|
+
|
|
11
|
+
export function verifyStripeStyle(
|
|
12
|
+
rawBody: string | Buffer,
|
|
13
|
+
signatureHeader: string,
|
|
14
|
+
secret: string,
|
|
15
|
+
toleranceSeconds?: number
|
|
16
|
+
): boolean;
|
package/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Verify webhook signature (generic HMAC version).
|
|
5
|
+
* @param {string|Buffer} rawBody - Raw request body, must NOT be parsed JSON.
|
|
6
|
+
* @param {string} signature - Signature value from the request header.
|
|
7
|
+
* @param {string} secret - Shared secret key.
|
|
8
|
+
* @param {object} [options]
|
|
9
|
+
* @param {string} [options.encoding='hex'] - 'hex' or 'base64'
|
|
10
|
+
* @param {string} [options.algorithm='sha256']
|
|
11
|
+
* @returns {boolean}
|
|
12
|
+
*/
|
|
13
|
+
function verifySignature(rawBody, signature, secret, options = {}) {
|
|
14
|
+
const { encoding = 'hex', algorithm = 'sha256' } = options;
|
|
15
|
+
|
|
16
|
+
if (!rawBody || !signature || !secret) {
|
|
17
|
+
throw new Error('Missing required parameters: rawBody, signature, secret');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const bodyBuffer = Buffer.isBuffer(rawBody)
|
|
21
|
+
? rawBody
|
|
22
|
+
: Buffer.from(rawBody, 'utf8');
|
|
23
|
+
|
|
24
|
+
const expected = crypto
|
|
25
|
+
.createHmac(algorithm, secret)
|
|
26
|
+
.update(bodyBuffer)
|
|
27
|
+
.digest(encoding);
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
return crypto.timingSafeEqual(
|
|
31
|
+
Buffer.from(expected, encoding),
|
|
32
|
+
Buffer.from(signature, encoding)
|
|
33
|
+
);
|
|
34
|
+
} catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Verify Stripe-style signature header: "t=timestamp,v1=signature".
|
|
41
|
+
* Signed payload is `${timestamp}.${rawBody}`.
|
|
42
|
+
*/
|
|
43
|
+
function verifyStripeStyle(rawBody, signatureHeader, secret, toleranceSeconds = 300) {
|
|
44
|
+
if (!signatureHeader) return false;
|
|
45
|
+
|
|
46
|
+
const parts = signatureHeader.split(',');
|
|
47
|
+
let timestamp = null;
|
|
48
|
+
let signature = null;
|
|
49
|
+
|
|
50
|
+
for (const part of parts) {
|
|
51
|
+
const [key, value] = part.split('=');
|
|
52
|
+
if (key === 't') timestamp = value;
|
|
53
|
+
if (key === 'v1') signature = value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!timestamp || !signature) return false;
|
|
57
|
+
|
|
58
|
+
const now = Math.floor(Date.now() / 1000);
|
|
59
|
+
const ts = parseInt(timestamp, 10);
|
|
60
|
+
if (Number.isNaN(ts) || Math.abs(now - ts) > toleranceSeconds) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const signedPayload = `${timestamp}.${rawBody}`;
|
|
65
|
+
return verifySignature(signedPayload, signature, secret, { encoding: 'hex' });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = { verifySignature, verifyStripeStyle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payment-webhook-verify",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Minimal webhook signature verification for payment providers",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": ["index.js", "index.d.ts"],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "node test.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["webhook", "payment", "signature", "hmac", "stripe"],
|
|
12
|
+
"author": "Your Name",
|
|
13
|
+
"license": "MIT"
|
|
14
|
+
}
|
|
15
|
+
|
package/README.md
DELETED