2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert-plus')
|
|
|
|
const util = require('util')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var asn1 = require('asn1')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const LDAPMessage = require('./message')
|
|
|
|
const Attribute = require('../attribute')
|
|
|
|
const Protocol = require('../protocol')
|
|
|
|
const lassert = require('../assert')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var BerWriter = asn1.BerWriter
|
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 SearchEntry (options) {
|
|
|
|
options = options || {}
|
|
|
|
assert.object(options)
|
|
|
|
lassert.optionalStringDN(options.objectName)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
options.protocolOp = Protocol.LDAP_REP_SEARCH_ENTRY
|
|
|
|
LDAPMessage.call(this, options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.objectName = options.objectName || null
|
|
|
|
this.setAttributes(options.attributes || [])
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(SearchEntry, LDAPMessage)
|
2015-10-31 17:28:25 +00:00
|
|
|
Object.defineProperties(SearchEntry.prototype, {
|
|
|
|
type: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getType () { return 'SearchEntry' },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
_dn: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getDN () { return this.objectName },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
object: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getObject () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const obj = {
|
2015-10-31 17:28:25 +00:00
|
|
|
dn: this.dn.toString(),
|
|
|
|
controls: []
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
|
|
|
if (a.vals && a.vals.length) {
|
|
|
|
if (a.vals.length > 1) {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[a.type] = a.vals.slice()
|
2015-10-31 17:28:25 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[a.type] = a.vals[0]
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
2011-09-30 17:48:56 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[a.type] = []
|
2011-09-30 17:48:56 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2020-12-07 01:26:27 +00:00
|
|
|
this.controls.forEach(function (element) {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj.controls.push(element.json)
|
|
|
|
})
|
|
|
|
return obj
|
2015-10-31 17:28:25 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
raw: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getRaw () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const obj = {
|
2015-10-31 17:28:25 +00:00
|
|
|
dn: this.dn.toString(),
|
|
|
|
controls: []
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
|
|
|
|
this.attributes.forEach(function (a) {
|
|
|
|
if (a.buffers && a.buffers.length) {
|
|
|
|
if (a.buffers.length > 1) {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[a.type] = a.buffers.slice()
|
2015-10-31 17:28:25 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[a.type] = a.buffers[0]
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
2012-12-07 22:07:53 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[a.type] = []
|
2012-12-07 22:07:53 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2020-12-07 01:26:27 +00:00
|
|
|
this.controls.forEach(function (element) {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj.controls.push(element.json)
|
|
|
|
})
|
|
|
|
return obj
|
2015-10-31 17:28:25 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.addAttribute = function (attr) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!attr || typeof (attr) !== 'object') { throw new TypeError('attr (attribute) required') }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.attributes.push(attr)
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.toObject = function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.object
|
|
|
|
}
|
2011-08-23 00:16:36 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.fromObject = function (obj) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (obj) !== 'object') { throw new TypeError('object required') }
|
2011-08-12 23:37:47 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2019-08-27 21:11:49 +00:00
|
|
|
if (obj.controls) { this.controls = obj.controls }
|
2011-08-12 23:37:47 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (obj.attributes) { obj = obj.attributes }
|
|
|
|
this.attributes = []
|
2011-08-12 23:37:47 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Object.keys(obj).forEach(function (k) {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.attributes.push(new Attribute({ type: k, vals: obj[k] }))
|
|
|
|
})
|
2011-08-12 23:37:47 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-12 23:37:47 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.setAttributes = function (obj) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (obj) !== 'object') { throw new TypeError('object required') }
|
2011-08-04 21:25:12 +00:00
|
|
|
|
|
|
|
if (Array.isArray(obj)) {
|
2012-02-18 08:15:52 +00:00
|
|
|
obj.forEach(function (a) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!Attribute.isAttribute(a)) { throw new TypeError('entry must be an Array of Attributes') }
|
|
|
|
})
|
|
|
|
this.attributes = obj
|
2011-08-04 21:25:12 +00:00
|
|
|
} else {
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2011-08-04 21:25:12 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
self.attributes = []
|
2012-02-18 08:15:52 +00:00
|
|
|
Object.keys(obj).forEach(function (k) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const attr = new Attribute({ type: k })
|
2011-08-04 21:25:12 +00:00
|
|
|
if (Array.isArray(obj[k])) {
|
2012-02-18 08:15:52 +00:00
|
|
|
obj[k].forEach(function (v) {
|
2019-08-27 21:11:49 +00:00
|
|
|
attr.addValue(v.toString())
|
|
|
|
})
|
2011-08-04 21:25:12 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
attr.addValue(obj[k].toString())
|
2011-08-04 21:25:12 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
self.attributes.push(attr)
|
|
|
|
})
|
2011-08-04 21:25:12 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2011-08-04 21:25:12 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.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.objectName = this.objectName.toString()
|
|
|
|
j.attributes = []
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2019-08-27 21:11:49 +00:00
|
|
|
j.attributes.push(a.json || a)
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return j
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.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.objectName = ber.readString()
|
|
|
|
assert.ok(ber.readSequence())
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const end = ber.offset + ber.length
|
2011-08-04 20:32:01 +00:00
|
|
|
while (ber.offset < end) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const a = new Attribute()
|
2019-08-27 21:11:49 +00:00
|
|
|
a.parse(ber)
|
|
|
|
this.attributes.push(a)
|
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
|
|
|
SearchEntry.prototype._toBer = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const formattedObjectName = this.objectName.format({ skipSpace: true })
|
2020-07-04 09:05:59 +00:00
|
|
|
ber.writeString(formattedObjectName)
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.startSequence()
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
// This may or may not be an attribute
|
2019-08-27 21:11:49 +00:00
|
|
|
ber = Attribute.toBer(a, ber)
|
|
|
|
})
|
|
|
|
ber.endSequence()
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return ber
|
|
|
|
}
|
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 = SearchEntry
|