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