Skip to content

Commit

Permalink
Merge 842725c into a3ab9b1
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Sep 9, 2019
2 parents a3ab9b1 + 842725c commit 482b952
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
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);
}

0 comments on commit 482b952

Please sign in to comment.