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
'use strict'
 
const { operations } = require('@ldapjs/protocol')
const { DN } = require('@ldapjs/dn')
const LdapMessage = require('../ldap-message')
 
/**
 * Implements the compare request message as described in
 * https://www.rfc-editor.org/rfc/rfc4511.html#section-4.10.
 */
class CompareRequest extends LdapMessage {
  #attribute
  #entry
  #value
 
  /**
   * @typedef {LdapMessageOptions} CompareRequestOptions
   * @property {string|null} [attribute] The attribute name to compare
   * against.
   * @property {string} [entry] The target LDAP entity whose attribute
   * will be compared.
   * @property {string} [value] The value of the attribute to compare.
   */
 
  /**
   * @param {CompareRequestOptions} [options]
   */
  constructor (options = {}) {
    options.protocolOp = operations.LDAP_REQ_COMPARE
    super(options)
 
    this.attribute = options.attribute || ''
    this.entry = options.entry || null
    this.value = options.value || ''
  }
 
  /**
   * The property of an LDAP entry to compare against.
   *
   * @returns {string}
   */
  get attribute () {
    return this.#attribute
  }
 
  /**
   * Define the LDAP entry property to compare against.
   *
   * @param {string} value
   */
  set attribute (value) {
    this.#attribute = value
  }
 
  /**
   * The LDAP entry that will be inspected.
   *
   * @returns {string | null}
   */
  get entry () {
    return this.#entry ?? null
  }
 
  /**
   * Define the LDAP entity to inspect.
   *
   * @param {string | null} value
   */
  set entry (value) {
    if (value === null) return
    if (typeof value === 'string') {
      this.#entry = DN.fromString(value)
    } else if (Object.prototype.toString.call(value) === '[object LdapDn]') {
      this.#entry = value
    } else {
      throw Error('entry must be a valid DN string or instance of LdapDn')
    }
  }
 
  /**
   * The name of the request type.
   *
   * @type {string}
   */
  get type () {
    return 'CompareRequest'
  }
 
  /**
   * The value the attribute should be set to.
   *
   * @returns {string}
   */
  get value () {
    return this.#value
  }
 
  /**
   * Define the value the attribute should match.
   *
   * @param {string} value
   */
  set value (value) {
    this.#value = value
  }
 
  get _dn () {
    return this.#entry
  }
 
  /**
   * Internal use only.
   *
   * @param {import('@ldapjs/asn1').BerWriter} ber
   *
   * @returns {import('@ldapjs/asn1').BerWriter}
   */
  _toBer (ber) {
    ber.startSequence(operations.LDAP_REQ_COMPARE)
 
    ber.writeString(this.#entry.toString())
    ber.startSequence()
    ber.writeString(this.#attribute)
    ber.writeString(this.#value)
    ber.endSequence()
 
    ber.endSequence()
    return ber
  }
 
  /**
   * Internal use only.
   *
   * @param {object}
   *
   * @returns {object}
   */
  _pojo (obj = {}) {
    obj.attribute = this.#attribute
    obj.entry = this.#entry ? this.#entry.toString() : null
    obj.value = this.#value
    return obj
  }
 
  /**
   * Implements the standardized `parseToPojo` method.
   *
   * @see LdapMessage.parseToPojo
   *
   * @param {import('@ldapjs/asn1').BerReader} ber
   */
  static parseToPojo (ber) {
    const protocolOp = ber.readSequence()
    if (protocolOp !== operations.LDAP_REQ_COMPARE) {
      const op = protocolOp.toString(16).padStart(2, '0')
      throw Error(`found wrong protocol operation: 0x${op}`)
    }
 
    const entry = ber.readString()
    ber.readSequence()
    const attribute = ber.readString()
    const value = ber.readString()
 
    return {
      protocolOp,
      entry,
      attribute,
      value
    }
  }
}
 
module.exports = CompareRequest