|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { BerReader, BerWriter } = require('@ldapjs/asn1') |
| 4 | +const warning = require('./deprecations') |
| 5 | + |
| 6 | +/** |
| 7 | + * Implements a base LDAP message as defined in |
| 8 | + * https://www.rfc-editor.org/rfc/rfc4511.html#section-4.1.1. |
| 9 | + */ |
| 10 | +class LdapMessage { |
| 11 | + #messageId = 0 |
| 12 | + #protocolOp |
| 13 | + #controls = [] |
| 14 | + |
| 15 | + /** |
| 16 | + * @param {object} [options] |
| 17 | + * @param {number} [options.messageId=1] An identifier for the message. |
| 18 | + * @param {number} [options.protocolOp] The tag for the message operation. |
| 19 | + * @param {object[]} [options.controls] A set of LDAP controls to send with |
| 20 | + * the message. See the `@ldapjs/controls` package. |
| 21 | + */ |
| 22 | + constructor (options = {}) { |
| 23 | + this.#messageId = parseInt(options.messageId || options.messageID || '1', 10) |
| 24 | + if (options.messageID) { |
| 25 | + warning.emit('LDAP_MESSAGE_DEP_001') |
| 26 | + } |
| 27 | + |
| 28 | + if (options.protocolOp) { |
| 29 | + this.#protocolOp = options.protocolOp |
| 30 | + } |
| 31 | + |
| 32 | + if (Array.isArray(options.controls)) { |
| 33 | + Array.prototype.push.apply(this.#controls, options.controls) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + get [Symbol.toStringTag] () { |
| 38 | + return 'LdapMessage' |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * The message identifier. |
| 43 | + * |
| 44 | + * @type {number} |
| 45 | + */ |
| 46 | + get id () { return this.#messageId } |
| 47 | + |
| 48 | + /** |
| 49 | + * Message type specific. Each message type must implement a `_dn` property |
| 50 | + * that provides this value. |
| 51 | + * |
| 52 | + * @type {string} |
| 53 | + */ |
| 54 | + get dn () { |
| 55 | + return this._dn |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * The name of the message class. |
| 60 | + * |
| 61 | + * @type {string} |
| 62 | + */ |
| 63 | + get type () { return 'LdapMessage' } |
| 64 | + |
| 65 | + /** |
| 66 | + * Use {@link pojo} instead. |
| 67 | + * |
| 68 | + * @deprecated |
| 69 | + */ |
| 70 | + get json () { |
| 71 | + warning.emit('LDAP_MESSAGE_DEP_002') |
| 72 | + return this.pojo |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A serialized representation of the message as a plain JavaScript object. |
| 77 | + * Specific message types must implement the `_pojo(obj)` method. The passed |
| 78 | + * in `obj` must be extended with the specific message's unique properties |
| 79 | + * and returned as the result. |
| 80 | + * |
| 81 | + * @returns {object} |
| 82 | + */ |
| 83 | + get pojo () { |
| 84 | + let result = { |
| 85 | + messageId: this.id, |
| 86 | + protocolOp: this.#protocolOp, |
| 87 | + type: this.type |
| 88 | + } |
| 89 | + |
| 90 | + if (typeof this._pojo === 'function') { |
| 91 | + result = this._pojo(result) |
| 92 | + } |
| 93 | + |
| 94 | + result.controls = this.#controls |
| 95 | + |
| 96 | + return result |
| 97 | + } |
| 98 | + |
| 99 | + addControl (control) { |
| 100 | + this.#controls.push(control) |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Converts an {@link LdapMessage} object into a set of BER bytes that can |
| 105 | + * be sent across the wire. Specific message implementations must implement |
| 106 | + * the `_toBer(ber)` method. This method will write its unique sequence(s) |
| 107 | + * to the passed in `ber` object. |
| 108 | + * |
| 109 | + * @returns {import('@ldapjs/asn1').BerReader} |
| 110 | + */ |
| 111 | + toBer () { |
| 112 | + if (typeof this._toBer !== 'function') { |
| 113 | + throw Error(`${this.type} does not implement _toBer`) |
| 114 | + } |
| 115 | + |
| 116 | + const writer = new BerWriter() |
| 117 | + writer.startSequence() |
| 118 | + writer.writeInt(this.id) |
| 119 | + |
| 120 | + this._toBer(writer) |
| 121 | + |
| 122 | + if (Array.isArray(this.#controls) === true && this.#controls.length > 0) { |
| 123 | + writer.startSequence(0xa0) |
| 124 | + this.#controls.forEach(function (c) { |
| 125 | + c.toBer(writer) |
| 126 | + }) |
| 127 | + writer.endSequence() |
| 128 | + } |
| 129 | + |
| 130 | + writer.endSequence() |
| 131 | + return new BerReader(writer.buffer) |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Serializes the message into a JSON representation. |
| 136 | + * |
| 137 | + * @returns {string} |
| 138 | + */ |
| 139 | + toString () { |
| 140 | + return JSON.stringify(this.pojo) |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Parses a BER into a message object. The offset of the BER _must_ point |
| 145 | + * to the start of an LDAP Message sequence. That is, the first few bytes |
| 146 | + * must indicate: |
| 147 | + * |
| 148 | + * 1. a sequence tag and how many bytes are in that sequence |
| 149 | + * 2. an integer representing the message identifier |
| 150 | + * 3. a protocol operation, e.g. BindRequest, and the number of bytes in |
| 151 | + * that operation |
| 152 | + * |
| 153 | + * @param {import('@ldapjs/asn1').BerReader} ber |
| 154 | + * |
| 155 | + * @returns {LdapMessage} |
| 156 | + */ |
| 157 | + static parse (ber) { |
| 158 | + // We must require here because `parseToMessage` imports subclasses |
| 159 | + // that need `LdapMessage` to be defined. If we try importing earlier, |
| 160 | + // then `LdapMessage` will not be available and we will get errors about |
| 161 | + // trying to subclass null objects. |
| 162 | + return require('./parse-to-message')(ber) |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * When invoked on specific message types, e.g. {@link BindRequest}, this |
| 167 | + * method will parse a BER into a plain JavaScript object that is usable as |
| 168 | + * an options object for constructing that specific message object. |
| 169 | + * |
| 170 | + * @param {import('@ldapjs/asn1').BerReader} ber A BER to parse. The reader |
| 171 | + * offset must point to the start of a valid sequence, i.e. the "tag" byte |
| 172 | + * in the TLV tuple, that represents the message to be parsed. For example, |
| 173 | + * in a {@link BindRequest} the starting sequence and message identifier must |
| 174 | + * already be read such that the offset is at the protocol operation sequence |
| 175 | + * byte. |
| 176 | + */ |
| 177 | + static parseToPojo (ber) { |
| 178 | + throw Error('Use LdapMessage.parse, or a specific message type\'s parseToPojo, instead.') |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +module.exports = LdapMessage |
0 commit comments