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
4 changes: 2 additions & 2 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
}
}

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

const encoding = options.encoding || 'utf8';

Expand Down
24 changes: 24 additions & 0 deletions test/callback-issue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const assert = require('assert');
const jwt = require('../index.js');

describe('Fix: jwt.sign callback should not be called twice', function () {
it('should call callback only once when payload.iss conflicts with options.issuer', function (done) {
let callbackCount = 0;

jwt.sign(
{ iss: 'bar', iat: 1757476476 },
'secret',
{ algorithm: 'HS256', issuer: 'foo' },
(err) => {
callbackCount++;
assert.ok(err, 'Expected an error due to issuer conflict');
assert.strictEqual(
err.message,
'Bad "options.issuer" option. The payload already has an "iss" property.'
);
assert.strictEqual(callbackCount, 1, 'Callback was called more than once');
done();
}
);
});
});