node-ldapjs/lib/messages/result.js

122 lines
2.9 KiB
JavaScript
Raw Normal View History

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
// var asn1 = require('asn1')
2011-08-04 20:32:01 +00:00
2020-10-31 21:07:32 +00:00
const dtrace = require('../dtrace')
const LDAPMessage = require('./message')
const Protocol = require('../protocol')
2011-11-15 18:49:23 +00:00
/// --- Globals
2011-08-04 20:32:01 +00:00
// var Ber = asn1.Ber
// var BerWriter = asn1.BerWriter
2011-08-04 20:32:01 +00:00
/// --- API
2011-08-04 20:32:01 +00:00
function LDAPResult (options) {
options = options || {}
assert.object(options)
assert.optionalNumber(options.status)
assert.optionalString(options.matchedDN)
assert.optionalString(options.errorMessage)
assert.optionalArrayOfString(options.referrals)
2011-08-04 20:32:01 +00:00
LDAPMessage.call(this, options)
2011-08-04 20:32:01 +00:00
this.status = options.status || 0 // LDAP SUCCESS
this.matchedDN = options.matchedDN || ''
this.errorMessage = options.errorMessage || ''
this.referrals = options.referrals || []
2011-08-04 20:32:01 +00:00
this.connection = options.connection || null
2011-08-04 20:32:01 +00:00
}
util.inherits(LDAPResult, LDAPMessage)
Object.defineProperties(LDAPResult.prototype, {
type: {
get: function getType () { return 'LDAPResult' },
configurable: false
}
})
2011-08-04 20:32:01 +00:00
LDAPResult.prototype.end = function (status) {
assert.ok(this.connection)
2011-08-04 20:32:01 +00:00
if (typeof (status) === 'number') { this.status = status }
2011-08-04 20:32:01 +00:00
2020-10-31 21:07:32 +00:00
const ber = this.toBer()
this.log.debug('%s: sending: %j', this.connection.ldap.id, this.json)
2011-08-04 20:32:01 +00:00
try {
2020-10-31 21:07:32 +00:00
const self = this
this.connection.write(ber)
2011-11-15 18:49:23 +00:00
if (self._dtraceOp && self._dtraceId) {
dtrace.fire('server-' + self._dtraceOp + '-done', function () {
2020-10-31 21:07:32 +00:00
const c = self.connection || { ldap: {} }
2011-11-15 18:49:23 +00:00
return [
self._dtraceId || 0,
(c.remoteAddress || ''),
c.ldap.bindDN ? c.ldap.bindDN.toString() : '',
(self.requestDN ? self.requestDN.toString() : ''),
status || self.status,
self.errorMessage
]
})
2011-11-15 18:49:23 +00:00
}
} catch (e) {
this.log.warn(e, '%s failure to write message %j',
this.connection.ldap.id, this.json)
}
}
2011-08-04 20:32:01 +00:00
LDAPResult.prototype._parse = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
this.status = ber.readEnumeration()
this.matchedDN = ber.readString()
this.errorMessage = ber.readString()
2011-08-04 20:32:01 +00:00
2020-10-31 21:07:32 +00:00
const t = ber.peek()
2011-08-04 20:32:01 +00:00
if (t === Protocol.LDAP_REP_REFERRAL) {
2020-10-31 21:07:32 +00:00
const end = ber.offset + ber.length
while (ber.offset < end) { this.referrals.push(ber.readString()) }
2011-08-04 20:32:01 +00:00
}
return true
}
2011-08-04 20:32:01 +00:00
LDAPResult.prototype._toBer = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
ber.writeEnumeration(this.status)
ber.writeString(this.matchedDN || '')
ber.writeString(this.errorMessage || '')
2011-08-04 20:32:01 +00:00
if (this.referrals.length) {
ber.startSequence(Protocol.LDAP_REP_REFERRAL)
ber.writeStringArray(this.referrals)
ber.endSequence()
2011-08-04 20:32:01 +00:00
}
return ber
}
2011-08-04 20:32:01 +00:00
LDAPResult.prototype._json = function (j) {
assert.ok(j)
2011-08-04 20:32:01 +00:00
j.status = this.status
j.matchedDN = this.matchedDN
j.errorMessage = this.errorMessage
j.referrals = this.referrals
return j
}
/// --- Exports
module.exports = LDAPResult