-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 0.2.0 - Add signing functions, end-to-end testing (#2)
- Loading branch information
1 parent
06c90ea
commit 274b783
Showing
11 changed files
with
252 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
sudo: required | ||
|
||
language: node_js | ||
node_js: '10' | ||
cache: npm | ||
|
||
notifications: | ||
email: | ||
recipients: | ||
- [email protected] | ||
on_success: always | ||
on_failure: always | ||
|
||
before_script: | ||
- npm install | ||
|
||
script: | ||
- npm test |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,14 @@ | ||
module.exports = { | ||
staging: { | ||
// staging must never contain secret keys | ||
publicKey: 'rjv41kYqZwcbe3r6ymMEEKQ+Vd+DPuogN+Gzq3lP2Og=', | ||
}, | ||
production: { | ||
// production must never contain secret keys | ||
publicKey: '3Tt8VduXsjjd4IrpdCd7BAkdZl/vUCstu9UvTX84FWw=', | ||
}, | ||
test: { | ||
publicKey: 'KUY1XT30ar+XreVjsS1w/c3EpDs2oASbF6G3evvaUJM=', | ||
secretKey: '/u+LP57Ib9y5Ytpud56FzuitSC9O6lJ4EOLOFHpsHlYpRjVdPfRqv5et5WOxLXD9zcSkOzagBJsXobd6+9pQkw==', | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
describe('FormSG SDK', () => { | ||
it('should be able to initialise without arguments', () => { | ||
expect(() => require('../index')()).not.toThrow() | ||
}) | ||
}) |
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,11 @@ | ||
{ | ||
"spec_dir": "spec", | ||
"spec_files": [ | ||
"**/*[sS]pec.js" | ||
], | ||
"helpers": [ | ||
"helpers/**/*.js" | ||
], | ||
"stopSpecOnExpectationFailure": false, | ||
"random": true | ||
} |
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,39 @@ | ||
const webhookSecretKey = require('../resource/webhook-keys').test.secretKey | ||
const webhook = require('../src/webhooks')({ | ||
mode: 'test', | ||
webhookSecretKey | ||
}) | ||
|
||
describe('Webhooks', () => { | ||
const uri = 'https://some-endpoint.com/post' | ||
const submissionId = 'someSubmissionId' | ||
const formId = 'someFormId' | ||
|
||
it('should be signing the signature and generating the X-FormSG-Signature header with the correct format', () => { | ||
const epoch = 1583136171649 | ||
const signature = webhook.generateSignature({ uri, submissionId, formId, epoch }) | ||
|
||
expect(signature).toBe('KMirkrGJLPqu+Na+gdZLUxl9ZDgf2PnNGPnSoG1FuTMRUTiQ6o0jB/GTj1XFjn2s9JtsL5GiCmYROpjJhDyxCw==') | ||
|
||
// X-FormSG-Signature | ||
const header = webhook.constructHeader({ epoch, submissionId, formId, signature }) | ||
|
||
expect(header).toBe(`t=1583136171649,s=someSubmissionId,f=someFormId,v1=KMirkrGJLPqu+Na+gdZLUxl9ZDgf2PnNGPnSoG1FuTMRUTiQ6o0jB/GTj1XFjn2s9JtsL5GiCmYROpjJhDyxCw==`) | ||
}) | ||
|
||
it('should authenticate a signature that was recently generated', () => { | ||
const epoch = Date.now() | ||
const signature = webhook.generateSignature({ uri, submissionId, formId, epoch }) | ||
const header = webhook.constructHeader({ epoch, submissionId, formId, signature }) | ||
|
||
webhook.authenticate(header, uri) | ||
}) | ||
|
||
it('should reject signatures generated more than 5 minutes ago', () => { | ||
const epoch = Date.now() - 5 * 60 * 1000 - 1 | ||
const signature = webhook.generateSignature({ uri, submissionId, formId, epoch }) | ||
const header = webhook.constructHeader({ epoch, submissionId, formId, signature }) | ||
|
||
expect(() => webhook.authenticate(header, uri)).toThrow() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = { | ||
staging: 'staging', | ||
production: 'production', | ||
test: 'test' | ||
} |
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