-
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { generateSignature } from './signature.js'; | ||
|
||
describe('generateSignature()', () => { | ||
it('should generate correct signature', () => { | ||
const signingKey = 'foo'; | ||
const payload = { | ||
foo: 'foo', | ||
bar: 'bar', | ||
}; | ||
|
||
const signature = generateSignature(signingKey, payload); | ||
|
||
expect(signature).toBe( | ||
'sha256=436958f1dbfefab37712fb3927760490fbf7757da8c0b2306ee7b485f0360eee' | ||
); | ||
}); | ||
|
||
it('should generate correct signature if payload is empty', () => { | ||
const signingKey = 'foo'; | ||
const payload = {}; | ||
const signature = generateSignature(signingKey, payload); | ||
|
||
expect(signature).toBe( | ||
'sha256=c76356efa19d219d1d7e08ccb20b1d26db53b143156f406c99dcb8e0876d6c55' | ||
); | ||
}); | ||
|
||
it('should generate the same signature if payload contents are the same but payload JSON strings are not the same', () => { | ||
const signingKey = 'foo'; | ||
|
||
const payload = { | ||
foo: 'foo', | ||
bar: { | ||
baz: 'baz', | ||
}, | ||
}; | ||
|
||
const disorderedPayload = { | ||
bar: { | ||
baz: 'baz', | ||
}, | ||
foo: 'foo', | ||
}; | ||
|
||
const signature = generateSignature(signingKey, payload); | ||
const signatureByDisorderedPayload = generateSignature(signingKey, disorderedPayload); | ||
|
||
expect(JSON.stringify(payload)).not.toEqual(JSON.stringify(disorderedPayload)); | ||
expect(signature).toEqual(signatureByDisorderedPayload); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createHmac } from 'node:crypto'; | ||
|
||
import { canonicalize } from 'json-canonicalize'; | ||
|
||
export const generateSignature = (signingKey: string, payload: Record<string, unknown>) => { | ||
const hmac = createHmac('sha256', signingKey); | ||
const payloadString = canonicalize(payload); | ||
hmac.update(payloadString); | ||
return `sha256=${hmac.digest('hex')}`; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.