From c8ea58fc60f8c4278772c3aa9bb4429131433564 Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Mon, 15 Aug 2011 13:50:15 -0700 Subject: [PATCH] modifyDN touchups --- README.md | 42 +++++++++++++++++++++++++++++- docs/guide.md | 8 +++--- lib/messages/moddn_request.js | 8 +++--- tst/messages/moddn_request.test.js | 4 +-- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ba1cd2f..cd9192f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,44 @@ -Docs coming soon. +ldapjs makes the LDAP protocol a first class citizen in node.js. + +## Usage + +For full docs, head on over to . + + var ldap = require('ldapjs'); + + var server = ldap.createServer(); + + server.bind('cn=root', function(req, res, next) { + if (req.credentials !== 'secret') + return next(new ldap.InvalidCredentialsError()); + + res.end(); + }); + + server.search('dc=example', function(req, res, next) { + var obj = { + dn: req.dn.toString(), + attributes: { + objectclass: 'helloworld', + cn: 'hello', + sn: 'world' + } + }; + + if (req.filter.matches(obj)) + res.send(obj); + + res.end(); + }); + + server.listen(1389, function() { + console.log('ldapjs listening at ' + server.url); + }); + +To run that, assuming you've got the [OpenLDAP](http://www.openldap.org/) client on +your system: + + $ ldapsearch -H ldap://localhost:1389 -x -D cn=root -w secret -b dc=example objectclass=* ## Installation diff --git a/docs/guide.md b/docs/guide.md index 3ab8e91..00b88cd 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -52,7 +52,7 @@ Then there's a few things to note: name_, or _dn_ for short. A dn is comprised of attributes that lead to that node in the tree, as shown above (the syntax is foo=bar, ...). * The root of the tree is at the right of the _dn_, which is inverted from a -filesystem hierarchy, if that wasn't already obvious. +filesystem hierarchy. * Every entry in the tree is an _instance of_ an _objectclass_. * An _objectclass_ is a schema concept; think of it like a table in a traditional ORM. @@ -501,16 +501,16 @@ Here's a few new things: extend this little project with groups? We probably want those under a different part of the tree. * We did some really minimal schema enforcement by: -** Checking that the leaf RDN (relative distinguished name) was a _cn_ + + Checking that the leaf RDN (relative distinguished name) was a _cn_ attribute. -** We then did `req.toObject()`. As mentioned before, each of the req/res + + We then did `req.toObject()`. As mentioned before, each of the req/res objects have special APIs that make sense for that operation. Without getting into the details, the LDAP add operation on the wire doesn't look like a JS object, and we want to support both the LDAP nerd that wants to see what got sent, and the "easy" case. So use `.toObject()`. Note we also filtered out to the `attributes` portion of the object since that's all we're really looking at. -** Lastly, we did a super minimal check to see if the entry was of type + + Lastly, we did a super minimal check to see if the entry was of type `unixUser`. Frankly for this case, it's kind of useless, but it does illustrate one point: attribute names are case-insensitive, so ldapjs converts them all to lower case (note the client sent _objectClass_ over the wire). diff --git a/lib/messages/moddn_request.js b/lib/messages/moddn_request.js index 26c98e7..3dab4f6 100644 --- a/lib/messages/moddn_request.js +++ b/lib/messages/moddn_request.js @@ -49,9 +49,7 @@ function ModifyDNRequest(options) { var self = this; this.__defineGetter__('type', function() { return 'ModifyDNRequest'; }); - this.__defineGetter__('_dn', function() { - return self.entry ? self.entry.toString() : ''; - }); + this.__defineGetter__('_dn', function() { return self.entry; }); } util.inherits(ModifyDNRequest, LDAPMessage); module.exports = ModifyDNRequest; @@ -63,8 +61,8 @@ ModifyDNRequest.prototype._parse = function(ber) { this.entry = dn.parse(ber.readString()); this.newRdn = dn.parse(ber.readString()); this.deleteOldRdn = ber.readBoolean(); - if (ber.peek() === Ber.OctetString) - this.newSuperior = ber.readString(); + if (ber.peek() === 0x80) + this.newSuperior = dn.parse(ber.readString(0x80)); return true; }; diff --git a/tst/messages/moddn_request.test.js b/tst/messages/moddn_request.test.js index a9afdd3..c73e7ac 100644 --- a/tst/messages/moddn_request.test.js +++ b/tst/messages/moddn_request.test.js @@ -36,7 +36,7 @@ test('new with args', function(t) { deleteOldRdn: true }); t.ok(req); - t.equal(req.dn, 'cn=foo, o=test'); + t.equal(req.dn.toString(), 'cn=foo, o=test'); t.equal(req.newRdn.toString(), 'cn=foo2'); t.equal(req.deleteOldRdn, true); t.end(); @@ -51,7 +51,7 @@ test('parse', function(t) { var req = new ModifyDNRequest(); t.ok(req._parse(new BerReader(ber.buffer))); - t.equal(req.dn, 'cn=foo, o=test'); + t.equal(req.dn.toString(), 'cn=foo, o=test'); t.equal(req.newRdn.toString(), 'cn=foo2'); t.equal(req.deleteOldRdn, true);