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
'use strict'
 
const LdapMessage = require('../ldap-message')
const Protocol = require('@ldapjs/protocol')
const { BerTypes } = require('@ldapjs/asn1')
 
/**
 * Implements the bind request message as described in
 * https://www.rfc-editor.org/rfc/rfc4511.html#section-4.2.
 *
 * The bind request is further defined by:
 * https://www.rfc-editor.org/rfc/rfc4513#section-5.
 */
class BindRequest extends LdapMessage {
  static SIMPLE_BIND = 'simple'
  static SASL_BIND = 'sasl'
 
  #version = 0x03
  #name
  #authentication = BindRequest.SIMPLE_BIND
  #credentials = ''
 
  /**
   * @typedef {LdapMessageOptions} BindRequestOptions
   * @property {number} [version=3] Version of the protocol being used.
   * @property {string} [name=null] The "username" (dn) to connect with.
   * @property {string} [authentication='simple'] The authentication
   * mechanism to use. Currently, only `simple` is supported.
   * @property {string} [credentials=''] The password to use.
   */
 
  /**
   * @param {BindRequestOptions} [options]
   */
  constructor (options = {}) {
    options.protocolOp = Protocol.operations.LDAP_REQ_BIND
    super(options)
 
    const {
      version = 0x03,
      name = null,
      authentication = BindRequest.SIMPLE_BIND,
      credentials = ''
    } = options
    this.#version = version
    this.#name = name
    this.#authentication = authentication
    this.#credentials = credentials
  }
 
  /**
   * The authentication credentials for the request.
   *
   * @returns {string}
   */
  get credentials () {
    return this.#credentials
  }
 
  /**
   * The DN, or "username", that is to be used in the bind request.
   *
   * @type {string}
   */
  get name () {
    return this.#name
  }
 
  /**
   * The name of the request type.
   *
   * @type {string}
   */
  get type () {
    return 'BindRequest'
  }
 
  /**
   * The version number that the bind request conforms to.
   *
   * @type {number}
   */
  get version () {
    return this.#version
  }
 
  /**
   * Use {@link name} instead.
   *
   * @type {string}
   */
  get _dn () {
    return this.#name
  }
 
  /**
   * Internal use only.
   *
   * @param {import('@ldapjs/asn1').BerWriter} ber
   *
   * @returns {import('@ldapjs/asn1').BerWriter}
   */
  _toBer (ber) {
    ber.startSequence(Protocol.operations.LDAP_REQ_BIND)
    ber.writeInt(this.#version)
    ber.writeString(this.#name || '')
    // TODO add support for SASL et al
    ber.writeString(this.#credentials || '', BerTypes.Context)
    ber.endSequence()
    return ber
  }
 
  /**
   * Internal use only.
   *
   * @param {object}
   *
   * @returns {object}
   */
  _pojo (obj = {}) {
    obj.version = this.#version
    obj.name = this.#name
    obj.authenticationType = this.#authentication
    obj.credentials = this.#credentials
    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 !== Protocol.operations.LDAP_REQ_BIND) {
      const op = protocolOp.toString(16).padStart(2, '0')
      throw Error(`found wrong protocol operation: 0x${op}`)
    }
 
    const version = ber.readInt()
    const name = ber.readString()
 
    const tag = ber.peek()
 
    // TODO: add support for SASL et al
    if (tag !== BerTypes.Context) {
      // Currently only support 0x80. To support SASL, must support 0x83.
      const authType = tag.toString(16).padStart(2, '0')
      throw Error(`authentication 0x${authType} not supported`)
    }
 
    const authentication = BindRequest.SIMPLE_BIND
    const credentials = ber.readString(BerTypes.Context)
 
    return {
      protocolOp,
      version,
      name,
      authentication,
      credentials
    }
  }
}
 
module.exports = BindRequest