Last active
July 3, 2018 18:45
-
-
Save jcheng5/0425aa9468ffc7e8d6b215c6094f85e0 to your computer and use it in GitHub Desktop.
LDAP test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before running this script, set the LDAP_PASSWORD environment variable in | |
// the shell. You can do this without echoing the password by running: | |
// | |
// read -s LDAP_PASSWORD && export LDAP_PASSWORD | |
// | |
// You'll also need to modify the variable definitions at the top of this file. | |
// == BEGIN configuration variables =============== | |
var url = "ldap://adtest.rstudio.com/dc=adtest,dc=rstudio,dc=com"; | |
var username = "john"; | |
var suffix = "adtest.rstudio.com"; | |
var search_base = "cn=Users,dc=adtest,dc=rstudio,dc=com"; | |
var search_filter = "(member:1.2.840.113556.1.4.1941:=CN=John Doe,CN=Users,DC=adtest,DC=rstudio,DC=com)"; | |
// == END configuration variables ================= | |
var username = username + "@" + suffix; | |
var password = process.env.LDAP_PASSWORD; | |
if (!password) { | |
console.error("LDAP_PASSWORD not set. Set it by calling 'read -s LDAP_PASSWORD && export LDAP_PASSWORD' from the shell."); | |
process.exit(1); | |
} | |
var ldapjs = require("ldapjs"); | |
var client = ldapjs.createClient({url: url}); | |
client.bind(username, password, function(err) { | |
if (!err) { | |
console.error("Successful bind."); | |
var opts = { | |
scope: "sub", | |
filter: search_filter, | |
attributes: ["dn"] | |
}; | |
client.search(search_base, opts, function(err, res) { | |
if (err) { | |
console.error("Error performing search:"); | |
console.error(err); | |
process.exit(1); | |
} | |
res.on('searchEntry', function(entry) { | |
console.log('entry: ' + JSON.stringify(entry.object)); | |
}); | |
res.on('searchReference', function(referral) { | |
console.log('referral: ' + referral.uris.join()); | |
}); | |
res.on('error', function(err) { | |
console.error('error: ' + err.message); | |
}); | |
res.on('end', function(result) { | |
console.log('status: ' + result.status); | |
client.unbind(); | |
}); | |
}); | |
} else { | |
console.error(err); | |
process.exit(1); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment