forked from auth0/nginx-jwt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_integration.js
295 lines (241 loc) · 10.5 KB
/
test_integration.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* global describe, it, before, after */
'use strict';
var cp = require('child_process');
var request = require('super-request');
var jwt = require('jsonwebtoken');
var expect = require('chai').expect;
var exec = require('child_process').exec;
var url = 'http://' + process.env.PROXY_HOST;
var secret, proxyName;
function runCommand (command, done) {
exec(command, function (err) {
if (err) { return done(err); }
// add a small delay so port bindings are ready
setTimeout(done, 100);
});
}
function runProxyContainer (name, done) {
console.log(' > Building and starting proxy container: ' + name);
var command = 'cd ..; ./scripts/run_proxy_container.sh ' + name;
runCommand(command, done);
}
function stopProxyContainer (name, done) {
console.log(' > Stopping and deleting proxy container: ' + name);
var command = 'cd ..; ./scripts/stop_proxy_container.sh ' + name;
runCommand(command, done);
}
describe('proxy', function () {
this.timeout(30000);
before(function (done) {
console.log(' > Building and starting backend container');
var command = 'cd ..; ./scripts/run_backend.sh';
runCommand(command, done);
});
after(function (done) {
console.log(' > Stopping and deleting backend container');
var command = 'cd ..; ./scripts/stop_backend.sh';
runCommand(command, done);
});
describe("configured with normal secret", function () {
before(function (done) {
secret = 'JWTs are the best!';
proxyName = 'default';
runProxyContainer(proxyName, done);
});
after(function (done) {
stopProxyContainer(proxyName, done);
});
describe("GET /", function () {
it("should return 200 with expected proxied response", function () {
return request(url)
.get('/')
.expect(200, "Backend API root")
.end();
});
});
describe("GET /secure", function () {
it("should return 401 when passing no JWT", function () {
return request(url)
.get('/secure')
.expect(401)
.expect('WWW-Authenticate', 'Bearer')
.end();
});
it("should return 401 when passing a bogus JWT", function () {
return request(url)
.get('/secure')
.headers({'Authorization': 'Bearer not-a-valid-jwt'})
.expect(401)
.expect('WWW-Authenticate', 'Bearer error="invalid_token"')
.end();
});
it("should return a 200 with the expected response header when a valid JWT is passed", function () {
var token = jwt.sign(
{ sub: 'foo-user' },
secret
);
return request(url)
.get('/secure')
.headers({'Authorization': 'Bearer ' + token})
.expect(200)
.expect('Content-Type', /json/)
.expect({ message: 'This endpoint needs to be secure.' })
.expect('X-Auth-UserId', 'foo-user')
.end();
});
});
describe("GET /secure/admin", function () {
it("should return 401 when an authenticated user is missing a required claim", function () {
var token = jwt.sign(
// roles claim missing
{ sub: 'foo-user', aud: 'foo1:bar', scope: 'admin' },
secret);
return request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(401)
.expect('WWW-Authenticate', 'Bearer')
.end();
});
it("should return 401 when a claim of an authenticated user doesn't pass a 'pattern' claim spec", function () {
var token = jwt.sign(
// aud claim has incorrect value
{ sub: 'foo-user', aud: 'foo1:bar', roles: ["sales", "marketing"], scope: 'admin' },
secret);
return request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(401)
.expect('WWW-Authenticate', 'Bearer')
.end();
});
it("should return 403 when a scope claim has incorrect value", function () {
var token = jwt.sign(
// scope claim has incorrect value
{ sub: 'foo-user', aud: 'foo:bar', roles: ["sales", "marketing"], scope: 'user' },
secret);
return request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(403)
.expect('WWW-Authenticate', 'Bearer error="insufficient_scope",scope=admin')
.end();
});
it("should return 401 when a claim of an authenticated user doesn't pass a 'function' claim spec", function () {
var token = jwt.sign(
// roles claim is missing 'marketing' role
{ sub: 'foo-user', aud: 'foo:bar', roles: ["sales"], scope: 'admin' },
secret);
return request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(401)
.expect('WWW-Authenticate', 'Bearer')
.end();
});
it("should return 200 when an authenticated user is also authorized by all claims", function () {
var token = jwt.sign(
// everything is good
{ sub: 'foo-user', aud: 'foo:bar', roles: ["sales", "marketing"], scope: 'admin' },
secret);
return request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(200)
.expect('Content-Type', /json/)
.expect({ message: 'This endpoint needs to be secure for an admin.' })
.expect('X-Auth-UserId', 'foo-user')
.end();
});
});
});
describe("configured with URL-safe base-64 encoded secret", function () {
before(function (done) {
secret = 'This secret is stored base-64 encoded on the proxy host';
proxyName = 'base64-secret';
runProxyContainer(proxyName, done);
});
after(function (done) {
stopProxyContainer(proxyName, done);
});
describe("GET /secure", function () {
it("should return a 200 with the expected response header when a valid JWT is passed", function () {
var token = jwt.sign(
{ sub: 'foo-user' },
secret);
return request(url)
.get('/secure')
.headers({'Authorization': 'Bearer ' + token})
.expect(200)
.expect('Content-Type', /json/)
.expect({ message: 'This endpoint needs to be secure.' })
.expect('X-Auth-UserId', 'foo-user')
.end();
});
});
});
describe("configured with claim_specs param that's not a table", function () {
before(function (done) {
secret = 'JWTs are the best!';
proxyName = 'config-claim_specs-not-table';
runProxyContainer(proxyName, done);
});
after(function (done) {
stopProxyContainer(proxyName, done);
});
describe("GET /secure/admin", function () {
it("should return a 500", function (done) {
var token = jwt.sign(
// everything is good
{ sub: 'foo-user', aud: 'foo:bar', roles: ["sales", "marketing"] },
secret);
request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(500)
.end(function (err) {
if (err) { done(err); }
// check docker logs for expected config error
cp.exec('docker logs proxy-config-claim_specs-not-table', function (err, stdout, stderr) {
if (err) { done(err); }
expect(stderr).to.have.string(
"Configuration error: claim_specs arg must be a table");
done();
});
});
});
});
});
describe("configured with claim_specs param that contains a spec that's not a pattern (string) or table", function () {
before(function (done) {
secret = 'JWTs are the best!';
proxyName = 'config-unsupported-claim-spec-type';
runProxyContainer(proxyName, done);
});
after(function (done) {
stopProxyContainer(proxyName, done);
});
describe("GET /secure/admin", function () {
it("should return a 500", function (done) {
var token = jwt.sign(
// everything is good
{ sub: 'foo-user', aud: 'foo:bar', roles: ["sales", "marketing"] },
secret);
request(url)
.get('/secure/admin')
.headers({'Authorization': 'Bearer ' + token})
.expect(500)
.end(function (err) {
if (err) { done(err); }
// check docker logs for expected config error
cp.exec('docker logs proxy-config-unsupported-claim-spec-type', function (err, stdout, stderr) {
if (err) { done(err); }
expect(stderr).to.have.string(
"Configuration error: claim_specs arg claim 'aud' must be a string or a table");
done();
});
});
});
});
});
});