wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
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
'use strict'
 
const warning = require('./deprecations')
const RDN = require('./rdn')
const parseString = require('./utils/parse-string')
 
/**
 * Implements distinguished name strings as described in
 * https://www.rfc-editor.org/rfc/rfc4514 as an object.
 * This is the primary implementation for parsing and generating DN strings.
 *
 * @example
 * const dn = new DN({rdns: [{cn: 'jdoe', givenName: 'John'}] })
 * dn.toString() // 'cn=jdoe+givenName=John'
 */
class DN {
  #rdns = []
 
  /**
   * @param {object} input
   * @param {RDN[]} [input.rdns=[]] A set of RDN objects that define the DN.
   * Remember that DNs are in reverse domain order. Thus, the target RDN must
   * be the first item and the top-level RDN the last item.
   *
   * @throws When the provided `rdns` array is invalid.
   */
  constructor ({ rdns = [] } = {}) {
    if (Array.isArray(rdns) === false) {
      throw Error('rdns must be an array')
    }
 
    const hasNonRdn = rdns.some(
      r => RDN.isRdn(r) === false
    )
    if (hasNonRdn === true) {
      throw Error('rdns must be an array of RDN objects')
    }
 
    Array.prototype.push.apply(
      this.#rdns,
      rdns.map(r => {
        if (Object.prototype.toString.call(r) === '[object LdapRdn]') {
          return r
        }
        return new RDN(r)
      })
    )
  }
 
  get [Symbol.toStringTag] () {
    return 'LdapDn'
  }
 
  /**
   * The number of RDNs that make up the DN.
   *
   * @returns {number}
   */
  get length () {
    return this.#rdns.length
  }
 
  /**
   * Determine if the current instance is the child of another DN instance or
   * DN string.
   *
   * @param {DN|string} dn
   *
   * @returns {boolean}
   */
  childOf (dn) {
    if (typeof dn === 'string') {
      const parsedDn = DN.fromString(dn)
      return parsedDn.parentOf(this)
    }
    return dn.parentOf(this)
  }
 
  /**
   * Get a new instance that is a replica of the current instance.
   *
   * @returns {DN}
   */
  clone () {
    return new DN({ rdns: this.#rdns })
  }
 
  /**
   * Determine if the instance is equal to another DN.
   *
   * @param {DN|string} dn
   *
   * @returns {boolean}
   */
  equals (dn) {
    if (typeof dn === 'string') {
      const parsedDn = DN.fromString(dn)
      return parsedDn.equals(this)
    }
 
    if (this.length !== dn.length) return false
 
    for (let i = 0; i < this.length; i += 1) {
      if (this.#rdns[i].equals(dn.rdnAt(i)) === false) {
        return false
      }
    }
 
    return true
  }
 
  /**
   * @deprecated Use .toString() instead.
   *
   * @returns {string}
   */
  format () {
    warning.emit('LDAP_DN_DEP_002')
    return this.toString()
  }
 
  /**
   * Determine if the instance has any RDNs defined.
   *
   * @returns {boolean}
   */
  isEmpty () {
    return this.#rdns.length === 0
  }
 
  /**
   * Get a DN representation of the parent of this instance.
   *
   * @returns {DN|undefined}
   */
  parent () {
    if (this.length === 0) return undefined
    const save = this.shift()
    const dn = new DN({ rdns: this.#rdns })
    this.unshift(save)
    return dn
  }
 
  /**
   * Determine if the instance is the parent of a given DN instance or DN
   * string.
   *
   * @param {DN|string} dn
   *
   * @returns {boolean}
   */
  parentOf (dn) {
    if (typeof dn === 'string') {
      const parsedDn = DN.fromString(dn)
      return this.parentOf(parsedDn)
    }
 
    if (this.length >= dn.length) {
      // If we have more RDNs in our set then we must be a descendent at least.
      return false
    }
 
    const numberOfElementsDifferent = dn.length - this.length
    for (let i = this.length - 1; i >= 0; i -= 1) {
      const myRdn = this.#rdns[i]
      const theirRdn = dn.rdnAt(i + numberOfElementsDifferent)
      if (myRdn.equals(theirRdn) === false) {
        return false
      }
    }
 
    return true
  }
 
  /**
   * Removes the last RDN from the list and returns it. This alters the
   * instance.
   *
   * @returns {RDN}
   */
  pop () {
    return this.#rdns.pop()
  }
 
  /**
   * Adds a new RDN to the end of the list (i.e. the "top most" RDN in the
   * directory path) and returns the new RDN count.
   *
   * @param {RDN} rdn
   *
   * @returns {number}
   *
   * @throws When the input is not a valid RDN.
   */
  push (rdn) {
    if (Object.prototype.toString.call(rdn) !== '[object LdapRdn]') {
      throw Error('rdn must be a RDN instance')
    }
    return this.#rdns.push(rdn)
  }
 
  /**
   * Return the RDN at the provided index in the list of RDNs associated with
   * this instance.
   *
   * @param {number} index
   *
   * @returns {RDN}
   */
  rdnAt (index) {
    return this.#rdns[index]
  }
 
  /**
   * Reverse the RDNs list such that the first element becomes the last, and
   * the last becomes the first. This is useful when the RDNs were added in the
   * opposite order of how they should have been.
   *
   * This is an in-place operation. The instance is changed as a result of
   * this operation.
   *
   * @returns {DN} The current instance (i.e. this method is chainable).
   */
  reverse () {
    this.#rdns.reverse()
    return this
  }
 
  /**
   * @deprecated Formatting options are not supported.
   */
  setFormat () {
    warning.emit('LDAP_DN_DEP_004')
  }
 
  /**
   * Remove the first RDN from the set of RDNs and return it.
   *
   * @returns {RDN}
   */
  shift () {
    return this.#rdns.shift()
  }
 
  /**
   * Render the DN instance as a spec compliant DN string.
   *
   * @returns {string}
   */
  toString () {
    let result = ''
    for (const rdn of this.#rdns) {
      const rdnString = rdn.toString()
      result += `,${rdnString}`
    }
    return result.substring(1)
  }
 
  /**
   * Adds an RDN to the beginning of the RDN list and returns the new length.
   *
   * @param {RDN} rdn
   *
   * @returns {number}
   *
   * @throws When the RDN is invalid.
   */
  unshift (rdn) {
    if (Object.prototype.toString.call(rdn) !== '[object LdapRdn]') {
      throw Error('rdn must be a RDN instance')
    }
    return this.#rdns.unshift(rdn)
  }
 
  /**
   * Determine if an object is an instance of {@link DN} or is at least
   * a DN-like object. It is safer to perform a `toString` check.
   *
   * @example Valid Instance
   * const dn = new DN()
   * DN.isDn(dn) // true
   *
   * @example DN-like Instance
   * let dn = { rdns: [{name: 'cn', value: 'foo'}] }
   * DN.isDn(dn) // true
   *
   * dn = { rdns: [{cn: 'foo', sn: 'bar'}, {dc: 'example'}, {dc: 'com'}]}
   * DN.isDn(dn) // true
   *
   * @example Preferred Check
   * let dn = new DN()
   * Object.prototype.toString.call(dn) === '[object LdapDn]' // true
   *
   * dn = { rdns: [{name: 'cn', value: 'foo'}] }
   * Object.prototype.toString.call(dn) === '[object LdapDn]' // false
   *
   * @param {object} dn
   * @returns {boolean}
   */
  static isDn (dn) {
    if (Object.prototype.toString.call(dn) === '[object LdapDn]') {
      return true
    }
    if (
      Object.prototype.toString.call(dn) !== '[object Object]' ||
      Array.isArray(dn.rdns) === false
    ) {
      return false
    }
    if (dn.rdns.some(dn => RDN.isRdn(dn) === false) === true) {
      return false
    }
 
    return true
  }
 
  /**
   * Parses a DN string and returns a new {@link DN} instance.
   *
   * @example
   * const dn = DN.fromString('cn=foo,dc=example,dc=com')
   * DN.isDn(dn) // true
   *
   * @param {string} dnString
   *
   * @returns {DN}
   *
   * @throws If the string is not parseable.
   */
  static fromString (dnString) {
    const rdns = parseString(dnString)
    return new DN({ rdns })
  }
}
 
module.exports = DN