Skip to content

Commit

Permalink
Merge pull request #39 from nearform/fix-payload-overwrite
Browse files Browse the repository at this point in the history
Removes undefined props
  • Loading branch information
ovhemert authored Sep 4, 2020
2 parents 36d4841 + 3743aea commit f01ef74
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ module.exports = function createSigner(options) {
throw new TokenError(TokenError.codes.invalidOption, 'The header option must be a object.')
}

const fpo = { jti, aud, iss, sub, nonce }
const fixedPayload = Object.keys(fpo).reduce((obj, key) => { return (fpo[key] !== undefined) ? Object.assign(obj, { [key]: fpo[key] }) : obj }, {})

// Return the signer
const context = {
key,
Expand All @@ -280,13 +283,7 @@ module.exports = function createSigner(options) {
kid,
isAsync: keyType === 'function',
additionalHeader,
fixedPayload: {
jti,
aud,
iss,
sub,
nonce
}
fixedPayload
}

return sign.bind(null, context)
Expand Down
9 changes: 9 additions & 0 deletions test/signer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ test('it respect the payload iat, if one is set', t => {
t.end()
})

test('it respect the payload sub, if one is set', t => {
t.equal(
sign({ a: 1, sub: 'SUB' }, { noTimestamp: true }),
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoxLCJzdWIiOiJTVUIifQ.wweP9vNGt77bBGwZ_PLXfPxy2qcx2mnjUa0AWVA5bEM'
)

t.end()
})

test('it uses the clockTimestamp option, if one is set', t => {
t.equal(
sign({ a: 1 }, { clockTimestamp: 123000 }),
Expand Down

0 comments on commit f01ef74

Please sign in to comment.