5
5
// @ts -ignore
6
6
// eslint-disable-next-line import/no-unresolved
7
7
} from "../schema" ;
8
- import crypto from "crypto" ;
9
8
10
9
/* -------------------------------------------------------------------------------------------------
11
10
* Client
@@ -31,7 +30,7 @@ type WorkflowSchemaMap = {
31
30
export const authenticateWebhook = async <
32
31
Key extends `${WorkflowKeys } :${string } `,
33
32
> ( {
34
- secret,
33
+ secret : _secret ,
35
34
body,
36
35
signature,
37
36
} : {
@@ -65,6 +64,14 @@ export const authenticateWebhook = async <
65
64
return { success : false , error : "Signature is required" } ;
66
65
}
67
66
67
+ let secret : string | undefined = _secret ;
68
+ if ( _secret . startsWith ( "bshb_workflow" ) ) {
69
+ secret = _secret . split ( ":" ) [ 1 ] ;
70
+ }
71
+ if ( typeof secret !== "string" ) {
72
+ return { success : false , error : "Invalid secret" } ;
73
+ }
74
+
68
75
// Handle different body types
69
76
let rawBody : string ;
70
77
let parsedBody : unknown ;
@@ -131,12 +138,14 @@ export const authenticateWebhook = async <
131
138
payload : parsedBody as WorkflowSchemaMap [ ExtractWorkflowKey < Key > ] ,
132
139
} ;
133
140
} catch ( error ) {
141
+ let message =
142
+ error instanceof Error ? error . message : "Signature verification failed" ;
143
+ if ( message === "Unexpected end of JSON input" ) {
144
+ message = "Invalid body" ;
145
+ }
134
146
return {
135
147
success : false ,
136
- error :
137
- error instanceof Error
138
- ? error . message
139
- : "Signature verification failed" ,
148
+ error : message ,
140
149
} ;
141
150
}
142
151
} ;
0 commit comments