jwt.test.js
10.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const jwt = require("jsonwebtoken");
const assert = require("assert");
const koajwt = require("../lib");
const UnauthorizedError = require("../lib/errors/UnauthorizedError");
const mockContext = require("./context");
describe("failure tests", function() {
let ctx;
beforeEach(() => {
ctx = mockContext();
});
it("should throw if options not sent", function() {
try {
koajwt();
} catch (err) {
assert.ok(err);
assert.equal(err.message, "secret should be set");
}
});
it("should throw if no authorization header and credentials are required", async () => {
try {
await koajwt({ secret: "shhhh", credentialsRequired: true })(
ctx,
() => {}
);
} catch (err) {
assert.ok(err);
assert.equal(err.code, "credentials_required");
}
});
it("support unless skip", async () => {
ctx.req.url = "/index.html";
await koajwt({ secret: "shhhh" }).unless({
path: "/index.html",
useOriginalUrl: false
})(ctx, () => {});
assert.ok(true);
});
it("should skip on CORS preflight", async () => {
ctx.req.method = "OPTIONS";
ctx.req.headers = {
"access-control-request-headers": "sasa, sras, authorization"
};
await koajwt({ secret: "shhhh" })(ctx, () => {});
assert.ok(true);
});
it("should throw if authorization header is malformed", async () => {
ctx.headers.authorization = "wrong";
try {
await koajwt({ secret: "shhhh" })(ctx, () => {});
} catch (err) {
assert.ok(err);
assert.equal(err.code, "credentials_bad_format");
}
});
it("should throw if authorization header is not Bearer", async () => {
ctx.headers.authorization = "Basic foobar";
try {
await koajwt({ secret: "shhhh" })(ctx, () => {});
} catch (err) {
assert.equal(err.code, "credentials_bad_scheme");
}
});
it("should next if authorization header is not Bearer and credentialsRequired is false", async () => {
ctx.headers.authorization = "Basic foobar";
await koajwt({ secret: "shhhh", credentialsRequired: false })(
ctx,
() => {}
);
assert.ok(true);
});
it("should throw if authorization header is not well-formatted jwt", async () => {
ctx.headers.authorization = "Bearer wrongjwt";
try {
await koajwt({ secret: "shhhh" })(ctx, () => {});
} catch (err) {
assert.equal(err.code, "invalid_token");
}
});
it("should throw if jwt is an invalid json", async () => {
ctx.headers.authorization =
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.yJ1c2VybmFtZSI6InNhZ3VpYXIiLCJpYXQiOjE0NzEwMTg2MzUsImV4cCI6MTQ3MzYxMDYzNX0.foo";
try {
await koajwt({ secret: "shhhh" })(ctx, () => {});
} catch (err) {
assert.equal(err.code, "invalid_token");
}
});
it("should throw if authorization header is not valid jwt", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar" }, secret);
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: "different-shhhh" })(ctx, () => {});
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.message, "invalid signature");
}
});
it("should throw if audience is not expected", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar", aud: "expected-audience" }, secret);
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: "shhhhhh", audience: "not-expected-audience" })(
ctx,
() => {}
);
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(
err.message,
"jwt audience invalid. expected: not-expected-audience"
);
}
});
it("should throw if token is expired", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar", exp: 1382412921 }, secret);
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: "shhhhhh" })(ctx, () => {});
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.inner.name, "TokenExpiredError");
assert.equal(err.message, "jwt expired");
}
});
it("should throw if token issuer is wrong", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar", iss: "http://foo" }, secret);
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: "shhhhhh", issuer: "http://wrong" })(
ctx,
() => {}
);
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.message, "jwt issuer invalid. expected: http://wrong");
}
});
it("should use errors thrown from custom getToken function", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar" }, secret);
function getTokenThatThrowsError() {
throw new UnauthorizedError("invalid_token", {
message: "Invalid token!"
});
}
try {
await koajwt({
secret: "shhhhhh",
getToken: getTokenThatThrowsError
})(ctx, () => {});
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.message, "Invalid token!");
}
});
it("should throw error when signature is wrong", async () => {
const secret = "shhh";
const token = jwt.sign({ foo: "bar", iss: "http://www" }, secret);
// manipulate the token
const newContent = new Buffer("{foo: 'bar', edg: 'ar'}").toString("base64");
const splitetToken = token.split(".");
splitetToken[1] = newContent;
const newToken = splitetToken.join(".");
// build request
ctx.headers = [];
ctx.headers.authorization = "Bearer " + newToken;
try {
await koajwt({ secret: secret })(ctx, () => {});
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.message, "invalid token");
}
});
it("should throw error if token is expired even with when credentials are not required", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar", exp: 1382412921 }, secret);
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: secret, credentialsRequired: false })(
ctx,
() => {}
);
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.message, "jwt expired");
}
});
it("should throw error if token is invalid even with when credentials are not required", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar", exp: 1382412921 }, secret);
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: "not the secret", credentialsRequired: false })(
ctx,
() => {}
);
} catch (err) {
assert.ok(err);
assert.equal(err.code, "invalid_token");
assert.equal(err.message, "invalid signature");
}
});
});
describe("work tests", function() {
let ctx;
beforeEach(() => {
ctx = mockContext();
});
it("should work if authorization header is valid jwt", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar" }, secret);
ctx.headers.authorization = "Bearer " + token;
await koajwt({ secret })(ctx, () => {});
assert.equal("bar", ctx.state.user.foo);
});
it("should work with nested properties", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar" }, secret);
ctx.headers.authorization = "Bearer " + token;
await koajwt({ secret, property: "auth.token" })(ctx, () => {});
assert.equal("bar", ctx.state.auth.token.foo);
});
it("should work if authorization header is valid with a buffer secret", async () => {
const secret = new Buffer(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"base64"
);
const token = jwt.sign({ foo: "bar" }, secret);
ctx.headers.authorization = "Bearer " + token;
await koajwt({ secret })(ctx, () => {});
assert.equal("bar", ctx.state.user.foo);
});
it("should set property if option provided", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar" }, secret);
ctx.headers.authorization = "Bearer " + token;
await koajwt({ secret, property: "auth" })(ctx, () => {});
assert.equal("bar", ctx.state.auth.foo);
});
it("should work if no authorization header and credentials are not required", async () => {
await koajwt({ secret: "shhhh", credentialsRequired: false })(
ctx,
() => {}
);
assert.ok(true);
});
it("should not work if no authorization header", async () => {
try {
await koajwt({ secret: "shhhh" })(ctx, () => {});
} catch (err) {
assert(typeof err !== "undefined");
}
});
it("should produce a stack trace that includes the failure reason", async () => {
const token = jwt.sign({ foo: "bar" }, "secretA");
ctx.headers.authorization = "Bearer " + token;
try {
await koajwt({ secret: "secretB" })(ctx, () => {});
} catch (err) {
const index = err.stack.indexOf("UnauthorizedError: invalid signature");
assert.equal(
index,
0,
"Stack trace didn't include 'invalid signature' message."
);
}
});
it("should work with a custom getToken function", async () => {
const secret = "shhhhhh";
const token = jwt.sign({ foo: "bar" }, secret);
ctx.query = {
token
};
function getTokenFromQuery(ctx) {
return ctx.query.token;
}
await koajwt({
secret,
getToken: getTokenFromQuery
})(ctx, () => {
assert.equal("bar", ctx.state.user.foo);
});
});
it("should work with a secret async function that accepts header argument", async () => {
const secret = "shhhhhh";
const secretAsync = async function(ctx, headers, payload, cb) {
assert.equal(headers.alg, "HS256");
assert.equal(payload.foo, "bar");
return new Promise(resolve => {
process.nextTick(function() {
resolve(secret);
});
});
};
const token = jwt.sign({ foo: "bar" }, secret);
ctx.headers.authorization = "Bearer " + token;
await koajwt({ secret: secretAsync })(ctx, () => {});
assert.equal("bar", ctx.state.user.foo);
});
});