-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
59 lines (49 loc) · 1.39 KB
/
utils.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
// eslint-disable-next-line import/no-dynamic-require
const hapi = require(process.env.HAPI_VERSION === '19' ? 'hapi19' : '@hapi/hapi');
const boom = require('@hapi/boom');
const anyAuth = require('../');
const request = server => server.inject({
url: '/',
headers: { authorization: 'Bearer Something' },
});
const authenticate = succeed => (_, h) => {
if (succeed) {
return h.authenticated({ credentials: { scope: [] } });
}
return boom.unauthorized('Missing Credentials');
};
const setupStrategies = (server, strategies) => {
const names = strategies.map((succeed, index) => {
const name = `strategy-${index}`;
server.auth.strategy(name, 'test', { succeed });
return name;
});
server.auth.strategy('any', 'any', { strategies: names });
server.auth.default('any');
};
const setupServerAndPlugin = async () => {
const server = hapi.server();
await server.initialize();
server.register(anyAuth);
server.route({
method: 'GET',
path: '/',
options: {
handler() {
return 'authenticated';
},
},
});
server.auth.scheme('test', (_, { succeed }) => ({ authenticate: authenticate(succeed) }));
return server;
};
const setupAll = async (strategies) => {
const server = await setupServerAndPlugin();
await setupStrategies(server, strategies);
return server;
};
module.exports = {
setup: setupAll,
setupServerAndPlugin,
request,
};