This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
344 lines (308 loc) · 7.93 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'use strict'
const { core: { LBER_SET } } = require('@ldapjs/protocol')
const {
BerTypes,
BerReader,
BerWriter
} = require('@ldapjs/asn1')
const warning = require('./lib/deprecations')
/**
* Represents an LDAP attribute and its associated values as defined by
* https://www.rfc-editor.org/rfc/rfc4512#section-2.5.
*/
class Attribute {
#buffers = []
#type
/**
* @param {object} options
* @param {string} [options.type=''] The name of the attribute, e.g. "cn" for
* the common name attribute. For binary attributes, include the `;binary`
* option, e.g. `foo;binary`.
* @param {string|string[]} [options.values] Either a single value for the
* attribute, or a set of values for the attribute.
*/
constructor (options = {}) {
if (options.type && typeof (options.type) !== 'string') {
throw TypeError('options.type must be a string')
}
this.type = options.type || ''
const values = options.values || options.vals || []
if (options.vals) {
warning.emit('LDAP_ATTRIBUTE_DEP_001')
}
this.values = values
}
get [Symbol.toStringTag] () {
return 'LdapAttribute'
}
/**
* A copy of the buffers that represent the values for the attribute.
*
* @returns {Buffer[]}
*/
get buffers () {
return this.#buffers.slice(0)
}
/**
* Serializes the attribute to a plain JavaScript object representation.
*
* @returns {object}
*/
get pojo () {
return {
type: this.type,
values: this.values
}
}
/**
* The attribute name as provided during construction.
*
* @returns {string}
*/
get type () {
return this.#type
}
/**
* Set the attribute name.
*
* @param {string} name
*/
set type (name) {
this.#type = name
}
/**
* The set of attribute values as strings.
*
* @returns {string[]}
*/
get values () {
const encoding = _bufferEncoding(this.#type)
return this.#buffers.map(function (v) {
return v.toString(encoding)
})
}
/**
* Set the attribute's associated values. This will replace any values set
* at construction time.
*
* @param {string|string[]} vals
*/
set values (vals) {
if (Array.isArray(vals) === false) {
return this.addValue(vals)
}
for (const value of vals) {
this.addValue(value)
}
}
/**
* Use {@link values} instead.
*
* @deprecated
* @returns {string[]}
*/
get vals () {
warning.emit('LDAP_ATTRIBUTE_DEP_003')
return this.values
}
/**
* Use {@link values} instead.
*
* @deprecated
* @param {string|string[]} values
*/
set vals (values) {
warning.emit('LDAP_ATTRIBUTE_DEP_003')
this.values = values
}
/**
* Append a new value, or set of values, to the current set of values
* associated with the attributes.
*
* @param {string|string[]} value
*/
addValue (value) {
if (Buffer.isBuffer(value)) {
this.#buffers.push(value)
} else {
this.#buffers.push(
Buffer.from(value + '', _bufferEncoding(this.#type))
)
}
}
/**
* Replaces instance properties with those found in a given BER.
*
* @param {import('@ldapjs/asn1').BerReader} ber
*
* @deprecated Use {@link fromBer} instead.
*/
parse (ber) {
const attr = Attribute.fromBer(ber)
this.#type = attr.type
this.values = attr.values
}
/**
* Convert the {@link Attribute} instance to a {@link BerReader} capable of
* being used in an LDAP message.
*
* @returns {BerReader}
*/
toBer () {
const ber = new BerWriter()
ber.startSequence()
ber.writeString(this.type)
ber.startSequence(LBER_SET)
if (this.#buffers.length > 0) {
for (const buffer of this.#buffers) {
ber.writeByte(BerTypes.OctetString)
ber.writeLength(buffer.length)
ber.appendBuffer(buffer)
}
} else {
ber.writeStringArray([])
}
ber.endSequence()
ber.endSequence()
return new BerReader(ber.buffer)
}
toJSON () {
return this.pojo
}
/**
* Given two {@link Attribute} instances, determine if they are equal or
* different.
*
* @param {Attribute} attr1 The first object to compare.
* @param {Attribute} attr2 The second object to compare.
*
* @returns {number} `0` if the attributes are equal in value, `-1` if
* `attr1` should come before `attr2` when sorted, and `1` if `attr2` should
* come before `attr1` when sorted.
*
* @throws When either input object is not an {@link Attribute}.
*/
static compare (attr1, attr2) {
if (Attribute.isAttribute(attr1) === false || Attribute.isAttribute(attr2) === false) {
throw TypeError('can only compare Attribute instances')
}
if (attr1.type < attr2.type) return -1
if (attr1.type > attr2.type) return 1
const aValues = attr1.values
const bValues = attr2.values
if (aValues.length < bValues.length) return -1
if (aValues.length > bValues.length) return 1
for (let i = 0; i < aValues.length; i++) {
if (aValues[i] < bValues[i]) return -1
if (aValues[i] > bValues[i]) return 1
}
return 0
}
/**
* Read a BER representation of an attribute, and its values, and
* create a new {@link Attribute} instance. The BER must start
* at the beginning of a sequence.
*
* @param {import('@ldapjs/asn1').BerReader} ber
*
* @returns {Attribute}
*/
static fromBer (ber) {
ber.readSequence()
const type = ber.readString()
const values = []
// If the next byte represents a BER "SET" sequence...
if (ber.peek() === LBER_SET) {
// .. read that sequence ...
/* istanbul ignore else */
if (ber.readSequence(LBER_SET)) {
const end = ber.offset + ber.length
// ... and read all values in that set.
while (ber.offset < end) {
values.push(
ber.readString(BerTypes.OctetString, true)
)
}
}
}
const result = new Attribute({
type,
values
})
return result
}
/**
* Given an object of attribute types mapping to attribute values, construct
* a set of Attributes.
*
* @param {object} obj Each key is an attribute type, and each value is an
* attribute value or set of values.
*
* @returns {Attribute[]}
*
* @throws If an attribute cannot be constructed correctly.
*/
static fromObject (obj) {
const attributes = []
for (const [key, value] of Object.entries(obj)) {
if (Array.isArray(value) === true) {
attributes.push(new Attribute({
type: key,
values: value
}))
} else {
attributes.push(new Attribute({
type: key,
values: [value]
}))
}
}
return attributes
}
/**
* Determine if an object represents an {@link Attribute}.
*
* @param {object} attr The object to check. It can be an instance of
* {@link Attribute} or a plain JavaScript object that looks like an
* {@link Attribute} and can be passed to the constructor to create one.
*
* @returns {boolean}
*/
static isAttribute (attr) {
if (typeof attr !== 'object') {
return false
}
if (Object.prototype.toString.call(attr) === '[object LdapAttribute]') {
return true
}
const typeOk = typeof attr.type === 'string'
let valuesOk = Array.isArray(attr.values)
if (valuesOk === true) {
for (const val of attr.values) {
if (typeof val !== 'string' && Buffer.isBuffer(val) === false) {
valuesOk = false
break
}
}
}
if (typeOk === true && valuesOk === true) {
return true
}
return false
}
}
module.exports = Attribute
/**
* Determine the encoding for values based upon whether the binary
* option is set on the attribute.
*
* @param {string} type
*
* @returns {string} Either "utf8" for a plain string value, or "base64" for
* a binary attribute.
*
* @private
*/
function _bufferEncoding (type) {
return /;binary$/.test(type) ? 'base64' : 'utf8'
}