Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ldap_ohm.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Original file created by Prof.Dr. Matthias Hopf
  2. /*
  3. * Valdiate ohm logins with ldap service
  4. */
  5. const ldap = require('ldapjs');
  6. const ldap_escape = require('ldap-escape');
  7. // NOTE: Where do I get the URL from?? A: Is given.
  8. var ldap_client = ldap.createClient({
  9. //url: 'ldap://gso2.ads1.fh-nuernberg.de/',
  10. url: 'ldap://sso.cs.ohm-hochschule.de:389/',
  11. //url: 'ldaps://sso.cs.ohm-hochschule.de:636/',
  12. reconnect: true,
  13. // timeouts don't work reliably
  14. });
  15. // NOTE: Where do I get the 'bindpath' parameters info from? A: Is given.
  16. const ldap_config = {
  17. bindpath: 'cn=Users,dc=ohm-hochschule,dc=de',
  18. timeout: 2000
  19. };
  20. const ldap_ohm = {
  21. init: function () {
  22. },
  23. // Authorize user with password
  24. // Calls callback with null if unauthorized
  25. // Calls callback with object describing user if successful:
  26. authorize: function (user, pwd, cb) {
  27. if (typeof user != 'string' || typeof pwd != 'string')
  28. return cb (null);
  29. // Empty passwords *may* bind successfully anonymously
  30. if (user.length < 1 || pwd.length < 1)
  31. return cb (null);
  32. /* Same function, different writing style */
  33. /* Escape ldap login input */
  34. //escaped = ldap_escape.dn`cn=${user},`+ldap_config.bindpath;
  35. escaped = ldap_escape.dn (['cn=',','+ldap_config.bindpath], user);
  36. // Timeout handler: call callback,
  37. // make sure later ldap returns don't do anything weird
  38. var return_object = {};
  39. var timeoutHandle = setTimeout (function () {
  40. console.log('ldap timeout');
  41. return_object = null;
  42. cb (null);
  43. }, ldap_config.timeout);
  44. // Bind ldap to user (authorize)
  45. ldap_client.bind (escaped, pwd, function (err, res) {
  46. if (return_object === null)
  47. return; // Timeout, cb has already been called
  48. if (err !== null) {
  49. console.log ("ldap bind: failed for user " + user + ": " + err);
  50. clearTimeout (timeoutHandle);
  51. return cb (null);
  52. }
  53. // Search for user entry of just bound user
  54. // There should be only one...
  55. ldap_client.search (escaped, { sizeLimit: 1 }, function (err, res) {
  56. if (return_object === null)
  57. return; // Timeout, cb has already been called
  58. if (err !== null) {
  59. console.log ("ldap search: search after bind didn't work for user "
  60. + user + ": " + err);
  61. clearTimeout (timeoutHandle);
  62. return cb (null);
  63. }
  64. // Populate return with search results
  65. res.on('searchEntry', function(entry) {
  66. if (return_object === null)
  67. return; // Timeout, cb has already been called
  68. return_object.user = user;
  69. return_object.name = entry.object.displayname;
  70. return_object.type = entry.object.employeetype;
  71. return_object.mail = entry.object.mail;
  72. return_object.gender = entry.object.orclgender;
  73. // Calling cb here, not in 'end', because of potential bugs with
  74. // concurrency failures, and we have our single(!) entry
  75. // https://github.com/joyent/node-ldapjs/pull/424
  76. clearTimeout (timeoutHandle);
  77. if (typeof return_object.mail != 'string' || return_object.mail.length < 1) {
  78. console.log("ldap search error after bind for user " + user);
  79. return cb (null);
  80. }
  81. // ldap_test output
  82. // return_object = entry.object;
  83. return cb (return_object);
  84. });
  85. res.on('error', function(err) {
  86. console.log('ldap error: ' + err.message);
  87. });
  88. res.on('end', function(result) {
  89. // TODO: analyze result.status?
  90. // console.info('ldap result: ');
  91. // console.info(result);
  92. });
  93. });
  94. });
  95. }
  96. };
  97. module.exports = ldap_ohm;