-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt-tests.js
More file actions
136 lines (130 loc) · 3.49 KB
/
jwt-tests.js
File metadata and controls
136 lines (130 loc) · 3.49 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
import { expect } from "chai";
import { createJWTFactory, createJWTValidator } from "meteor/leaonline:jwt";
describe("JWT", () => {
let factory;
beforeEach(() => {
factory = createJWTFactory({
key: "moo",
sub: "foo-user",
expires: 0,
});
});
describe(createJWTFactory.name, () => {
it("creates a JWT", () => {
const { body, header, signingKey } = factory({
name: "test",
compact: false,
});
expect(signingKey).to.deep.equal("moo");
expect(header.typ).to.equal("JWT");
expect(header.alg).to.equal("HS256");
const { jti, iat, iss, sub, scope } = body;
expect(iat).to.be.a("number");
expect(jti).to.be.a("string").with.lengthOf(36);
expect(scope).to.equal("test");
expect(iss).to.equal(Meteor.absoluteUrl());
expect(sub).to.equal("foo-user");
const compact = factory({ name: "test" });
expect(compact).to.be.a("string").with.lengthOf(249);
});
});
describe(createJWTValidator.name, () => {
it("validates a JWT", () => {
const token = factory({ name: "test" });
const validator = createJWTValidator({
key: "moo",
positives: [
{
url: Meteor.absoluteUrl(),
sub: "foo-user",
},
],
});
const { valid, reason } = validator(token, "test");
expect(valid).to.equal(true);
expect(reason).to.equal(undefined);
});
it("invalidates a JWT with wrong key", () => {
const token = factory({ name: "test" });
const validator = createJWTValidator({
key: "moo-wrong",
positives: [
{
url: Meteor.absoluteUrl(),
sub: "foo-user",
},
],
});
const { valid, reason } = validator(token, "test");
expect(valid).to.equal(false);
expect(reason).to.equal("Signature verification failed");
});
it("invalidates a JWT with wrong sub", () => {
const token = factory({ name: "test" });
const validator = createJWTValidator({
key: "moo",
positives: [
{
url: Meteor.absoluteUrl(),
sub: "foo-wrong",
},
],
});
const { valid, reason } = validator(token, "test");
expect(valid).to.equal(false);
expect(reason).to.equal(
`host ${Meteor.absoluteUrl()} has invalid url or invalid sub foo-user`,
);
});
it("invalidates a JWT with wrong issuer", () => {
const token = factory({ name: "test" });
const validator = createJWTValidator({
key: "moo",
positives: [
{
url: Meteor.absoluteUrl("wrong"),
sub: "foo-user",
},
],
});
const { valid, reason } = validator(token, "test");
expect(valid).to.equal(false);
expect(reason).to.equal(
`host ${Meteor.absoluteUrl()} has invalid url or invalid sub foo-user`,
);
});
it("invalidates a JWT with wrong scope", () => {
const token = factory({ name: "test-wrong" });
const validator = createJWTValidator({
key: "moo",
positives: [
{
url: Meteor.absoluteUrl(),
sub: "foo-user",
},
],
});
const { valid, reason } = validator(token, "test");
expect(valid).to.equal(false);
expect(reason).to.equal("invalid scope for test: test-wrong");
});
it("invalidates a JWT with wrong expiration", (done) => {
const token = factory({ name: "test-wrong", expires: 10 });
const validator = createJWTValidator({
key: "moo",
positives: [
{
url: Meteor.absoluteUrl(),
sub: "foo-user",
},
],
});
setTimeout(() => {
const { valid, reason } = validator(token, "test");
expect(valid).to.equal(false);
expect(reason).to.equal("Jwt is expired");
done();
}, 500);
});
});
});