@hookflo/tern 1.0.4 → 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: {
@@ -67,13 +62,11 @@ exports.platformAlgorithmConfigs = {
67
62
  customConfig: {
68
63
  signatureFormat: "v1={signature}",
69
64
  payloadFormat: "{id}.{timestamp}.{body}",
70
- encoding: "base64",
71
65
  idHeader: "webhook-id",
72
66
  },
73
67
  },
74
- description: "Dodo Payments webhooks use HMAC-SHA256",
68
+ description: "Dodo Payments webhooks use HMAC-SHA256 with svix-style format (Standard Webhooks)",
75
69
  },
76
- // Shopify uses HMAC-SHA256
77
70
  shopify: {
78
71
  platform: "shopify",
79
72
  signatureConfig: {
@@ -85,7 +78,6 @@ exports.platformAlgorithmConfigs = {
85
78
  },
86
79
  description: "Shopify webhooks use HMAC-SHA256",
87
80
  },
88
- // Vercel uses HMAC-SHA256
89
81
  vercel: {
90
82
  platform: "vercel",
91
83
  signatureConfig: {
@@ -98,7 +90,6 @@ exports.platformAlgorithmConfigs = {
98
90
  },
99
91
  description: "Vercel webhooks use HMAC-SHA256",
100
92
  },
101
- // Polar uses HMAC-SHA256
102
93
  polar: {
103
94
  platform: "polar",
104
95
  signatureConfig: {
@@ -111,7 +102,6 @@ exports.platformAlgorithmConfigs = {
111
102
  },
112
103
  description: "Polar webhooks use HMAC-SHA256",
113
104
  },
114
- // Supabase uses simple token-based authentication
115
105
  supabase: {
116
106
  platform: "supabase",
117
107
  signatureConfig: {
@@ -126,7 +116,6 @@ exports.platformAlgorithmConfigs = {
126
116
  },
127
117
  description: "Supabase webhooks use token-based authentication",
128
118
  },
129
- // Custom platform - can be configured per instance
130
119
  custom: {
131
120
  platform: "custom",
132
121
  signatureConfig: {
@@ -141,7 +130,6 @@ exports.platformAlgorithmConfigs = {
141
130
  },
142
131
  description: "Custom webhook configuration",
143
132
  },
144
- // Unknown platform - fallback
145
133
  unknown: {
146
134
  platform: "unknown",
147
135
  signatureConfig: {
@@ -153,37 +141,32 @@ exports.platformAlgorithmConfigs = {
153
141
  description: "Unknown platform - using default HMAC-SHA256",
154
142
  },
155
143
  };
156
- // Helper function to get algorithm config for a platform
157
144
  function getPlatformAlgorithmConfig(platform) {
158
145
  return exports.platformAlgorithmConfigs[platform] || exports.platformAlgorithmConfigs.unknown;
159
146
  }
160
- // Helper function to check if a platform uses a specific algorithm
161
147
  function platformUsesAlgorithm(platform, algorithm) {
162
148
  const config = getPlatformAlgorithmConfig(platform);
163
149
  return config.signatureConfig.algorithm === algorithm;
164
150
  }
165
- // Helper function to get all platforms using a specific algorithm
166
151
  function getPlatformsUsingAlgorithm(algorithm) {
167
152
  return Object.entries(exports.platformAlgorithmConfigs)
168
153
  .filter(([_, config]) => config.signatureConfig.algorithm === algorithm)
169
154
  .map(([platform, _]) => platform);
170
155
  }
171
- // Helper function to validate signature config
172
156
  function validateSignatureConfig(config) {
173
157
  if (!config.algorithm || !config.headerName) {
174
158
  return false;
175
159
  }
176
- // Validate algorithm-specific requirements
177
160
  switch (config.algorithm) {
178
161
  case "hmac-sha256":
179
162
  case "hmac-sha1":
180
163
  case "hmac-sha512":
181
- return true; // These algorithms only need headerName
164
+ return true;
182
165
  case "rsa-sha256":
183
166
  case "ed25519":
184
- return !!config.customConfig?.publicKey; // These need public key
167
+ return !!config.customConfig?.publicKey;
185
168
  case "custom":
186
- return !!config.customConfig; // Custom needs custom config
169
+ return !!config.customConfig;
187
170
  default:
188
171
  return false;
189
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.4",
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",