|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const jwt = require('../'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const sinon = require('sinon'); |
| 6 | +const util = require('util'); |
| 7 | +const testUtils = require('./test-utils'); |
| 8 | + |
| 9 | +const base64UrlEncode = testUtils.base64UrlEncode; |
| 10 | +const noneAlgorithmHeader = 'eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0'; |
| 11 | + |
| 12 | +function signWithExpiresIn(payload, expiresIn) { |
| 13 | + const options = {algorithm: 'none'}; |
| 14 | + if (expiresIn !== undefined) { |
| 15 | + options.expiresIn = expiresIn; |
| 16 | + } |
| 17 | + return jwt.sign(payload, undefined, options); |
| 18 | +} |
| 19 | + |
| 20 | +describe('expires', function() { |
| 21 | + describe('`jwt.sign` "expiresIn" option validation', function () { |
| 22 | + [ |
| 23 | + true, |
| 24 | + false, |
| 25 | + null, |
| 26 | + -1.1, |
| 27 | + 1.1, |
| 28 | + -Infinity, |
| 29 | + Infinity, |
| 30 | + NaN, |
| 31 | + ' ', |
| 32 | + 'invalid', |
| 33 | + [], |
| 34 | + ['foo'], |
| 35 | + {}, |
| 36 | + {foo: 'bar'}, |
| 37 | + ].forEach((expiresIn) => { |
| 38 | + it(`should error with with value ${util.inspect(expiresIn)}`, function () { |
| 39 | + expect(() => signWithExpiresIn({}, expiresIn)).to.throw( |
| 40 | + '"expiresIn" should be a number of seconds or string representing a timespan' |
| 41 | + ); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + // TODO this should throw the same error as other invalid inputs |
| 46 | + it(`should error with with value ''`, function () { |
| 47 | + expect(() => signWithExpiresIn({}, '')).to.throw( |
| 48 | + 'val is not a non-empty string or a valid number. val=""' |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + // undefined needs special treatment because {} is not the same as {expiresIn: undefined} |
| 53 | + it('should error with with value undefined', function () { |
| 54 | + expect(() =>jwt.sign({}, undefined, {expiresIn: undefined, algorithm: 'none'})).to.throw( |
| 55 | + '"expiresIn" should be a number of seconds or string representing a timespan' |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it ('should error when "exp" is in payload', function() { |
| 60 | + expect(() => signWithExpiresIn({exp: 100}, 100)).to.throw( |
| 61 | + 'Bad "options.expiresIn" option the payload already has an "exp" property.' |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should error with a string payload', function() { |
| 66 | + expect(() => signWithExpiresIn('a string payload', 100)).to.throw( |
| 67 | + 'invalid expiresIn option for string payload' |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should error with a Buffer payload', function() { |
| 72 | + expect(() => signWithExpiresIn(Buffer.from('a Buffer payload'), 100)).to.throw( |
| 73 | + 'invalid expiresIn option for object payload' |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('`jwt.sign` "exp" claim validation', function () { |
| 79 | + [ |
| 80 | + true, |
| 81 | + false, |
| 82 | + null, |
| 83 | + undefined, |
| 84 | + '', |
| 85 | + ' ', |
| 86 | + 'invalid', |
| 87 | + [], |
| 88 | + ['foo'], |
| 89 | + {}, |
| 90 | + {foo: 'bar'}, |
| 91 | + ].forEach((exp) => { |
| 92 | + it(`should error with with value ${util.inspect(exp)}`, function () { |
| 93 | + expect(() => signWithExpiresIn({exp})).to.throw( |
| 94 | + '"exp" should be a number of seconds' |
| 95 | + ); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('"exp" in payload validation', function () { |
| 101 | + [ |
| 102 | + true, |
| 103 | + false, |
| 104 | + null, |
| 105 | + -Infinity, |
| 106 | + Infinity, |
| 107 | + NaN, |
| 108 | + '', |
| 109 | + ' ', |
| 110 | + 'invalid', |
| 111 | + [], |
| 112 | + ['foo'], |
| 113 | + {}, |
| 114 | + {foo: 'bar'}, |
| 115 | + ].forEach((exp) => { |
| 116 | + it(`should error with with value ${util.inspect(exp)}`, function () { |
| 117 | + const encodedPayload = base64UrlEncode(JSON.stringify({exp})); |
| 118 | + const token = `${noneAlgorithmHeader}.${encodedPayload}.`; |
| 119 | + expect(() => jwt.verify(token, undefined)).to.throw( |
| 120 | + jwt.JsonWebTokenError, |
| 121 | + 'invalid exp value' |
| 122 | + ); |
| 123 | + }); |
| 124 | + }) |
| 125 | + }); |
| 126 | + |
| 127 | + describe('when signing and verifying a token with expires option', function () { |
| 128 | + let fakeClock; |
| 129 | + beforeEach(function() { |
| 130 | + fakeClock = sinon.useFakeTimers({now: 60000}); |
| 131 | + }); |
| 132 | + |
| 133 | + afterEach(function() { |
| 134 | + fakeClock.uninstall(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should set correct "exp" with negative number of seconds', function() { |
| 138 | + const token = signWithExpiresIn({}, -10); |
| 139 | + fakeClock.tick(-10001); |
| 140 | + |
| 141 | + const decoded = jwt.decode(token); |
| 142 | + const verified = jwt.verify(token, undefined); |
| 143 | + expect(decoded).to.deep.equal(verified); |
| 144 | + expect(decoded.exp).to.equal(50); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should set correct "exp" with positive number of seconds', function() { |
| 148 | + const token = signWithExpiresIn({}, 10); |
| 149 | + |
| 150 | + const decoded = jwt.decode(token); |
| 151 | + const verified = jwt.verify(token, undefined); |
| 152 | + expect(decoded).to.deep.equal(verified); |
| 153 | + expect(decoded.exp).to.equal(70); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should set correct "exp" with zero seconds', function() { |
| 157 | + const token = signWithExpiresIn({}, 0); |
| 158 | + |
| 159 | + fakeClock.tick(-1); |
| 160 | + |
| 161 | + const decoded = jwt.decode(token); |
| 162 | + const verified = jwt.verify(token, undefined); |
| 163 | + expect(decoded).to.deep.equal(verified); |
| 164 | + expect(decoded.exp).to.equal(60); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should set correct "exp" with negative string timespan', function() { |
| 168 | + const token = signWithExpiresIn({}, '-10 s'); |
| 169 | + |
| 170 | + fakeClock.tick(-10001); |
| 171 | + |
| 172 | + const decoded = jwt.decode(token); |
| 173 | + const verified = jwt.verify(token, undefined); |
| 174 | + expect(decoded).to.deep.equal(verified); |
| 175 | + expect(decoded.exp).to.equal(50); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should set correct "exp" with positive string timespan', function() { |
| 179 | + const token = signWithExpiresIn({}, '10 s'); |
| 180 | + |
| 181 | + fakeClock.tick(-10001); |
| 182 | + const decoded = jwt.decode(token); |
| 183 | + |
| 184 | + const verified = jwt.verify(token, undefined); |
| 185 | + expect(decoded).to.deep.equal(verified); |
| 186 | + expect(decoded.exp).to.equal(70); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should set correct "exp" with zero string timespan', function() { |
| 190 | + const token = signWithExpiresIn({}, '0 s'); |
| 191 | + |
| 192 | + fakeClock.tick(-1); |
| 193 | + const decoded = jwt.decode(token); |
| 194 | + const verified = jwt.verify(token, undefined); |
| 195 | + expect(decoded).to.deep.equal(verified); |
| 196 | + expect(decoded.exp).to.equal(60); |
| 197 | + }); |
| 198 | + |
| 199 | + // TODO an exp of -Infinity should fail validation |
| 200 | + it('should set null "exp" when given -Infinity', function () { |
| 201 | + const token = signWithExpiresIn({exp: -Infinity}); |
| 202 | + |
| 203 | + const decoded = jwt.decode(token); |
| 204 | + expect(decoded.exp).to.be.null; |
| 205 | + }); |
| 206 | + |
| 207 | + // TODO an exp of Infinity should fail validation |
| 208 | + it('should set null "exp" when given value Infinity', function () { |
| 209 | + const token = signWithExpiresIn({exp: Infinity}); |
| 210 | + |
| 211 | + const decoded = jwt.decode(token); |
| 212 | + expect(decoded.exp).to.be.null; |
| 213 | + }); |
| 214 | + |
| 215 | + // TODO an exp of NaN should fail validation |
| 216 | + it('should set null "exp" when given value NaN', function () { |
| 217 | + const token = signWithExpiresIn({exp: NaN}); |
| 218 | + |
| 219 | + const decoded = jwt.decode(token); |
| 220 | + expect(decoded.exp).to.be.null; |
| 221 | + }); |
| 222 | + |
| 223 | + it('should set correct "exp" when "iat" is passed', function () { |
| 224 | + const token = signWithExpiresIn({iat: 80}, -10); |
| 225 | + |
| 226 | + const decoded = jwt.decode(token); |
| 227 | + |
| 228 | + const verified = jwt.verify(token, undefined); |
| 229 | + expect(decoded).to.deep.equal(verified); |
| 230 | + expect(decoded.exp).to.equal(70); |
| 231 | + }); |
| 232 | + |
| 233 | + it('should verify "exp" using "clockTimestamp"', function () { |
| 234 | + const token = signWithExpiresIn({}, 10); |
| 235 | + |
| 236 | + const verified = jwt.verify(token, undefined, {clockTimestamp: 69}); |
| 237 | + expect(verified.iat).to.equal(60); |
| 238 | + expect(verified.exp).to.equal(70); |
| 239 | + }); |
| 240 | + |
| 241 | + it('should verify "exp" using "clockTolerance"', function () { |
| 242 | + const token = signWithExpiresIn({}, 5); |
| 243 | + |
| 244 | + fakeClock.tick(10000); |
| 245 | + |
| 246 | + const verified = jwt.verify(token, undefined, {clockTolerance: 6}); |
| 247 | + expect(verified.iat).to.equal(60); |
| 248 | + expect(verified.exp).to.equal(65); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should ignore a expired token when "ignoreExpiration" is true', function () { |
| 252 | + const token = signWithExpiresIn({}, '-10 s'); |
| 253 | + |
| 254 | + const verified = jwt.verify(token, undefined, {ignoreExpiration: true}); |
| 255 | + expect(verified.iat).to.equal(60); |
| 256 | + expect(verified.exp).to.equal(50); |
| 257 | + }); |
| 258 | + |
| 259 | + it('should error on verify if "exp" is at current time', function() { |
| 260 | + const token = signWithExpiresIn({exp: 60}); |
| 261 | + |
| 262 | + expect(() => jwt.verify(token, undefined)).to.throw( |
| 263 | + jwt.TokenExpiredError, |
| 264 | + 'jwt expired' |
| 265 | + ); |
| 266 | + }); |
| 267 | + |
| 268 | + it('should error on verify if "exp" is before current time using clockTolerance', function () { |
| 269 | + const token = signWithExpiresIn({}, -5); |
| 270 | + |
| 271 | + expect(() => jwt.verify(token, undefined, {clockTolerance: 5})).to.throw( |
| 272 | + jwt.TokenExpiredError, |
| 273 | + 'jwt expired' |
| 274 | + ); |
| 275 | + }); |
| 276 | + }); |
| 277 | +}); |
0 commit comments