-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmitel.js
92 lines (80 loc) · 2.94 KB
/
mitel.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
83
84
85
86
87
88
89
90
91
const logger = require('pino')({ level: 'debug' })
const ldap = require('ldapjs');
const Attribute = require('@ldapjs/attribute')
const {
SearchResultEntry: SearchEntry,
} = require('@ldapjs/messages')
const parseDN = require('@ldapjs/dn').DN.fromString
const server = ldap.createServer({ log: logger });
const fetch = require('node-fetch');
let phonebook = []
let url = "https://phones.emfcamp.org/api/phonebook/EMF2024";
let settings = { method: "Get"};
async function updatePB(){
const response = await fetch(url, settings);
const data = await response.json();
phonebook = []
data.forEach(pb => {
let obj = {
dn: `cn=${pb.value},ou=EMF2024,dc=emf`,
attributes: {
ou: 'EMF2024',
sn: [pb.label.toString()],
cn: [pb.value.toString()],
telephoneNumber: [pb.value.toString()],
objectClass: ['top', 'person', 'organizationalPerson']
}
};
phonebook.push(obj)
})
logger.info("Phonebook Updated - "+data.length+" items")
}
function patchEntry(res, entry, nofiltering) {
if (!entry || typeof (entry) !== 'object') { throw new TypeError('entry (SearchEntry) required') }
if (nofiltering === undefined) { nofiltering = false }
if (typeof (nofiltering) !== 'boolean') { throw new TypeError('noFiltering must be a boolean') }
if (!entry.attributes) { throw new Error('entry.attributes required') }
const savedAttrs = {}
const all = (res.attributes.indexOf('*') !== -1)
// Filter attributes in a plain object according to the magic `_` prefix
// and presence in `notAttributes`.
Object.keys(entry.attributes).forEach(function (a) {
const _a = a//.toLowerCase()
if (!nofiltering && _a.length && _a[0] === '_') {
savedAttrs[a] = entry.attributes[a]
delete entry.attributes[a]
} else if (!nofiltering && res.notAttributes.indexOf(_a) !== -1) {
savedAttrs[a] = entry.attributes[a]
delete entry.attributes[a]
} else if (all) {
// do nothing
} else if (res.attributes.length && res.attributes.indexOf(_a) === -1) {
savedAttrs[a] = entry.attributes[a]
delete entry.attributes[a]
}
})
save = entry
entry = new SearchEntry({
objectName: typeof (save.dn) === 'string' ? parseDN(save.dn) : save.dn,
messageId: res.messageId,
attributes: Attribute.fromObject(entry.attributes)
})
// Restore attributes
Object.keys(savedAttrs).forEach(function (k) {
save.attributes[k] = savedAttrs[k]
})
return entry
}
server.search('dc=emf', (req, res, next) => {
for (const entry of phonebook) {
if (req.filter.matches(entry.attributes, /* strictCase */ false)){
res.send(patchEntry(res, entry));
}
}
res.end();
});
updatePB()
setInterval(updatePB, 60000)
server.listen(389, '0.0.0.0', function() {
logger.info('LDAP server listening at: ' + server.url);
});