2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
var assert = require('assert-plus')
|
|
|
|
var util = require('util')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
var LDAPMessage = require('./message')
|
|
|
|
var Protocol = require('../protocol')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function ExtendedRequest (options) {
|
|
|
|
options = options || {}
|
|
|
|
assert.object(options)
|
|
|
|
assert.optionalString(options.requestName)
|
2015-10-31 17:28:25 +00:00
|
|
|
if (options.requestValue &&
|
|
|
|
!(Buffer.isBuffer(options.requestValue) ||
|
|
|
|
typeof (options.requestValue) === 'string')) {
|
2019-08-27 21:11:49 +00:00
|
|
|
throw new TypeError('options.requestValue must be a buffer or a string')
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
options.protocolOp = Protocol.LDAP_REQ_EXTENSION
|
|
|
|
LDAPMessage.call(this, options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.requestName = options.requestName || ''
|
|
|
|
this.requestValue = options.requestValue
|
2019-08-27 12:04:45 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (Buffer.isBuffer(this.requestValue)) {
|
|
|
|
this.requestValueBuffer = this.requestValue
|
2019-08-27 12:04:45 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.requestValueBuffer = Buffer.from(this.requestValue || '', 'utf8')
|
2019-08-27 12:04:45 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(ExtendedRequest, LDAPMessage)
|
2015-10-31 17:28:25 +00:00
|
|
|
Object.defineProperties(ExtendedRequest.prototype, {
|
|
|
|
type: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getType () { return 'ExtendedRequest' },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
_dn: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getDN () { return this.requestName },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
name: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getName () { return this.requestName },
|
|
|
|
set: function setName (val) {
|
|
|
|
assert.string(val)
|
|
|
|
this.requestName = val
|
2015-10-31 17:28:25 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
value: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getValue () { return this.requestValue },
|
|
|
|
set: function setValue (val) {
|
|
|
|
if (!(Buffer.isBuffer(val) || typeof (val) === 'string')) { throw new TypeError('value must be a buffer or a string') }
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (Buffer.isBuffer(val)) {
|
|
|
|
this.requestValueBuffer = val
|
2019-08-27 12:04:45 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.requestValueBuffer = Buffer.from(val, 'utf8')
|
2019-08-27 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.requestValue = val
|
2015-10-31 17:28:25 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
2019-08-27 12:04:45 +00:00
|
|
|
},
|
|
|
|
valueBuffer: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getValueBuffer () {
|
|
|
|
return this.requestValueBuffer
|
2019-08-27 12:04:45 +00:00
|
|
|
},
|
2019-08-27 21:11:49 +00:00
|
|
|
set: function setValueBuffer (val) {
|
|
|
|
if (!Buffer.isBuffer(val)) { throw new TypeError('valueBuffer must be a buffer') }
|
2019-08-27 12:04:45 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.value = val
|
2019-08-27 12:04:45 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
ExtendedRequest.prototype._parse = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.requestName = ber.readString(0x80)
|
2019-08-27 12:04:45 +00:00
|
|
|
if (ber.peek() === 0x81) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.requestValueBuffer = ber.readString(0x81, true)
|
|
|
|
this.requestValue = this.requestValueBuffer.toString('utf8')
|
2019-08-27 12:04:45 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
ExtendedRequest.prototype._toBer = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeString(this.requestName, 0x80)
|
2015-04-26 03:36:36 +00:00
|
|
|
if (Buffer.isBuffer(this.requestValue)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeBuffer(this.requestValue, 0x81)
|
2015-04-26 03:36:36 +00:00
|
|
|
} else if (typeof (this.requestValue) === 'string') {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeString(this.requestValue, 0x81)
|
2014-01-17 17:52:26 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return ber
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
ExtendedRequest.prototype._json = function (j) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(j)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
j.requestName = this.requestName
|
|
|
|
j.requestValue = (Buffer.isBuffer(this.requestValue))
|
|
|
|
? this.requestValue.toString('hex') : this.requestValue
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return j
|
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
module.exports = ExtendedRequest
|