@fingerprint/node-sdk 7.0.0-test.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/.npmignore +9 -0
- package/LICENSE +19 -0
- package/dist/index.cjs +598 -0
- package/dist/index.d.ts +1872 -0
- package/dist/index.mjs +587 -0
- package/package.json +75 -0
- package/readme.md +191 -0
- package/src/errors/apiErrors.ts +51 -0
- package/src/errors/handleErrorResponse.ts +13 -0
- package/src/errors/toError.ts +7 -0
- package/src/errors/unsealError.ts +32 -0
- package/src/generatedApiTypes.ts +1517 -0
- package/src/index.ts +16 -0
- package/src/sealedResults.ts +94 -0
- package/src/serverApiClient.ts +321 -0
- package/src/types.ts +90 -0
- package/src/urlUtils.ts +171 -0
- package/src/webhook.ts +67 -0
package/src/webhook.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import crypto from 'crypto'
|
|
2
|
+
|
|
3
|
+
function isValidHmacSignature(signature: string, data: Buffer, secret: string) {
|
|
4
|
+
return signature === crypto.createHmac('sha256', secret).update(data).digest('hex')
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface IsValidWebhookSignatureParams {
|
|
8
|
+
/**
|
|
9
|
+
* The value of the "fpjs-event-signature" header.
|
|
10
|
+
* */
|
|
11
|
+
header: string
|
|
12
|
+
/**
|
|
13
|
+
* The raw data of the incoming request
|
|
14
|
+
* */
|
|
15
|
+
data: Buffer
|
|
16
|
+
/**
|
|
17
|
+
* The secret key used to sign the request.
|
|
18
|
+
* */
|
|
19
|
+
secret: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Verifies the HMAC signature extracted from the "fpjs-event-signature" header of the incoming request. This is a part of the webhook signing process, which is available only for enterprise customers.
|
|
24
|
+
* If you wish to enable it, please contact our support: https://fingerprint.com/support
|
|
25
|
+
*
|
|
26
|
+
* @param {IsValidWebhookSignatureParams} params
|
|
27
|
+
* @param {string} params.header - The value of the "fpjs-event-signature" header.
|
|
28
|
+
* @param {Buffer} params.data - The raw data of the incoming request.
|
|
29
|
+
* @param {string} params.secret - The secret key used to sign the request.
|
|
30
|
+
*
|
|
31
|
+
* @return {boolean} true if the signature is valid, false otherwise.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```javascript
|
|
35
|
+
* // Webhook endpoint handler
|
|
36
|
+
* export async function POST(request: Request) {
|
|
37
|
+
* try {
|
|
38
|
+
* const secret = process.env.WEBHOOK_SIGNATURE_SECRET;
|
|
39
|
+
* const header = request.headers.get("fpjs-event-signature");
|
|
40
|
+
* const data = Buffer.from(await request.arrayBuffer());
|
|
41
|
+
*
|
|
42
|
+
* if (!isValidWebhookSignature({ header, data, secret })) {
|
|
43
|
+
* return Response.json(
|
|
44
|
+
* { message: "Webhook signature is invalid." },
|
|
45
|
+
* { status: 403 },
|
|
46
|
+
* );
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* return Response.json({ message: "Webhook received." });
|
|
50
|
+
* } catch (error) {
|
|
51
|
+
* return Response.json({ error }, { status: 500 });
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export function isValidWebhookSignature(params: IsValidWebhookSignatureParams): boolean {
|
|
57
|
+
const { header, data, secret } = params
|
|
58
|
+
|
|
59
|
+
const signatures = header.split(',')
|
|
60
|
+
for (const signature of signatures) {
|
|
61
|
+
const [version, hash] = signature.split('=')
|
|
62
|
+
if (version === 'v1' && isValidHmacSignature(hash, data, secret)) {
|
|
63
|
+
return true
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return false
|
|
67
|
+
}
|