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
'use strict'
 
const LdapMessage = require('../ldap-message')
const { operations } = require('@ldapjs/protocol')
const RECOGNIZED_OIDS = require('./extension-utils/recognized-oids')
 
/**
 * Implements the extension request message as described in
 * https://www.rfc-editor.org/rfc/rfc4511.html#section-4.12.
 *
 * There is a set of supported extension request OIDs supported. Any
 * unrecognized OID will be treated a simple string pair, i.e. both
 * `requestName` and `requestValue` will be assumed to be simple strings.
 */
class ExtensionRequest extends LdapMessage {
  #requestName
  #requestValue
 
  /**
   * @typedef {LdapMessageOptions} ExtensionRequestOptions
   * @property {string} [requestName=''] The name of the extension, i.e.
   * OID for the request.
   * @property {string|object} [requestValue] The value for the request.
   * If `undefined`, no value will be sent. If the request requires a simple
   * string value, provide such a string. For complex valued requests, e.g.
   * for a password modify request, it should be a plain object with the
   * appropriate properties. See the implementation of {@link parseToPojo}
   * for the set of supported objects.
   */
 
  /**
   * @param {ExtensionRequestOptions} [options]
   */
  constructor (options = {}) {
    options.protocolOp = operations.LDAP_REQ_EXTENSION
    super(options)
 
    this.requestName = options.requestName || ''
    this.requestValue = options.requestValue
  }
 
  /**
   * Alias of {@link requestName}.
   *
   * @type {string}
   */
  get _dn () {
    return this.#requestName
  }
 
  /**
   * The name (OID) of the request.
   *
   * @returns {string}
   */
  get requestName () {
    return this.#requestName
  }
 
  /**
   * Set the name for the request. Should be an OID that
   * matches a specification.
   *
   * @param {string} value
   */
  set requestName (value) {
    this.#requestName = value
  }
 
  /**
   * The name of the request type.
   *
   * @type {string}
   */
  get type () {
    return 'ExtensionRequest'
  }
 
  /**
   * The value, if any, for the request.
   *
   * @returns {undefined|string|object} value
   */
  get requestValue () {
    return this.#requestValue
  }
 
  /**
   * Set the value for the request. The value should conform
   * to the specification identified by the {@link requestName}.
   * See the implemenation of {@link parseToPojo} for valid
   * value shapes.
   *
   * @param {undefined|string|object} value
   */
  set requestValue (val) {
    this.#requestValue = val
  }
 
  /**
   * Internal use only.
   *
   * @param {import('@ldapjs/asn1').BerWriter} ber
   *
   * @returns {import('@ldapjs/asn1').BerWriter}
   */
  _toBer (ber) {
    ber.startSequence(operations.LDAP_REQ_EXTENSION)
    ber.writeString(this.requestName, 0x80)
 
    if (this.requestValue) {
      switch (this.requestName) {
        case RECOGNIZED_OIDS.get('CANCEL_REQUEST'): {
          encodeCancelRequest({ ber, requestValue: this.requestValue })
          break
        }
 
        case RECOGNIZED_OIDS.get('PASSWORD_MODIFY'): {
          encodePasswordModify({
            ber,
            requestValue: this.requestValue
          })
          break
        }
 
        default: {
          // We assume the value is a plain string since
          // we do not recognize the request OID, or we know
          // that the OID uses a plain string value.
          ber.writeString(this.requestValue, 0x81)
        }
      }
    }
 
    ber.endSequence()
    return ber
  }
 
  /**
   * Internal use only.
   *
   * @param {object}
   *
   * @returns {object}
   */
  _pojo (obj = {}) {
    obj.requestName = this.requestName
    obj.requestValue = this.requestValue
    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_EXTENSION) {
      const op = protocolOp.toString(16).padStart(2, '0')
      throw Error(`found wrong protocol operation: 0x${op}`)
    }
 
    // While the requestName is an OID, it is not an
    // _encoded_ OID. It is a plain string. So we do
    // not use `.readOID` here.
    const requestName = ber.readString(0x80)
    if (ber.peek() !== 0x81) {
      // There is not a request value present, so we just
      // return an empty value representation.
      return { protocolOp, requestName }
    }
 
    let requestValue
    switch (requestName) {
      case RECOGNIZED_OIDS.get('CANCEL_REQUEST'): {
        requestValue = readCancelRequest(ber)
        break
      }
 
      case RECOGNIZED_OIDS.get('PASSWORD_MODIFY'): {
        requestValue = readPasswordModify(ber)
        break
      }
 
      default: {
        // We will assume it is a plain string value
        // since we do not recognize the OID, or we know
        // that the OID uses a plain string value.
        requestValue = ber.readString(0x81)
        break
      }
    }
 
    return { protocolOp, requestName, requestValue }
  }
 
  /**
   * A list of EXTENDED operation OIDs that this module
   * recognizes. Key names are named according to the common name
   * of the extension. Key values are the OID associated with that
   * extension. For example, key `PASSWORD_MODIFY` corresponds to
   * OID `1.3.6.1.4.1.4203.1.11.1`.
   *
   * @returns {Map<string, string>}
   */
  static recognizedOIDs () {
    return RECOGNIZED_OIDS
  }
}
 
module.exports = ExtensionRequest
 
/**
 * @param {object} input
 * @param {@import('@ldapjs/asn1').BerWriter} input.ber
 * @param {object} requestValue
 */
function encodeCancelRequest ({ ber, requestValue }) {
  ber.startSequence(0x81)
  ber.startSequence()
  ber.writeInt(requestValue)
  ber.endSequence()
  ber.endSequence()
}
 
/**
 * @param {@import('@ldapjs/asn1').BerReader} ber
 * @returns {number}
 */
function readCancelRequest (ber) {
  ber.readSequence(0x81)
  ber.readSequence()
  return ber.readInt()
}
 
/**
 * @param {object} input
 * @param {@import('@ldapjs/asn1').BerWriter} input.ber
 * @param {object} requestValue
 */
function encodePasswordModify ({ ber, requestValue }) {
  // start the value sequence
  ber.startSequence(0x81)
  // start the generic packed sequence
  ber.startSequence()
  if (requestValue.userIdentity) {
    ber.writeString(requestValue.userIdentity, 0x80)
  }
  if (requestValue.oldPassword) {
    ber.writeString(requestValue.oldPassword, 0x81)
  }
  if (requestValue.newPassword) {
    ber.writeString(requestValue.newPassword, 0x82)
  }
  ber.endSequence()
  ber.endSequence()
}
 
/**
 * @param {@import('@ldapjs/asn1').BerReader} ber
 * @returns {object}
 */
function readPasswordModify (ber) {
  // advance to the embedded sequence
  ber.readSequence(0x81)
  // advance to the value of the embedded sequence
  ber.readSequence()
  let userIdentity
  if (ber.peek() === 0x80) {
    userIdentity = ber.readString(0x80)
  }
  let oldPassword
  if (ber.peek() === 0x81) {
    oldPassword = ber.readString(0x81)
  }
  let newPassword
  if (ber.peek() === 0x82) {
    newPassword = ber.readString(0x82)
  }
  return { userIdentity, oldPassword, newPassword }
}