* Sign a payload to create a JWT, ensuring that the asynchronous and synchronous calls to `sign` have the same result * @param {object} payload The JWT payload * @param {string} secretOrPrivateKey The shared secret or private key * @param {object} options Sign options * @param {function(err, tok
(payload, secretOrPrivateKey, options, callback)
| 94 | * @param {function(err, token):void} callback |
| 95 | */ |
| 96 | function signJWTHelper(payload, secretOrPrivateKey, options, callback) { |
| 97 | // freeze the time to ensure the clock remains stable across the async and sync calls |
| 98 | const fakeClock = sinon.useFakeTimers({now: Date.now()}); |
| 99 | let error; |
| 100 | let syncSigned; |
| 101 | try { |
| 102 | syncSigned = jwt.sign(payload, secretOrPrivateKey, options); |
| 103 | } |
| 104 | catch (err) { |
| 105 | error = err; |
| 106 | } |
| 107 | jwt.sign(payload, secretOrPrivateKey, options, (err, asyncSigned) => { |
| 108 | fakeClock.restore(); |
| 109 | if (error) { |
| 110 | expectEqualError(err, error); |
| 111 | callback(err); |
| 112 | } |
| 113 | else { |
| 114 | expect(syncSigned, 'Async/Sync token equality').to.equal(asyncSigned); |
| 115 | callback(null, syncSigned); |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | module.exports = { |
| 121 | asyncCheck, |
nothing calls this directly
no test coverage detected