-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
security.handlers.spec.ts
448 lines (413 loc) 路 15 KB
/
security.handlers.spec.ts
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
import {
OpenApiValidatorOpts,
ValidateSecurityOpts,
OpenAPIV3,
} from '../src/framework/types';
// NOTE/TODO: These tests modify eovConf.validateSecurity.handlers
// Thus test execution order matters :-(
describe('security.handlers', () => {
let app = null;
let basePath = null;
const eovConf: OpenApiValidatorOpts = {
apiSpec: path.join('test', 'resources', 'security.yaml'),
validateSecurity: {
handlers: {
ApiKeyAuth: (req, scopes, schema) => {
throw Error('custom api key handler failed');
},
},
},
};
before(async () => {
// Set up the express app
app = await createApp(eovConf, 3005);
basePath = app.basePath;
app.use(
`${basePath}`,
express
.Router()
.get(`/api_key`, (req, res) => res.json({ logged_in: true }))
.get(`/bearer`, (req, res) => res.json({ logged_in: true }))
.get(`/basic`, (req, res) => res.json({ logged_in: true }))
.get(`/cookie_auth`, (req, res) => res.json({ logged_in: true }))
.get(`/oauth2`, (req, res) => res.json({ logged_in: true }))
.get(`/openid`, (req, res) => res.json({ logged_in: true }))
.get(`/api_key_or_anonymous`, (req, res) =>
res.json({ logged_in: true }),
)
.get('/no_security', (req, res) => res.json({ logged_in: true })),
);
});
after(() => {
app.server.close();
});
it('should return 200 if no security', async () =>
request(app).get(`${basePath}/no_security`).expect(200));
it('should return 401 if apikey handler throws exception', async () =>
request(app)
.get(`${basePath}/api_key`)
.set('X-API-Key', 'test')
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.equals(
'custom api key handler failed',
);
}));
it('should return 401 if apikey handler returns false', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = <any>function (req, scopes, schema) {
expect(scopes).to.be.an('array').with.length(0);
return false;
};
return request(app)
.get(`${basePath}/api_key`)
.set('X-API-Key', 'test')
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.equals('unauthorized');
});
});
it('should return 401 if apikey handler returns Promise with false', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = <any>function (req, scopes, schema) {
expect(scopes).to.be.an('array').with.length(0);
return Promise.resolve(false);
};
return request(app)
.get(`${basePath}/api_key`)
.set('X-API-Key', 'test')
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.equals('unauthorized');
});
});
it('should return 401 if cookie auth handler returns Promise with false', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.CookieAuth = <any>function (req, scopes, schema) {
expect(scopes).to.be.an('array').with.length(0);
return Promise.resolve(false);
};
return request(app)
.get(`${basePath}/cookie_auth`)
.set('Cookie', ['JSESSIONID=12345667', 'myApp-other=blah'])
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.equals('unauthorized');
});
});
it('should return 401 if apikey handler returns Promise reject with custom message', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = (req, scopes, schema) => {
expect(scopes).to.be.an('array').with.length(0);
return Promise.reject(new Error('rejected promise'));
};
return request(app)
.get(`${basePath}/api_key`)
.set('X-API-Key', 'test')
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.equals('rejected promise');
});
});
it('should return 401 if apikey header is missing', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = (req, scopes, schema) => true;
return request(app)
.get(`${basePath}/api_key`)
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.include('X-API-Key');
});
});
it('should return 200 if apikey header exists and handler returns true', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = function (
req,
scopes,
schema: OpenAPIV3.ApiKeySecurityScheme,
) {
expect(schema.type).to.equal('apiKey');
expect(schema.in).to.equal('header');
expect(schema.name).to.equal('X-API-Key');
expect(scopes).to.be.an('array').with.length(0);
return true;
};
return request(app)
.get(`${basePath}/api_key`)
.set('X-API-Key', 'test')
.expect(200);
});
it('should return 404 if apikey header exists and handler returns true but path doesnt exist', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = (
req,
scopes,
schema: OpenAPIV3.ApiKeySecurityScheme,
) => {
expect(schema.type).to.equal('apiKey');
expect(schema.in).to.equal('header');
expect(schema.name).to.equal('X-API-Key');
expect(scopes).to.be.an('array').with.length(0);
return true;
};
return request(app)
.get(`${basePath}/api_key_but_invalid_path`)
.set('X-API-Key', 'test')
.expect(404);
});
it('should return 401 if auth header is missing for basic auth', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.BasicAuth = (req, scopes, schema) => true;
return request(app)
.get(`${basePath}/basic`)
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.include('Authorization');
});
});
it('should return 401 if auth header has malformed basic auth', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.BasicAuth = (req, scopes, schema) => true;
return request(app)
.get(`${basePath}/basic`)
.set('Authorization', 'XXXX')
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.include(
"Authorization header with scheme 'Basic' required",
);
});
});
it('should return 401 if auth header is missing for bearer auth', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.BearerAuth = (req, scopes, schema) => true;
return request(app)
.get(`${basePath}/bearer`)
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.include('Authorization');
});
});
it('should return 401 if auth header has malformed bearer auth', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.BearerAuth = (req, scopes, schema) => true;
return request(app)
.get(`${basePath}/bearer`)
.set('Authorization', 'XXXX')
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.errors[0].message).to.include(
"Authorization header with scheme 'Bearer' required",
);
});
});
it('should return 200 if bearer auth succeeds', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.BearerAuth = (
req,
scopes,
schema: OpenAPIV3.HttpSecurityScheme,
) => {
expect(schema.type).to.equal('http');
expect(schema.scheme).to.equal('bearer');
expect(scopes).to.be.an('array').with.length(0);
return true;
};
return request(app)
.get(`${basePath}/bearer`)
.set('Authorization', 'Bearer XXXX')
.expect(200);
});
it('should return 200 if oauth2 auth succeeds', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.OAuth2 = function (
req,
scopes,
schema: OpenAPIV3.OAuth2SecurityScheme,
) {
expect(schema.type).to.equal('oauth2');
expect(schema).to.have.property('flows');
expect(scopes).to.be.an('array').with.length(2);
return true;
};
return request(app).get(`${basePath}/oauth2`).expect(200);
});
it('should return 403 if oauth2 handler throws 403', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.OAuth2 = function (
req,
scopes: string[],
schema: OpenAPIV3.OAuth2SecurityScheme,
) {
expect(schema.type).to.equal('oauth2');
expect(schema).to.have.property('flows');
expect(scopes).to.be.an('array').with.length(2);
throw { status: 403, message: 'forbidden' };
};
return request(app)
.get(`${basePath}/oauth2`)
.expect(403)
.then((r) => {
const body = r.body;
expect(r.body.message).to.equal('forbidden');
});
});
it('should return 200 if openid auth succeeds', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.OpenID = (
req,
scopes,
schema: OpenAPIV3.OpenIdSecurityScheme,
) => {
expect(schema.type).to.equal('openIdConnect');
expect(schema).to.have.property('openIdConnectUrl');
expect(scopes).to.be.an('array').with.length(2);
return true;
};
return request(app).get(`${basePath}/openid`).expect(200);
});
it('should return 500 if security handlers are defined, but not for all securities', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
delete validateSecurity.handlers.OpenID;
validateSecurity.handlers.Test = (
req,
scopes,
schema: OpenAPIV3.OpenIdSecurityScheme,
) => {
expect(schema.type).to.equal('openIdConnect');
expect(schema).to.have.property('openIdConnectUrl');
expect(scopes).to.be.an('array').with.length(2);
return true;
};
return request(app)
.get(`${basePath}/openid`)
.expect(500)
.then((r) => {
const body = r.body;
const msg = "a security handler for 'OpenID' does not exist";
expect(body.message).to.equal(msg);
expect(body.errors[0].message).to.equal(msg);
expect(body.errors[0].path).to.equal(`${basePath}/openid`);
});
});
it('should return 500 if scopes are no allowed', async () =>
request(app)
.get(`${basePath}/api_key_with_scopes`)
.set('X-Api-Key', 'XXX')
.expect(500)
.then((r) => {
const body = r.body;
expect(body.message).to.equal(
"scopes array must be empty for security type 'http'",
);
}));
it('should return 200 if api_key or anonymous and no api key is supplied', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = <any>((req, scopes, schema) => true);
return request(app).get(`${basePath}/api_key_or_anonymous`).expect(200);
});
it('should return 200 if api_key or anonymous and api key is supplied', async () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.ApiKeyAuth = <any>((req, scopes, schema) => true);
return request(app)
.get(`${basePath}/api_key_or_anonymous`)
.set('x-api-key', 'XXX')
.expect(200);
});
});
describe('when securities declare: (apikey && bearer) || basic', () => {
let app = null;
let basePath = null;
const eovConf: OpenApiValidatorOpts = {
apiSpec: path.join('test', 'resources', 'security.yaml'),
};
before(async () => {
app = await createApp(eovConf, 3005);
basePath = app.basePath;
app.use(
`${basePath}`,
express
.Router()
.get('/apikey_and_bearer_or_basic', (req, res) =>
res.json({ logged_in: true }),
),
);
});
after(() => {
app.server.close();
});
it('should return 401 if not X-Api-Key is missing', async () =>
request(app)
.get(`${basePath}/apikey_and_bearer_or_basic`)
.set('Authorization', 'Bearer XXXX') // Bearer
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.message).to.include("'X-API-Key' header required");
}));
it('should return 401 if Bearer token is missing', async () => {
eovConf.validateSecurity = true;
return request(app)
.get(`${basePath}/apikey_and_bearer_or_basic`)
.set('X-Api-Key', 'XXX') // api key
.expect(401)
.then((r) => {
const body = r.body;
expect(body.errors).to.be.an('array');
expect(body.errors).to.have.length(1);
expect(body.message).to.include('Authorization header required');
});
});
it('should return 200 when X-Api-Key and Bearer token are present', async () => {
eovConf.validateSecurity = true;
return request(app)
.get(`${basePath}/apikey_and_bearer_or_basic`)
.set('Authorization', 'Bearer XXXX') // Bearer
.set('X-Api-Key', 'XXX') // api key
.expect(200);
});
it('should return 200 when Basic auth is present', async () => {
eovConf.validateSecurity = true;
return request(app)
.get(`${basePath}/apikey_and_bearer_or_basic`)
.set('Authorization', 'Basic XXXX')
.expect(200);
});
});