1- const { createSignatureVerifier } = require ( '../dist/index.js' ) ;
1+ const { createSignatureVerifier, createAsyncSignatureVerifier } = require ( '../dist/index.js' ) ;
22var crypto = require ( 'crypto' ) ;
33
4- describe ( 'Test Signature HMAC' , ( ) => {
4+ describe ( 'Test Signature HMAC with Deprecated Sync Function ' , ( ) => {
55 test ( 'Test With a Body' , ( ) => {
66 const guard = createSignatureVerifier ( {
77 secret : 'xXx' ,
@@ -11,13 +11,12 @@ describe('Test Signature HMAC', () => {
1111 } ) ,
1212 } ) ;
1313
14- expect (
15- guard ( 'xXx.xXx.xXx' , {
16- url : 'https://a17e-2601-645-4500-330-b07d-351d-ece7-41c1.ngrok.io/test/signature' ,
17- method : 'POST' ,
18- body : '{"item":{"get":{"id":"63f2d3b2a94533f79fc6397b","createdAt":"2023-02-20T01:58:10.000Z","updatedAt":"2023-02-23T07:58:34.685Z","name":"test"}}}' ,
19- } ) ,
20- ) ;
14+ const payload = guard ( 'xXx.xXx.xXx' , {
15+ url : 'https://a17e-2601-645-4500-330-b07d-351d-ece7-41c1.ngrok.io/test/signature' ,
16+ method : 'POST' ,
17+ body : '{"item":{"get":{"id":"63f2d3b2a94533f79fc6397b","createdAt":"2023-02-20T01:58:10.000Z","updatedAt":"2023-02-23T07:58:34.685Z","name":"test"}}}' ,
18+ } ) ;
19+ expect ( payload . hmac ) . toBe ( '1101b34dac8c55e5590a37271f1c41c3d745463854613494a1624a15be24f1f8' ) ;
2120 } ) ;
2221
2322 test ( 'Test Without a Body App' , ( ) => {
@@ -28,14 +27,12 @@ describe('Test Signature HMAC', () => {
2827 hmac : '157bee342a4856e14e964356fef54fd84b3a3508c1071ed674172d3f9b68892f' ,
2928 } ) ,
3029 } ) ;
31-
32- expect (
33- guard ( 'xXx.xXx.xXx' , {
34- url : 'https://helloworld.crystallize.app.local' ,
35- method : 'GET' ,
36- body : null ,
37- } ) ,
38- ) ;
30+ const payload = guard ( 'xXx.xXx.xXx' , {
31+ url : 'https://helloworld.crystallize.app.local' ,
32+ method : 'GET' ,
33+ body : null ,
34+ } ) ;
35+ expect ( payload . hmac ) . toBe ( '157bee342a4856e14e964356fef54fd84b3a3508c1071ed674172d3f9b68892f' ) ;
3936 } ) ;
4037
4138 test ( 'Test Without a Body Webhook' , ( ) => {
@@ -46,12 +43,66 @@ describe('Test Signature HMAC', () => {
4643 hmac : '61ce7a2e5072900a13369ac7f69b9e056e91c38c42f1bfe94389c80411d94b78' ,
4744 } ) ,
4845 } ) ;
49- expect (
50- guard ( 'xXx.xXx.xXx' , {
51- url : 'https://webhook.site/b56870a7-9600-41a6-86a0-98be0c7532fd?id=65d8fc4ce2ba75beec481ec1&userId=61f9933ec63b0a44d5004c2d&tenantId=61f9937c3b63c8386ea9e153&type=document&language=en' ,
52- webhookUrl : 'https://webhook.site/b56870a7-9600-41a6-86a0-98be0c7532fd' ,
53- method : 'GET' ,
46+
47+ const payload = guard ( 'xXx.xXx.xXx' , {
48+ url : 'https://webhook.site/b56870a7-9600-41a6-86a0-98be0c7532fd?id=65d8fc4ce2ba75beec481ec1&userId=61f9933ec63b0a44d5004c2d&tenantId=61f9937c3b63c8386ea9e153&type=document&language=en' ,
49+ webhookUrl : 'https://webhook.site/b56870a7-9600-41a6-86a0-98be0c7532fd' ,
50+ method : 'GET' ,
51+ } ) ;
52+
53+ expect ( payload . hmac ) . toBe ( '61ce7a2e5072900a13369ac7f69b9e056e91c38c42f1bfe94389c80411d94b78' ) ;
54+ } ) ;
55+ } ) ;
56+
57+ describe ( 'Test Signature HMAC with Async verifier and ASync Functions' , ( ) => {
58+ test ( 'Test With a Body' , async ( ) => {
59+ const guard = createAsyncSignatureVerifier ( {
60+ secret : 'xXx' ,
61+ sha256 : async ( data ) => crypto . createHash ( 'sha256' ) . update ( data ) . digest ( 'hex' ) ,
62+ jwtVerify : async ( token , secret ) => ( {
63+ hmac : '1101b34dac8c55e5590a37271f1c41c3d745463854613494a1624a15be24f1f8' ,
64+ } ) ,
65+ } ) ;
66+
67+ const payload = await guard ( 'xXx.xXx.xXx' , {
68+ url : 'https://a17e-2601-645-4500-330-b07d-351d-ece7-41c1.ngrok.io/test/signature' ,
69+ method : 'POST' ,
70+ body : '{"item":{"get":{"id":"63f2d3b2a94533f79fc6397b","createdAt":"2023-02-20T01:58:10.000Z","updatedAt":"2023-02-23T07:58:34.685Z","name":"test"}}}' ,
71+ } ) ;
72+ expect ( payload . hmac ) . toBe ( '1101b34dac8c55e5590a37271f1c41c3d745463854613494a1624a15be24f1f8' ) ;
73+ } ) ;
74+
75+ test ( 'Test Without a Body App' , async ( ) => {
76+ const guard = createAsyncSignatureVerifier ( {
77+ secret : 'xXx' ,
78+ sha256 : async ( data ) => crypto . createHash ( 'sha256' ) . update ( data ) . digest ( 'hex' ) ,
79+ jwtVerify : async ( token , secret ) => ( {
80+ hmac : '157bee342a4856e14e964356fef54fd84b3a3508c1071ed674172d3f9b68892f' ,
81+ } ) ,
82+ } ) ;
83+ const payload = await guard ( 'xXx.xXx.xXx' , {
84+ url : 'https://helloworld.crystallize.app.local' ,
85+ method : 'GET' ,
86+ body : null ,
87+ } ) ;
88+ expect ( payload . hmac ) . toBe ( '157bee342a4856e14e964356fef54fd84b3a3508c1071ed674172d3f9b68892f' ) ;
89+ } ) ;
90+
91+ test ( 'Test Without a Body Webhook' , async ( ) => {
92+ const guard = createAsyncSignatureVerifier ( {
93+ secret : 'xXx' ,
94+ sha256 : async ( data ) => crypto . createHash ( 'sha256' ) . update ( data ) . digest ( 'hex' ) ,
95+ jwtVerify : async ( token , secret ) => ( {
96+ hmac : '61ce7a2e5072900a13369ac7f69b9e056e91c38c42f1bfe94389c80411d94b78' ,
5497 } ) ,
55- ) ;
98+ } ) ;
99+
100+ const payload = await guard ( 'xXx.xXx.xXx' , {
101+ url : 'https://webhook.site/b56870a7-9600-41a6-86a0-98be0c7532fd?id=65d8fc4ce2ba75beec481ec1&userId=61f9933ec63b0a44d5004c2d&tenantId=61f9937c3b63c8386ea9e153&type=document&language=en' ,
102+ webhookUrl : 'https://webhook.site/b56870a7-9600-41a6-86a0-98be0c7532fd' ,
103+ method : 'GET' ,
104+ } ) ;
105+
106+ expect ( payload . hmac ) . toBe ( '61ce7a2e5072900a13369ac7f69b9e056e91c38c42f1bfe94389c80411d94b78' ) ;
56107 } ) ;
57108} ) ;
0 commit comments