Skip to content

Commit

Permalink
Merge pull request #93 from messagebird/fix/typos-and-remove-sandbox
Browse files Browse the repository at this point in the history
fix: typos and remove sandbox
  • Loading branch information
aodinok authored Oct 6, 2021
2 parents 6b58dd9 + 1129435 commit 2bc9447
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 2,440 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Messaging and Voice API use different pagination semantics:

````javascript
// list conversations
//In this case 20 is limit and 0 is offset
// In this case 20 is limit and 0 is offset
messagebird.conversations.list(20, 0, function (err, response) {
if (err) {
return console.log(err);
Expand All @@ -108,19 +108,19 @@ Verifying Signatures

For each HTTP request that MessageBird sends, a `MessageBird-Signature-JWT` header is added.

The `MessageBird-Signature-JWT` header is a signature that consists of all the information that is required to verify the integrity of the request. The signature is generated from the request URL and request body and is signed with the HMAC-SHA256 algorithm using your your signing key. You can validate this signature using our SDKsto e nsure that the request is valid and unaltered. The token also includes timestamp claims that allow you to prove the time of the request, protecting from replay attacks and the like.
For more details consult the [documentation](https://developers.messagebird.com/api/#verifying-http-requests).
The `MessageBird-Signature-JWT` header is a signature that consists of all the information that is required to verify the integrity of the request. The signature is generated from the request URL and request body and is signed with the HMAC-SHA256 algorithm using your your signing key. You can validate this signature using our SDKsto ensure that the request is valid and unaltered. The token also includes timestamp claims that allow you to prove the time of the request, protecting from replay attacks and the like.
For more details consult the [documentation](https://developers.messagebird.com/api/#verifying-http-requests).

Examples:

- [full example with Express](./examples/webhook-signature-express-middleware.js)
- [example in vanilla JS](./examples/webhook-signature-http-node.js)


Let's use Express Signature middleware to verify webhooks.
```javascript
// This example show how to verify the authenticity of a MessageBird webhook.
const mbWebookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const mbWebhookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const express = require('express');
const secret = '<YOUR SIGNING KEY>';
Expand All @@ -132,7 +132,7 @@ const app = express();
app.set('trust proxy', () => true);
// Replace <YOUR_SIGNING_KEY> with your actual signing key.
const verifySignature = new mbWebookSignatureJwt.ExpressMiddlewareVerify(secret);
const verifySignature = new mbWebhookSignatureJwt.ExpressMiddlewareVerify(secret);
// Retrieve the raw body as a buffer.
app.use(express.raw({ 'type': '*/*' }));
Expand All @@ -146,9 +146,6 @@ app.post('/webhook', verifySignature, (req, res) => {
});
```
Documentation
-------------
Expand Down
12 changes: 6 additions & 6 deletions examples/webhook-signature-express-middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This example show how to verify the authenticity of a MessageBird webhook.
const mbWebookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const mbWebhookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const express = require('express');

const secret = '<YOUR SIGNING KEY>';
Expand All @@ -11,7 +11,7 @@ const app = express();
app.set('trust proxy', () => true);

// Replace <YOUR_SIGNING_KEY> with your actual signing key.
const verifySignature = new mbWebookSignatureJwt.ExpressMiddlewareVerify(secret);
const verifySignature = new mbWebhookSignatureJwt.ExpressMiddlewareVerify(secret);

// Retrieve the raw body as a buffer.
app.use(express.raw({ 'type': '*/*' }));
Expand All @@ -32,10 +32,10 @@ app.post('/webhook', verifySignature, (req, res) => {
// However it doesn't verify if the URL was altered or not.
//
// This shouldn't be used in a production system and when used no query parameters should be trusted.
const skipUrlOpts = new mbWebookSignatureJwt.VerifyOptions();
const skipUrlOpts = new mbWebhookSignatureJwt.VerifyOptions();

skipUrlOpts.validateUrl = false;
let skipUrlVerifySignature = new mbWebookSignatureJwt.ExpressMiddlewareVerify(secret, skipUrlOpts);
let skipUrlVerifySignature = new mbWebhookSignatureJwt.ExpressMiddlewareVerify(secret, skipUrlOpts);

app.get('/webhook-skip-url-verification', skipUrlVerifySignature, (req, res) => {
res.send('partialy verified');
Expand All @@ -46,7 +46,7 @@ app.get('/webhook-skip-url-verification', skipUrlVerifySignature, (req, res) =>
// By default jti is required but always considered valid.
//
// Do note that the following implementation isn't production-grade and only for demonstration purposes.
const verifyJtiOpts = new mbWebookSignatureJwt.VerifyOptions();
const verifyJtiOpts = new mbWebhookSignatureJwt.VerifyOptions();
const seenJtis = new Set();

skipUrlOpts.jwtVerifyJti = (jti) => {
Expand All @@ -56,7 +56,7 @@ skipUrlOpts.jwtVerifyJti = (jti) => {
seenJtis.add(jti);
return true;
};
const verifyJtiVerifySignature = new mbWebookSignatureJwt.ExpressMiddlewareVerify(secret, verifyJtiOpts);
const verifyJtiVerifySignature = new mbWebhookSignatureJwt.ExpressMiddlewareVerify(secret, verifyJtiOpts);

app.get('/webhook-verify-jti', verifyJtiVerifySignature, (req, res) => {
res.send('verified with jti');
Expand Down
6 changes: 3 additions & 3 deletions examples/webhook-signature-http-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mbWebookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const mbWebhookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const http = require('http');
const { createSecretKey } = require('crypto');

Expand Down Expand Up @@ -30,9 +30,9 @@ const server = http.createServer((req, res) => {
.then(() => {
let body = Buffer.concat(chunks);
let url = `${getProtocol(req)}://${req.headers.host}${req.url}`;
let jwt = req.headers[mbWebookSignatureJwt.SIGNATURE_HEADER_NAME];
let jwt = req.headers[mbWebhookSignatureJwt.SIGNATURE_HEADER_NAME];

return mbWebookSignatureJwt.verify(
return mbWebhookSignatureJwt.verify(
url,
body,
jwt,
Expand Down
33 changes: 15 additions & 18 deletions lib/messagebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ module.exports = function (accessKey, timeout, features) {
timeout: timeout || 5000
};

var CONVERSATIONSENDPOINT = 'conversations.messagebird.com';
var CONVERSATIONS_ENDPOINT = 'conversations.messagebird.com';
var VOICE_ENDPOINT = 'voice.messagebird.com';
var IS_FIREBASE_PLUGIN_ENABLED = false;

if (features && 'indexOf' in features) {
if (features.indexOf('ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX') !== -1) {
CONVERSATIONSENDPOINT = 'whatsapp-sandbox.messagebird.com';
}
if (features.indexOf('ENABLE_FIREBASE_PLUGIN') !== -1) {
IS_FIREBASE_PLUGIN_ENABLED = true;
}
Expand Down Expand Up @@ -608,7 +605,7 @@ module.exports = function (accessKey, timeout, features) {

conversations: {
getEndpoint: function () {
return CONVERSATIONSENDPOINT;
return CONVERSATIONS_ENDPOINT;
},

/**
Expand All @@ -623,7 +620,7 @@ module.exports = function (accessKey, timeout, features) {
*/
send: function (params, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'POST',
path: '/v1/send',
params: params
Expand All @@ -642,7 +639,7 @@ module.exports = function (accessKey, timeout, features) {
*/
start: function (params, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'POST',
path: '/v1/conversations/start',
params: params
Expand Down Expand Up @@ -672,7 +669,7 @@ module.exports = function (accessKey, timeout, features) {
}

httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'GET',
path: '/v1/conversations',
params: params
Expand All @@ -688,7 +685,7 @@ module.exports = function (accessKey, timeout, features) {
*/
read: function (id, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'GET',
path: '/v1/conversations/' + id
}, callback);
Expand All @@ -704,7 +701,7 @@ module.exports = function (accessKey, timeout, features) {
*/
update: function (id, params, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'PATCH',
path: '/v1/conversations/' + id,
params: params
Expand All @@ -722,7 +719,7 @@ module.exports = function (accessKey, timeout, features) {
*/
reply: function (id, params, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'POST',
path: '/v1/conversations/' + id + '/messages',
params: params
Expand Down Expand Up @@ -751,7 +748,7 @@ module.exports = function (accessKey, timeout, features) {
}

httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'GET',
path: '/v1/conversations/' + id + '/messages',
params: params
Expand All @@ -767,7 +764,7 @@ module.exports = function (accessKey, timeout, features) {
*/
readMessage: function (id, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'GET',
path: '/v1/messages/' + id
}, callback);
Expand All @@ -784,7 +781,7 @@ module.exports = function (accessKey, timeout, features) {
*/
create: function (params, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'POST',
path: '/v1/webhooks',
params: params
Expand All @@ -800,7 +797,7 @@ module.exports = function (accessKey, timeout, features) {
*/
read: function (id, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'GET',
path: '/v1/webhooks/' + id
}, callback);
Expand All @@ -816,7 +813,7 @@ module.exports = function (accessKey, timeout, features) {
*/
update: function (id, params, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'PATCH',
path: '/v1/webhooks/' + id,
params: params
Expand Down Expand Up @@ -844,7 +841,7 @@ module.exports = function (accessKey, timeout, features) {
}

httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'GET',
path: '/v1/webhooks',
params: params
Expand All @@ -860,7 +857,7 @@ module.exports = function (accessKey, timeout, features) {
*/
delete: function (id, callback) {
httpRequest({
hostname: CONVERSATIONSENDPOINT,
hostname: CONVERSATIONS_ENDPOINT,
method: 'DELETE',
path: '/v1/webhooks/' + id
}, callback);
Expand Down
Loading

0 comments on commit 2bc9447

Please sign in to comment.