-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
45 lines (37 loc) · 985 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
40
41
42
43
44
45
'use strict'
const fp = require('fastify-plugin')
const got = require('got')
/**
*
*
* @param {fastify instance} fastify
* @param {plugin options} opts
* @param {call when you are done} next
* @returns a decorated fastify instance
*/
function ldap (fastify, opts, next) {
if (opts.provider == null) return next(Error('fastify-ldap-service: provider is required'))
const provider = opts.provider
if (typeof provider === 'string') {
fastify.decorate('ldap', async function (username, password) {
const response = await got.post(provider, {
json: true,
body: {
username,
password
}
})
return response.body
})
return next()
}
if (typeof provider === 'function') {
fastify.decorate('ldap', provider)
return next()
}
return next(Error('fastify-ldap-service: provider must be a url or function'))
}
module.exports = fp(ldap, {
fastify: '>=1.x',
name: 'fastify-ldap-service'
})