Skip to content
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
6 changes: 3 additions & 3 deletions lib/verify-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ function jwsDecode(jwsSig, opts) {
if (!header)
return null;

var payload = payloadFromJWS(jwsSig);
var payload = payloadFromJWS(jwsSig, opts.encoding);
if (header.typ === 'JWT' || opts.json)
payload = JSON.parse(payload, opts.encoding);
payload = JSON.parse(payload);

return {
header: header,
Expand Down Expand Up @@ -100,7 +100,7 @@ util.inherits(VerifyStream, Stream);
VerifyStream.prototype.verify = function verify() {
try {
var valid = jwsVerify(this.signature.buffer, this.algorithm, this.key.buffer);
var obj = jwsDecode(this.signature.buffer, this.encoding);
var obj = jwsDecode(this.signature.buffer, {encoding: this.encoding});
this.emit('done', valid, obj);
this.emit('data', valid);
this.emit('end');
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Note that the `"alg"` value from the signature header is ignored.
(Synchronous) Returns the decoded header, decoded payload, and signature
parts of the JWS Signature.

Options:

* `encoding` (Optional, defaults to 'utf8')

Returns an object with three properties, e.g.
```js
{ header: { alg: 'HS256' },
Expand Down
15 changes: 14 additions & 1 deletion test/jws.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ test('jws.decode: with invalid json in body', function (t) {
t.end();
});

test('jws.decode supports encoding option', function (t) {
const payloadString = 'åäöí';
const encoding = 'latin1';
const jwsObj = jws.sign({
header: { alg: 'HS256' },
payload: payloadString,
secret: 'shhh',
encoding: encoding
});
const parts = jws.decode(jwsObj, {encoding: encoding});
t.same(parts.payload, payloadString, 'should match payload');
t.end();
});

test('jws.verify: missing or invalid algorithm', function (t) {
const header = Buffer.from('{"something":"not an algo"}').toString('base64');
const payload = Buffer.from('sup').toString('base64');
Expand All @@ -316,7 +330,6 @@ test('jws.verify: missing or invalid algorithm', function (t) {
t.end();
});


test('jws.isValid', function (t) {
const valid = jws.sign({ header: { alg: 'HS256' }, payload: 'hi', secret: 'shhh' });
const invalid = (function(){
Expand Down