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

Add support for promises #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ validator.validate(message, function (err, message) {
});
```

The `validate` method also supports promises, if no callback is passed a promise
is returned:

```javascript
var MessageValidator = require('sns-validator'),
validator = new MessageValidator();

validator.validate(message)
then(function (message) {
// message has been validated and its signature checked.
})
.catch(function (err) {
// Your message could not be validated.
});
```

## Installation

The SNS Message Validator relies on the Node crypto module and is only designed
Expand Down
111 changes: 68 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,37 @@ var validateUrl = function (urlToValidate, hostPattern) {
&& hostPattern.test(parsed.host);
};

var getCertificate = function (certUrl, cb) {
if (certCache.hasOwnProperty(certUrl)) {
cb(null, certCache[certUrl]);
return;
}
var getCertificate = function (certUrl) {
return new Promise(function (resolve, reject) {
if (certCache.hasOwnProperty(certUrl)) {
resolve(certCache[certUrl]);
return;
}

https.get(certUrl, function (res) {
var chunks = [];
https.get(certUrl, function (res) {
var chunks = [];

if(res.statusCode !== 200){
return cb(new Error('Certificate could not be retrieved'));
}
if(res.statusCode !== 200){
reject(new Error('Certificate could not be retrieved'));
return;
}

res
.on('data', function (data) {
chunks.push(data.toString());
})
.on('end', function () {
certCache[certUrl] = chunks.join('');
cb(null, certCache[certUrl]);
});
}).on('error', cb)
res
.on('data', function (data) {
chunks.push(data.toString());
})
.on('end', function () {
certCache[certUrl] = chunks.join('');
resolve(certCache[certUrl]);
});
}).on('error', reject);
});
};

var validateSignature = function (message, cb, encoding) {
var validateSignature = function (message, encoding) {
if (message['SignatureVersion'] !== '1') {
cb(new Error('The signature version '
return Promise.reject(new Error('The signature version '
+ message['SignatureVersion'] + ' is not supported.'));
return;
}

var signableKeys = [];
Expand All @@ -143,21 +145,18 @@ var validateSignature = function (message, cb, encoding) {
}
}

getCertificate(message['SigningCertURL'], function (err, certificate) {
if (err) {
cb(err);
return;
}
try {
if (verifier.verify(certificate, message['Signature'], 'base64')) {
cb(null, message);
} else {
cb(new Error('The message signature is invalid.'));
return getCertificate(message['SigningCertURL'])
.then(function (certificate) {
try {
if (verifier.verify(certificate, message['Signature'], 'base64')) {
return Promise.resolve(message);
} else {
return Promise.reject(new Error('The message signature is invalid.'));
}
} catch (e) {
return Promise.reject(new Error('The message signature is invalid.'));
}
} catch (e) {
cb(e);
}
});
})
};

/**
Expand Down Expand Up @@ -186,31 +185,57 @@ function MessageValidator(hostPattern, encoding) {
* Validates a message's signature and passes it to the provided callback.
*
* @param {Object} hash
* @param {validationCallback} cb
* @param {validationCallback} cb - Optional callback, if not passed a Promise is returned.
* @returns {Promise<Object>} - If no callback is passed, a Promise is returned.
*/
MessageValidator.prototype.validate = function (hash, cb) {
if (typeof hash === 'string') {
try {
hash = JSON.parse(hash);
} catch (err) {
cb(err);
return;
if (cb) {
cb(err);
return;
}
return Promise.reject(err);
}
}

hash = convertLambdaMessage(hash);

if (!validateMessageStructure(hash)) {
cb(new Error('Message missing required keys.'));
return;
var err = new Error('Message missing required keys.');
if (cb) {
cb(err);
return;
}
return Promise.reject(err);
}

if (!validateUrl(hash['SigningCertURL'], this.hostPattern)) {
cb(new Error('The certificate is located on an invalid domain.'));
var err = new Error('The certificate is located on an invalid domain.');
if (cb) {
cb(err);
return;
}

return Promise.reject(err);
}

var result = validateSignature(hash, this.encoding);

if (cb) {
result
.then(function (message) {
cb(null, message);
})
.catch(function (err) {
cb(err);
});
return;
}

validateSignature(hash, cb, this.encoding);
return result;
};

module.exports = MessageValidator;
Loading