-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
39 lines (31 loc) · 861 Bytes
/
plugin.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
const fp = require('fastify-plugin')
const Keycloak = require('./Keycloak')
module.exports = fp((fastify, opts, next) => {
const {
options,
config,
middleware = {
admin: '/',
logout: '/logout'
},
...prototypes
} = opts
if (!prototypes.accessDenied) {
prototypes.accessDenied = (request) => {
request.log.error(`Access to ${request.url} denied.`)
const err = new Error('Access Denied')
err.status = 403
throw err
}
}
for (let p = 0; p < Object.keys(prototypes).length; p++) {
Keycloak.prototype[p] = prototypes[p]
}
const keycloak = new Keycloak(options, config)
fastify.decorate('keycloak', keycloak)
const middlewares = keycloak.middleware(middleware)
for (let x = 0; x < middlewares.length; x++) {
fastify.addHook('onRequest', middlewares[x])
}
next()
})