@hookflo/tern 1.0.3 → 1.0.5

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/README.md CHANGED
@@ -109,6 +109,67 @@ const result = await WebhookVerificationService.verify(request, stripeConfig);
109
109
  - **Polar**: HMAC-SHA256
110
110
  - **Supabase**: Token-based authentication
111
111
 
112
+ ## Custom Platform Configuration
113
+
114
+ This framework is fully configuration-driven. You can verify webhooks from any provider—even if it is not built-in—by supplying a custom configuration object. This allows you to support new or proprietary platforms instantly, without waiting for a library update.
115
+
116
+ ### Example: Standard HMAC-SHA256 Webhook
117
+
118
+ ```typescript
119
+ import { WebhookVerificationService } from '@hookflo/tern';
120
+
121
+ const acmeConfig = {
122
+ platform: 'acmepay',
123
+ secret: 'acme_secret',
124
+ signatureConfig: {
125
+ algorithm: 'hmac-sha256',
126
+ headerName: 'x-acme-signature',
127
+ headerFormat: 'raw',
128
+ timestampHeader: 'x-acme-timestamp',
129
+ timestampFormat: 'unix',
130
+ payloadFormat: 'timestamped', // signs as {timestamp}.{body}
131
+ }
132
+ };
133
+
134
+ const result = await WebhookVerificationService.verify(request, acmeConfig);
135
+ ```
136
+
137
+ ### Example: Svix/Standard Webhooks (Clerk, Dodo Payments, etc.)
138
+
139
+ ```typescript
140
+ const svixConfig = {
141
+ platform: 'my-svix-platform',
142
+ secret: 'whsec_abc123...',
143
+ signatureConfig: {
144
+ algorithm: 'hmac-sha256',
145
+ headerName: 'webhook-signature',
146
+ headerFormat: 'raw',
147
+ timestampHeader: 'webhook-timestamp',
148
+ timestampFormat: 'unix',
149
+ payloadFormat: 'custom',
150
+ customConfig: {
151
+ payloadFormat: '{id}.{timestamp}.{body}',
152
+ idHeader: 'webhook-id',
153
+ // encoding: 'base64' // only if the provider uses base64, otherwise omit
154
+ }
155
+ }
156
+ };
157
+
158
+ const result = await WebhookVerificationService.verify(request, svixConfig);
159
+ ```
160
+
161
+ You can configure any combination of algorithm, header, payload, and encoding. See the `SignatureConfig` type for all options.
162
+
163
+ ## Webhook Verification OK Tested Platforms
164
+ - **Stripe**
165
+ - **Supabase**
166
+ - **Github**
167
+ - **Clerk**
168
+ - **Dodo Payments**
169
+
170
+ - **Other Platforms** : Yet to verify....
171
+
172
+
112
173
  ## Custom Configurations
113
174
 
114
175
  ### Custom HMAC-SHA256
@@ -301,4 +362,4 @@ MIT License - see [LICENSE](./LICENSE) for details.
301
362
 
302
363
  - [Documentation](./USAGE.md)
303
364
  - [Framework Summary](./FRAMEWORK_SUMMARY.md)
304
- - [Issues](https://github.com/your-repo/tern/issues)
365
+ - [Issues](https://github.com/Hookflo/tern/issues)
@@ -5,9 +5,7 @@ exports.getPlatformAlgorithmConfig = getPlatformAlgorithmConfig;
5
5
  exports.platformUsesAlgorithm = platformUsesAlgorithm;
6
6
  exports.getPlatformsUsingAlgorithm = getPlatformsUsingAlgorithm;
7
7
  exports.validateSignatureConfig = validateSignatureConfig;
8
- // Platform to algorithm mapping configuration
9
8
  exports.platformAlgorithmConfigs = {
10
- // GitHub uses HMAC-SHA256 with prefixed signature
11
9
  github: {
12
10
  platform: "github",
13
11
  signatureConfig: {
@@ -15,19 +13,18 @@ exports.platformAlgorithmConfigs = {
15
13
  headerName: "x-hub-signature-256",
16
14
  headerFormat: "prefixed",
17
15
  prefix: "sha256=",
18
- timestampHeader: undefined, // GitHub doesn't use timestamp validation
16
+ timestampHeader: undefined,
19
17
  payloadFormat: "raw",
20
18
  },
21
19
  description: "GitHub webhooks use HMAC-SHA256 with sha256= prefix",
22
20
  },
23
- // Stripe uses HMAC-SHA256 with comma-separated format
24
21
  stripe: {
25
22
  platform: "stripe",
26
23
  signatureConfig: {
27
24
  algorithm: "hmac-sha256",
28
25
  headerName: "stripe-signature",
29
26
  headerFormat: "comma-separated",
30
- timestampHeader: undefined, // Timestamp is embedded in signature
27
+ timestampHeader: undefined,
31
28
  payloadFormat: "timestamped",
32
29
  customConfig: {
33
30
  signatureFormat: "t={timestamp},v1={signature}",
@@ -35,7 +32,6 @@ exports.platformAlgorithmConfigs = {
35
32
  },
36
33
  description: "Stripe webhooks use HMAC-SHA256 with comma-separated format",
37
34
  },
38
- // Clerk uses HMAC-SHA256 with custom base64 encoding
39
35
  clerk: {
40
36
  platform: "clerk",
41
37
  signatureConfig: {
@@ -54,7 +50,6 @@ exports.platformAlgorithmConfigs = {
54
50
  },
55
51
  description: "Clerk webhooks use HMAC-SHA256 with base64 encoding",
56
52
  },
57
- // Dodo Payments uses HMAC-SHA256
58
53
  dodopayments: {
59
54
  platform: "dodopayments",
60
55
  signatureConfig: {
@@ -63,11 +58,15 @@ exports.platformAlgorithmConfigs = {
63
58
  headerFormat: "raw",
64
59
  timestampHeader: "webhook-timestamp",
65
60
  timestampFormat: "unix",
66
- payloadFormat: "raw",
61
+ payloadFormat: "custom",
62
+ customConfig: {
63
+ signatureFormat: "v1={signature}",
64
+ payloadFormat: "{id}.{timestamp}.{body}",
65
+ idHeader: "webhook-id",
66
+ },
67
67
  },
68
- description: "Dodo Payments webhooks use HMAC-SHA256",
68
+ description: "Dodo Payments webhooks use HMAC-SHA256 with svix-style format (Standard Webhooks)",
69
69
  },
70
- // Shopify uses HMAC-SHA256
71
70
  shopify: {
72
71
  platform: "shopify",
73
72
  signatureConfig: {
@@ -79,7 +78,6 @@ exports.platformAlgorithmConfigs = {
79
78
  },
80
79
  description: "Shopify webhooks use HMAC-SHA256",
81
80
  },
82
- // Vercel uses HMAC-SHA256
83
81
  vercel: {
84
82
  platform: "vercel",
85
83
  signatureConfig: {
@@ -92,7 +90,6 @@ exports.platformAlgorithmConfigs = {
92
90
  },
93
91
  description: "Vercel webhooks use HMAC-SHA256",
94
92
  },
95
- // Polar uses HMAC-SHA256
96
93
  polar: {
97
94
  platform: "polar",
98
95
  signatureConfig: {
@@ -105,7 +102,6 @@ exports.platformAlgorithmConfigs = {
105
102
  },
106
103
  description: "Polar webhooks use HMAC-SHA256",
107
104
  },
108
- // Supabase uses simple token-based authentication
109
105
  supabase: {
110
106
  platform: "supabase",
111
107
  signatureConfig: {
@@ -120,7 +116,6 @@ exports.platformAlgorithmConfigs = {
120
116
  },
121
117
  description: "Supabase webhooks use token-based authentication",
122
118
  },
123
- // Custom platform - can be configured per instance
124
119
  custom: {
125
120
  platform: "custom",
126
121
  signatureConfig: {
@@ -135,7 +130,6 @@ exports.platformAlgorithmConfigs = {
135
130
  },
136
131
  description: "Custom webhook configuration",
137
132
  },
138
- // Unknown platform - fallback
139
133
  unknown: {
140
134
  platform: "unknown",
141
135
  signatureConfig: {
@@ -147,37 +141,32 @@ exports.platformAlgorithmConfigs = {
147
141
  description: "Unknown platform - using default HMAC-SHA256",
148
142
  },
149
143
  };
150
- // Helper function to get algorithm config for a platform
151
144
  function getPlatformAlgorithmConfig(platform) {
152
145
  return exports.platformAlgorithmConfigs[platform] || exports.platformAlgorithmConfigs.unknown;
153
146
  }
154
- // Helper function to check if a platform uses a specific algorithm
155
147
  function platformUsesAlgorithm(platform, algorithm) {
156
148
  const config = getPlatformAlgorithmConfig(platform);
157
149
  return config.signatureConfig.algorithm === algorithm;
158
150
  }
159
- // Helper function to get all platforms using a specific algorithm
160
151
  function getPlatformsUsingAlgorithm(algorithm) {
161
152
  return Object.entries(exports.platformAlgorithmConfigs)
162
153
  .filter(([_, config]) => config.signatureConfig.algorithm === algorithm)
163
154
  .map(([platform, _]) => platform);
164
155
  }
165
- // Helper function to validate signature config
166
156
  function validateSignatureConfig(config) {
167
157
  if (!config.algorithm || !config.headerName) {
168
158
  return false;
169
159
  }
170
- // Validate algorithm-specific requirements
171
160
  switch (config.algorithm) {
172
161
  case "hmac-sha256":
173
162
  case "hmac-sha1":
174
163
  case "hmac-sha512":
175
- return true; // These algorithms only need headerName
164
+ return true;
176
165
  case "rsa-sha256":
177
166
  case "ed25519":
178
- return !!config.customConfig?.publicKey; // These need public key
167
+ return !!config.customConfig?.publicKey;
179
168
  case "custom":
180
- return !!config.customConfig; // Custom needs custom config
169
+ return !!config.customConfig;
181
170
  default:
182
171
  return false;
183
172
  }
package/dist/test.js CHANGED
@@ -3,10 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runTests = runTests;
4
4
  const crypto_1 = require("crypto");
5
5
  const index_1 = require("./index");
6
- // Test data
7
6
  const testSecret = 'whsec_test_secret_key_12345';
8
7
  const testBody = JSON.stringify({ event: 'test', data: { id: '123' } });
9
- // Helper function to create a mock request
10
8
  function createMockRequest(headers, body = testBody) {
11
9
  return new Request('https://example.com/webhook', {
12
10
  method: 'POST',
@@ -14,7 +12,6 @@ function createMockRequest(headers, body = testBody) {
14
12
  body,
15
13
  });
16
14
  }
17
- // Helper function to create Stripe signature
18
15
  function createStripeSignature(body, secret, timestamp) {
19
16
  const signedPayload = `${timestamp}.${body}`;
20
17
  const hmac = (0, crypto_1.createHmac)('sha256', secret);
@@ -22,13 +19,11 @@ function createStripeSignature(body, secret, timestamp) {
22
19
  const signature = hmac.digest('hex');
23
20
  return `t=${timestamp},v1=${signature}`;
24
21
  }
25
- // Helper function to create GitHub signature
26
22
  function createGitHubSignature(body, secret) {
27
23
  const hmac = (0, crypto_1.createHmac)('sha256', secret);
28
24
  hmac.update(body);
29
25
  return `sha256=${hmac.digest('hex')}`;
30
26
  }
31
- // Helper function to create Clerk signature
32
27
  function createClerkSignature(body, secret, id, timestamp) {
33
28
  const signedContent = `${id}.${timestamp}.${body}`;
34
29
  const secretBytes = new Uint8Array(Buffer.from(secret.split('_')[1], 'base64'));
@@ -118,6 +113,31 @@ async function runTests() {
118
113
  catch (error) {
119
114
  console.log(' ❌ Generic test failed:', error);
120
115
  }
116
+ // Test 4.5: Dodo Payments (Standard Webhooks / svix-style)
117
+ console.log('\n4.5. Testing Dodo Payments...');
118
+ try {
119
+ const webhookId = 'test-webhook-id-123';
120
+ const timestamp = Math.floor(Date.now() / 1000);
121
+ // Create svix-style signature: {webhook-id}.{webhook-timestamp}.{payload}
122
+ const signedContent = `${webhookId}.${timestamp}.${testBody}`;
123
+ const hmac = (0, crypto_1.createHmac)('sha256', testSecret);
124
+ hmac.update(signedContent);
125
+ const signature = hmac.digest('hex');
126
+ const dodoRequest = createMockRequest({
127
+ 'webhook-signature': signature,
128
+ 'webhook-id': webhookId,
129
+ 'webhook-timestamp': timestamp.toString(),
130
+ 'content-type': 'application/json',
131
+ });
132
+ const dodoResult = await index_1.WebhookVerificationService.verifyWithPlatformConfig(dodoRequest, 'dodopayments', testSecret);
133
+ console.log(' ✅ Dodo Payments:', dodoResult.isValid ? 'PASSED' : 'FAILED');
134
+ if (!dodoResult.isValid) {
135
+ console.log(' ❌ Error:', dodoResult.error);
136
+ }
137
+ }
138
+ catch (error) {
139
+ console.log(' ❌ Dodo Payments test failed:', error);
140
+ }
121
141
  // Test 5: Token-based (Supabase)
122
142
  console.log('\n5. Testing Token-based Authentication...');
123
143
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hookflo/tern",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A robust, scalable webhook verification framework supporting multiple platforms and signature algorithms",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",