Skip to content

Commit

Permalink
include standard claims from payload. closes #196
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Apr 28, 2016
1 parent d805573 commit b51e31f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {

Object.keys(options_to_payload).forEach(function (key) {
var claim = options_to_payload[key];
if (typeof options[key] !== 'undefined' && typeof payload[claim] !== 'undefined') {
throw new Error('Bad "options.' + key + '" option. The payload already has an "' + claim + '" property.');
if (typeof options[key] !== 'undefined') {
if (typeof payload[claim] !== 'undefined') {
throw new Error('Bad "options.' + key + '" option. The payload already has an "' + claim + '" property.');
}
payload[claim] = options[key];
}
payload[claim] = options[key];
});

var encoding = options.encoding || 'utf8';
Expand Down
15 changes: 15 additions & 0 deletions test/issue_196.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var expect = require('chai').expect;
var jwt = require('./..');
var atob = require('atob');

describe('issue 196', function () {
function b64_to_utf8 (str) {
return decodeURIComponent(escape(atob( str )));
}

it('should use issuer provided in payload.iss', function () {
var token = jwt.sign({ iss: 'foo' }, 'shhhhh');
var decoded_issuer = JSON.parse(b64_to_utf8(token.split('.')[1])).iss;
expect(decoded_issuer).to.equal('foo');
});
});

0 comments on commit b51e31f

Please sign in to comment.