Created
August 1, 2017 01:47
-
-
Save mattwoolnough/4ab72ad0d00b9f3067bb55835bda1566 to your computer and use it in GitHub Desktop.
ldapjs Password Change
This file contains hidden or 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
var SUFFIX = 'dc=vis,dc=net'; | |
var pfUsername = '<ADMINDN>'; | |
var pfPassword = '<ADMINPW>'; | |
var url = 'ldaps://vis.res.eq.edu.au'; | |
var filter = '(uid=mjwoo1)'; | |
var oldPassword = "oldpw"; | |
var newPassword = "newpw"; | |
var Client, testClient, anon_client; | |
var ldap = require('ldapjs'), | |
_ = require('lodash'), | |
fs = require('fs'); | |
function encodePassword(password) { | |
return new Buffer('"' + password + '"', 'utf16le').toString(); | |
} | |
ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B; | |
Client = ldap.createClient({ | |
url: url, | |
tlsOptions: { | |
ca: [ | |
fs.readFileSync('MPEROOTCA.pem'), | |
fs.readFileSync('MPEINTPOLCA.pem'), | |
fs.readFileSync('MPEINTISS1CA.pem') | |
] | |
} | |
}); | |
Client.bind(pfUsername, pfPassword, function (err, result) { | |
if (err) { | |
console.error('error: ' + err); | |
} else { | |
Client.search('dc=vis,dc=net', { | |
filter: filter, | |
attributes: 'dn', | |
scope: 'sub' | |
}, function(err, res) { | |
res.on('searchEntry', function(entry) { | |
var userDN = entry.object.dn; | |
Client.modify(userDN, [ | |
new ldap.Change({ | |
operation: 'delete', | |
modification: { | |
unicodePwd: encodePassword(oldPassword) | |
} | |
}), | |
new ldap.Change({ | |
operation: 'add', | |
modification: { | |
unicodePwd: encodePassword(newPassword) | |
} | |
}) | |
], function(err) { | |
if (err) { | |
console.log(err.code); | |
console.log(err.name); | |
console.log(err.message); | |
client.unbind(); | |
} | |
else { | |
console.log('Password changed!'); | |
} | |
}); | |
}); | |
res.on('error', function(err) { | |
console.error('error: ' + err.message); | |
}); | |
res.on('end', function(result) { | |
console.log('status: ' + result.status); | |
}); | |
}); | |
} | |
}); | |
// Client.unbind(function (error) { | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can get your tlsOptions.. and I cannot handle this =>> ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B;
Would you please give me a favor?