-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·113 lines (96 loc) · 2.66 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const activedirectory = require('activedirectory');
const configFile = require('./config.json');
const imports = {
user: require('./src/user'),
group: require('./src/group'),
ou: require('./src/ou'),
others: require('./src/others'),
helpers: require('./src/internal/helpers'),
operations: require('./src/internal/operations'),
chainable: require('./src/chainable')
};
class AD {
constructor(config) {
if (config === undefined) {
throw new Error('Configuration is required.');
}
let invalid = !config.url || !config.user || !config.pass;
if (invalid) {
throw new Error(
'The following configuration is required: {url, user, pass}.'
);
}
if (String(config.url).indexOf('://') === -1) {
throw new Error(
'You must specify the protocol in the url, such as ldaps://127.0.0.1.'
);
}
if (String(config.user).indexOf('@') === -1) {
throw new Error(
'The user must include the fully qualified domain name, such as joe@acme.co.'
);
}
config.domain = String(config.user).split('@')[1];
if (config.baseDN === undefined) {
config.baseDN = config.domain.split('.').map(n => `DC=${n}`).join(',');
}
config = Object.assign(configFile, config);
this.config = config;
this._cache = {
enabled: true,
expiration: 600000,
users: {},
groups: {},
ous: {},
all: {},
get: (type, key) => {
if (!this._cache.enabled) {
return undefined;
}
if (!this._cache[type] || !this._cache[type][key]) {
return undefined;
}
let obj = this._cache[type][key];
let diff = new Date() - obj.timestamp;
if (diff > this._cache.expiration) {
delete this._cache[type][key];
return undefined;
}
return obj.value;
},
set: (type, key, value) => {
this._cache[type][key] = {
timestamp: new Date(),
value
};
}
};
this.ad = new activedirectory({
url: config.url,
baseDN: config.baseDN,
username: config.user,
password: config.pass,
tlsOptions: {
rejectUnauthorized: false
}
});
}
cache(bool) {
this._cache.enabled = bool;
return this;
}
cacheTimeout(millis) {
this._cache.expiration = millis;
return this;
}
}
const loadImports = () => {
for (const key in imports) {
let file = imports[key];
for (const fn in file) {
AD.prototype[fn] = file[fn];
}
}
};
loadImports();
module.exports = AD;