1 |
|
2 |
|
3 | const assert = require('assert-plus')
|
4 | const util = require('util')
|
5 |
|
6 | const LDAPMessage = require('./message')
|
7 | const Protocol = require('../protocol')
|
8 |
|
9 |
|
10 |
|
11 | function ExtendedRequest (options) {
|
12 | options = options || {}
|
13 | assert.object(options)
|
14 | assert.optionalString(options.requestName)
|
15 | if (options.requestValue &&
|
16 | !(Buffer.isBuffer(options.requestValue) ||
|
17 | typeof (options.requestValue) === 'string')) {
|
18 | throw new TypeError('options.requestValue must be a buffer or a string')
|
19 | }
|
20 |
|
21 | options.protocolOp = Protocol.LDAP_REQ_EXTENSION
|
22 | LDAPMessage.call(this, options)
|
23 |
|
24 | this.requestName = options.requestName || ''
|
25 | this.requestValue = options.requestValue
|
26 |
|
27 | if (Buffer.isBuffer(this.requestValue)) {
|
28 | this.requestValueBuffer = this.requestValue
|
29 | } else {
|
30 | this.requestValueBuffer = Buffer.from(this.requestValue || '', 'utf8')
|
31 | }
|
32 | }
|
33 | util.inherits(ExtendedRequest, LDAPMessage)
|
34 | Object.defineProperties(ExtendedRequest.prototype, {
|
35 | type: {
|
36 | get: function getType () { return 'ExtendedRequest' },
|
37 | configurable: false
|
38 | },
|
39 | _dn: {
|
40 | get: function getDN () { return this.requestName },
|
41 | configurable: false
|
42 | },
|
43 | name: {
|
44 | get: function getName () { return this.requestName },
|
45 | set: function setName (val) {
|
46 | assert.string(val)
|
47 | this.requestName = val
|
48 | },
|
49 | configurable: false
|
50 | },
|
51 | value: {
|
52 | get: function getValue () { return this.requestValue },
|
53 | set: function setValue (val) {
|
54 | if (!(Buffer.isBuffer(val) || typeof (val) === 'string')) { throw new TypeError('value must be a buffer or a string') }
|
55 |
|
56 | if (Buffer.isBuffer(val)) {
|
57 | this.requestValueBuffer = val
|
58 | } else {
|
59 | this.requestValueBuffer = Buffer.from(val, 'utf8')
|
60 | }
|
61 |
|
62 | this.requestValue = val
|
63 | },
|
64 | configurable: false
|
65 | },
|
66 | valueBuffer: {
|
67 | get: function getValueBuffer () {
|
68 | return this.requestValueBuffer
|
69 | },
|
70 | set: function setValueBuffer (val) {
|
71 | if (!Buffer.isBuffer(val)) { throw new TypeError('valueBuffer must be a buffer') }
|
72 |
|
73 | this.value = val
|
74 | },
|
75 | configurable: false
|
76 | }
|
77 | })
|
78 |
|
79 | ExtendedRequest.prototype._parse = function (ber) {
|
80 | assert.ok(ber)
|
81 |
|
82 | this.requestName = ber.readString(0x80)
|
83 | if (ber.peek() === 0x81) {
|
84 | this.requestValueBuffer = ber.readString(0x81, true)
|
85 | this.requestValue = this.requestValueBuffer.toString('utf8')
|
86 | }
|
87 |
|
88 | return true
|
89 | }
|
90 |
|
91 | ExtendedRequest.prototype._toBer = function (ber) {
|
92 | assert.ok(ber)
|
93 |
|
94 | ber.writeString(this.requestName, 0x80)
|
95 | if (Buffer.isBuffer(this.requestValue)) {
|
96 | ber.writeBuffer(this.requestValue, 0x81)
|
97 | } else if (typeof (this.requestValue) === 'string') {
|
98 | ber.writeString(this.requestValue, 0x81)
|
99 | }
|
100 |
|
101 | return ber
|
102 | }
|
103 |
|
104 | ExtendedRequest.prototype._json = function (j) {
|
105 | assert.ok(j)
|
106 |
|
107 | j.requestName = this.requestName
|
108 | j.requestValue = (Buffer.isBuffer(this.requestValue))
|
109 | ? this.requestValue.toString('hex')
|
110 | : this.requestValue
|
111 |
|
112 | return j
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 | module.exports = ExtendedRequest
|