add support for entry change notification control

This commit is contained in:
Yunong Xiao 2011-12-19 16:52:48 -08:00
parent ab94de8126
commit cbf809c856
7 changed files with 196 additions and 1 deletions

View File

@ -0,0 +1,93 @@
var assert = require('assert');
var util = require('util');
var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function EntryChangeNotificationControl(options) {
if (!options)
options = {};
options.type = EntryChangeNotificationControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
this.parse(options.value);
} else if (typeof(options.value) === 'object') {
this._value = options.value;
} else {
throw new TypeError('options.value must be a Buffer or Object');
}
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function() {
return self._value || {};
});
}
util.inherits(EntryChangeNotificationControl, Control);
module.exports = EntryChangeNotificationControl;
EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
var ber = new BerReader(buffer);
if (ber.readSequence()) {
this._value = {
changeType: ber.readInt()
};
// if the operation was moddn, then parse the optional previousDN attr
if (this._value.changeType === 8)
this._value.previousDN = ber.readString();
this._value.changeNumber = ber.readInt();
return true;
}
return false;
};
EntryChangeNotificationControl.prototype._toBer = function(ber) {
assert.ok(ber);
if (!this._value)
return;
var writer = new BerWriter();
writer.startSequence();
writer.writeInt(this.value.changeType);
if (this.value.previousDN)
writer.writeString(this.value.previousDN);
writer.writeInt(parseInt(this.value.changeNumber, 10));
writer.endSequence();
ber.writeBuffer(writer.buffer, 0x04);
};
EntryChangeNotificationControl.prototype._json = function(obj) {
obj.controlValue = this.value;
return obj;
};
EntryChangeNotificationControl.OID = '2.16.840.1.113730.3.4.7';

View File

@ -3,6 +3,8 @@
var assert = require('assert');
var Control = require('./control');
var EntryChangeNotificationControl =
require('./entry_change_notification_control');
var PersistentSearchControl = require('./persistent_search_control');
@ -42,6 +44,12 @@ module.exports = {
value: value
});
break;
case EntryChangeNotificationControl.OID:
control = new EntryChangeNotificationControl({
critical: critical,
value: value
});
break;
default:
control = new Control({
type: type,
@ -54,5 +62,6 @@ module.exports = {
},
Control: Control,
EntryChangeNotificationControl: EntryChangeNotificationControl,
PersistentSearchControl: PersistentSearchControl
};

View File

@ -60,7 +60,6 @@ module.exports = LDAPResult;
LDAPResult.prototype.end = function(status) {
assert.ok(this.connection);
if (typeof(status) === 'number')
this.status = status;

View File

@ -81,6 +81,8 @@ SearchEntry.prototype.fromObject = function(obj) {
throw new TypeError('object required');
var self = this;
if (obj.controls)
this.controls = obj.controls;
if (obj.attributes)
obj = obj.attributes;

View File

@ -30,6 +30,7 @@ function SearchRequest(options) {
options = {};
}
options.protocolOp = Protocol.LDAP_REQ_SEARCH;
LDAPMessage.call(this, options);

View File

@ -41,6 +41,8 @@ module.exports = SearchResponse;
* Defaults to 'false'.
*/
SearchResponse.prototype.send = function(entry, nofiltering) {
this.log.info('entry', entry);
this.log.info('nofiltering', nofiltering);
if (!entry || typeof(entry) !== 'object')
throw new TypeError('entry (SearchEntry) required');
if (nofiltering === undefined)
@ -87,6 +89,7 @@ SearchResponse.prototype.send = function(entry, nofiltering) {
}
try {
this.log.info('in search response send');
if (this.log.isDebugEnabled())
this.log.debug('%s: sending: %j', this.connection.ldap.id, entry.json);

View File

@ -0,0 +1,88 @@
var test = require('tap').test;
var asn1 = require('asn1');
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
var getControl;
var EntryChangeNotificationControl;
///--- Tests
test('load library', function(t) {
EntryChangeNotificationControl =
require('../../lib').EntryChangeNotificationControl;
t.ok(EntryChangeNotificationControl);
getControl = require('../../lib').getControl;
t.ok(getControl);
t.end();
});
test('new no args', function(t) {
t.ok(new EntryChangeNotificationControl());
t.end();
});
test('new with args', function(t) {
var c = new EntryChangeNotificationControl({
type: '2.16.840.1.113730.3.4.7',
criticality: true,
value: {
changeType: 8,
previousDN: 'cn=foobarbazcar',
changeNumber: 123456789
}
});
t.ok(c);
t.equal(c.type, '2.16.840.1.113730.3.4.7');
t.ok(c.criticality);
t.equal(c.value.changeType, 8);
t.equal(c.value.previousDN, 'cn=foobarbazcar');
t.equal(c.value.changeNumber, 123456789);
var writer = new BerWriter();
c.toBer(writer);
var reader = new BerReader(writer.buffer);
var psc = getControl(reader);
t.ok(psc);
console.log('psc', psc.value);
t.equal(psc.type, '2.16.840.1.113730.3.4.7');
t.ok(psc.criticality);
t.equal(psc.value.changeType, 8);
t.equal(psc.value.previousDN, 'cn=foobarbazcar');
t.equal(psc.value.changeNumber, 123456789);
t.end();
});
test('tober', function(t) {
var psc = new EntryChangeNotificationControl({
type: '2.16.840.1.113730.3.4.7',
criticality: true,
value: {
changeType: 8,
previousDN: 'cn=foobarbazcar',
changeNumber: 123456789
}
});
var ber = new BerWriter();
psc.toBer(ber);
var c = getControl(new BerReader(ber.buffer));
t.ok(c);
t.equal(c.type, '2.16.840.1.113730.3.4.7');
t.ok(c.criticality);
t.equal(c.value.changeType, 8);
t.equal(c.value.previousDN, 'cn=foobarbazcar');
t.equal(c.value.changeNumber, 123456789);
t.end();
});