Skip to content

Commit f3cf097

Browse files
enable to specify message encoding
1 parent 3ff5ed7 commit f3cf097

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ validator.validate(message, function (err, message) {
2828
// Your message could not be validated.
2929
return;
3030
}
31-
31+
3232
// message has been validated and its signature checked.
3333
});
3434
```
@@ -79,7 +79,7 @@ validator.validate(message, function (err, message) {
7979
console.error(err);
8080
return;
8181
}
82-
82+
8383
if (message['Type'] === 'SubscriptionConfirmation') {
8484
https.get(message['SubscribeURL'], function (res) {
8585
// You have confirmed your endpoint subscription
@@ -88,6 +88,15 @@ validator.validate(message, function (err, message) {
8888
});
8989
```
9090

91+
If an incoming message includes multibyte characters and its encoding is utf8,
92+
set the encoding to `validator`.
93+
94+
```javascript
95+
var MessageValidator = require('sns-validator'),
96+
validator = new MessageValidator();
97+
validator.encoding = 'utf8';
98+
```
99+
91100
### Receiving a Notification
92101

93102
To receive a notification, use the same code as the preceding example, but

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var getCertificate = function (certUrl, cb) {
8888
}).on('error', cb)
8989
};
9090

91-
var validateSignature = function (message, cb) {
91+
var validateSignature = function (message, cb, encoding) {
9292
if (message['SignatureVersion'] !== '1') {
9393
cb(new Error('The signature version '
9494
+ message['SignatureVersion'] + ' is not supported.'));
@@ -99,7 +99,7 @@ var validateSignature = function (message, cb) {
9999
for (var i = 0; i < signableKeys.length; i++) {
100100
if (signableKeys[i] in message) {
101101
verifier.update(signableKeys[i] + "\n"
102-
+ message[signableKeys[i]] + "\n");
102+
+ message[signableKeys[i]] + "\n", encoding);
103103
}
104104
}
105105

@@ -123,8 +123,9 @@ var validateSignature = function (message, cb) {
123123
* @constructor
124124
* @param {RegExp} [hostPattern=/^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$/] - A pattern used to validate that a message's certificate originates from a trusted domain.
125125
*/
126-
function MessageValidator(hostPattern) {
126+
function MessageValidator(hostPattern, encoding) {
127127
this.hostPattern = hostPattern || defaultHostPattern;
128+
this.encoding = encoding;
128129
}
129130

130131
/**
@@ -156,7 +157,7 @@ MessageValidator.prototype.validate = function (hash, cb) {
156157
return;
157158
}
158159

159-
validateSignature(hash, cb);
160+
validateSignature(hash, cb, this.encoding);
160161
};
161162

162163
module.exports = MessageValidator;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sns-validator",
3-
"version": "0.1.0",
3+
"version": "0.2.1",
44
"description": "A standalone validator for inbound SNS HTTP messages. No dependency on the AWS SDK for JavaScript.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)