This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
82 lines (71 loc) · 2.17 KB
/
index.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
'use strict'
const buildUdaru = require('@nearform/udaru-core')
const buildAuthorization = require('./authentication/authorization')
const buildHapiAuthService = require('./authentication/hapi-auth-service')
const buildAuthValidation = require('./authentication/hapi-auth-validation')
const buildConfig = require('./config')
function register (server, options, next) {
const config = buildConfig(options.config)
const udaru = buildUdaru(options.dbPool, config)
// If there are hooks to register
if (typeof options.hooks === 'object') {
for (const hook of Object.keys(options.hooks)) { // For each hook
// Normalize handlers to always be an array and only consider functions
let handlers = options.hooks[hook]
if (!Array.isArray(handlers)) handlers = [handlers]
handlers = handlers.filter(f => typeof f === 'function')
// Register each handler
for (const handler of handlers) {
udaru.hooks.add(hook, handler)
}
}
}
server.decorate('server', 'udaru', udaru)
server.decorate('server', 'udaruConfig', config)
server.decorate('request', 'udaruCore', udaru)
const authorization = buildAuthorization(config)
const HapiAuthService = buildHapiAuthService(authorization)
const authValidation = buildAuthValidation(authorization)
server.register(
[
{
register: require('./routes/public/users')
},
{
register: require('./routes/public/policies')
},
{
register: require('./routes/public/teams')
},
{
register: require('./routes/public/authorization')
},
{
register: require('./routes/public/organizations')
},
{
register: require('./routes/public/monitor')
},
{
register: require('./routes/private/policies')
},
HapiAuthService
],
function (err) {
if (err) {
return next(err)
}
server.auth.strategy('default', 'service', 'required', {
validateFunc: authValidation.bind(null, {})
})
return next()
}
)
}
module.exports = {
name: 'udaru-hapi-plugin',
register
}
module.exports.register.attributes = {
pkg: require('./package')
}