Skip to content

Commit

Permalink
Replace Lambda-specific handling with a Lambda -> HTTP-style message …
Browse files Browse the repository at this point in the history
…converter
  • Loading branch information
jeskew committed Jun 23, 2017
1 parent 981c1e4 commit e06d354
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ language: node_js
node_js:
- "0.10"
- "0.12"
- "4.0"
- "4.1"
- "4.2"
- "4.3"
- "4.4"
- "6.0"
- "6.1"
- "5"
- "6"
- "7"
- "8"

script:
- npm test
Expand Down
31 changes: 18 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,15 @@ var url = require('url'),
'Token',
'TopicArn',
'Type'
];

var hashHasKey = function (hash, key) {
if (!(key in hash)) {
if (/URL$/.test(key) && key.replace(/URL$/, 'Url') in hash) {
return true;
}
return false;
}
return true;
};
],
lambdaMessageKeys = {
'SigningCertUrl': 'SigningCertURL',
'UnsubscribeUrl': 'UnsubscribeURL'
};

var hashHasKeys = function (hash, keys) {
for (var i = 0; i < keys.length; i++) {
if (!hashHasKey(hash, keys[i])) {
if (!(keys[i] in hash)) {
return false;
}
}
Expand All @@ -71,6 +65,16 @@ var indexOf = function (array, value) {
return -1;
};

function convertLambdaMessage(message) {
for (var key in lambdaMessageKeys) {
if (key in message) {
message[lambdaMessageKeys[key]] = message[key];
}
}

return message;
}

var validateMessageStructure = function (message) {
var valid = hashHasKeys(message, requiredKeys);

Expand Down Expand Up @@ -182,13 +186,14 @@ function MessageValidator(hostPattern, encoding) {
*/
MessageValidator.prototype.validate = function (hash, cb) {
var hostPattern = this.hostPattern;
hash = convertLambdaMessage(hash);

if (!validateMessageStructure(hash)) {
cb(new Error('Message missing required keys.'));
return;
}

if (!validateUrl(hash['SigningCertURL'] || hash['SigningCertUrl'], hostPattern)) {
if (!validateUrl(hash['SigningCertURL'], hostPattern)) {
cb(new Error('The certificate is located on an invalid domain.'));
return;
}
Expand Down

0 comments on commit e06d354

Please sign in to comment.