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
|
"use strict";
import jwt from "jsonwebtoken";
import { expect, describe, it } from "bun:test";
import util from "util";
import testUtils from "./test-utils";
function signWithIssuer(issuer, payload, callback) {
const options = { algorithm: "HS256" };
if (issuer !== undefined) {
options.issuer = issuer;
}
testUtils.signJWTHelper(payload, "secret", options, callback);
}
describe("issuer", function () {
describe('`jwt.sign` "issuer" option validation', function () {
[true, false, null, -1, 0, 1, -1.1, 1.1, -Infinity, Infinity, NaN, [], ["foo"], {}, { foo: "bar" }].forEach(
issuer => {
it(`should error with with value ${util.inspect(issuer)}`, function (done) {
signWithIssuer(issuer, {}, err => {
testUtils.asyncCheck(done, () => {
expect(err).toBeInstanceOf(Error);
expect(err).toHaveProperty("message", '"issuer" must be a string');
});
});
});
},
);
// undefined needs special treatment because {} is not the same as {issuer: undefined}
it("should error with with value undefined", function (done) {
testUtils.signJWTHelper({}, "secret", { issuer: undefined, algorithm: "HS256" }, err => {
testUtils.asyncCheck(done, () => {
expect(err).toBeInstanceOf(Error);
expect(err).toHaveProperty("message", '"issuer" must be a string');
});
});
});
it('should error when "iss" is in payload', function (done) {
signWithIssuer("foo", { iss: "bar" }, err => {
testUtils.asyncCheck(done, () => {
expect(err).toBeInstanceOf(Error);
expect(err).toHaveProperty(
"message",
'Bad "options.issuer" option. The payload already has an "iss" property.',
);
});
});
});
it("should error with a string payload", function (done) {
signWithIssuer("foo", "a string payload", err => {
testUtils.asyncCheck(done, () => {
expect(err).toBeInstanceOf(Error);
expect(err).toHaveProperty("message", "invalid issuer option for string payload");
});
});
});
it("should error with a Buffer payload", function (done) {
signWithIssuer("foo", new Buffer("a Buffer payload"), err => {
testUtils.asyncCheck(done, () => {
expect(err).toBeInstanceOf(Error);
expect(err).toHaveProperty("message", "invalid issuer option for object payload");
});
});
});
});
describe("when signing and verifying a token", function () {
it('should not verify "iss" if verify "issuer" option not provided', function (done) {
signWithIssuer(undefined, { iss: "foo" }, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", {}, (e2, decoded) => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeNull();
expect(decoded).toHaveProperty("iss", "foo");
});
});
});
});
describe('with string "issuer" option', function () {
it('should verify with a string "issuer"', function (done) {
signWithIssuer("foo", {}, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: "foo" }, (e2, decoded) => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeNull();
expect(decoded).toHaveProperty("iss", "foo");
});
});
});
});
it('should verify with a string "iss"', function (done) {
signWithIssuer(undefined, { iss: "foo" }, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: "foo" }, (e2, decoded) => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeNull();
expect(decoded).toHaveProperty("iss", "foo");
});
});
});
});
it('should error if "iss" does not match verify "issuer" option', function (done) {
signWithIssuer(undefined, { iss: "foobar" }, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: "foo" }, e2 => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeInstanceOf(jwt.JsonWebTokenError);
expect(e2).toHaveProperty("message", "jwt issuer invalid. expected: foo");
});
});
});
});
it('should error without "iss" and with verify "issuer" option', function (done) {
signWithIssuer(undefined, {}, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: "foo" }, e2 => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeInstanceOf(jwt.JsonWebTokenError);
expect(e2).toHaveProperty("message", "jwt issuer invalid. expected: foo");
});
});
});
});
});
describe('with array "issuer" option', function () {
it('should verify with a string "issuer"', function (done) {
signWithIssuer("bar", {}, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: ["foo", "bar"] }, (e2, decoded) => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeNull();
expect(decoded).toHaveProperty("iss", "bar");
});
});
});
});
it('should verify with a string "iss"', function (done) {
signWithIssuer(undefined, { iss: "foo" }, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: ["foo", "bar"] }, (e2, decoded) => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeNull();
expect(decoded).toHaveProperty("iss", "foo");
});
});
});
});
it('should error if "iss" does not match verify "issuer" option', function (done) {
signWithIssuer(undefined, { iss: "foobar" }, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: ["foo", "bar"] }, e2 => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeInstanceOf(jwt.JsonWebTokenError);
expect(e2).toHaveProperty("message", "jwt issuer invalid. expected: foo,bar");
});
});
});
});
it('should error without "iss" and with verify "issuer" option', function (done) {
signWithIssuer(undefined, {}, (e1, token) => {
testUtils.verifyJWTHelper(token, "secret", { issuer: ["foo", "bar"] }, e2 => {
testUtils.asyncCheck(done, () => {
expect(e1).toBeNull();
expect(e2).toBeInstanceOf(jwt.JsonWebTokenError);
expect(e2).toHaveProperty("message", "jwt issuer invalid. expected: foo,bar");
});
});
});
});
});
});
});
|