Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix signature verification for Tweetnacl flow #10

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.node*.js
node_modules
8 changes: 4 additions & 4 deletions examples/webhook-signing/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ router.post('/webhooks', addRawBody, function(request, response) {
// Try adding the Event as `request.event`
event = telnyx.webhooks.constructEvent(
request.rawBody,
request.headers['telnyx-signature'],
request.headers['telnyx-timestamp'],
request.header('telnyx-signature-ed25519'),
request.header('telnyx-timestamp'),
publicKey
);
} catch (e) {
Expand All @@ -61,10 +61,10 @@ router.post('/webhooks', addRawBody, function(request, response) {
return response.status(400).send('Webhook Error:' + e.message);
}

console.log('Success', event.id);
console.log('Success', event.data.id);

// Event was 'constructed', so we can respond with a 200 OK
response.status(200).send('Signed Webhook Received: ' + event.id);
response.status(200).send('Signed Webhook Received: ' + event.data.id);
});

// You could either create this app, or just return the `Router` for use in an
Expand Down
4 changes: 2 additions & 2 deletions examples/webhook-signing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": { },
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"morgan": "^1.8.1",
"telnyx": "^1.0.0"
"telnyx": "^1.2.1"
}
}
7 changes: 5 additions & 2 deletions lib/Webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ var signature = {

var payloadBuffer = Buffer.from(`${timestampHeader}|${payload}`, 'utf8');

var keyPair = nacl.sign.keyPair.fromSeed(Buffer.from(publicKey, 'base64'));
var verification;

try {
verification = nacl.sign.detached.verify(payloadBuffer, signatureHeader, keyPair.publicKey);
verification = nacl.sign.detached.verify(
payloadBuffer,
Buffer.from(signatureHeader, 'base64'),
Buffer.from(publicKey, 'base64')
);
} catch (err) {
throwSignatureVerificationError(payload, signatureHeader, timestampHeader);
}
Expand Down
5 changes: 3 additions & 2 deletions test/Webhook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var EVENT_PAYLOAD = {
};
var EVENT_PAYLOAD_STRING = JSON.stringify(EVENT_PAYLOAD, null, 2);

var PUBLIC_KEY = crypto.randomBytes(32);
var KEY_PAIR = nacl.sign.keyPair.fromSeed(crypto.randomBytes(32));
var PUBLIC_KEY = KEY_PAIR.publicKey;

describe('Webhooks', function() {
describe('.constructEvent', function() {
Expand Down Expand Up @@ -144,5 +145,5 @@ function generateSignature(opts) {

var payload = Buffer.from(`${opts.timestamp}|${opts.payload}`, 'utf8');

return nacl.sign.detached(payload, nacl.sign.keyPair.fromSeed(Buffer.from(PUBLIC_KEY, 'base64')).secretKey);
return nacl.sign.detached(payload, KEY_PAIR.secretKey);
}