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.

authorization.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Original file created by Prof.Dr. Matthias Hopf
  2. /*
  3. * Authorization
  4. */
  5. var common, Users;
  6. const ldap = require ('./ldap_ohm'),
  7. crypto = require ('./crypto'),
  8. dbs = require ('./dbs');
  9. // deactivated is not used yet
  10. const serverVisibleSession = { user: true, name: true, type: true, mail: true, roles: true, gender: true, deactivated: true, host: true };
  11. const clientVisibleSession = { user: true, name: true, type: true, mail: true, roles: true, gender: true };
  12. // Fill in session object
  13. function fillSession (req, user, roles, cb) {
  14. if (req.session === undefined)
  15. next (common.genError (500, "Error"));
  16. // regenerate a new session-id with clean instance
  17. if (user !== undefined && roles !== undefined) {
  18. req.session.regenerate (function (err) {
  19. if (user !== undefined && ! err) {
  20. common.shallowCopy (user, serverVisibleSession, {roles: true}, req.session);
  21. console.info(req.session);
  22. if (user._id) {
  23. req.session.user = user._id;
  24. }
  25. req.session.roles = roles;
  26. }
  27. return cb (err);
  28. });
  29. } else {
  30. return cb ();
  31. }
  32. }
  33. // Save found user into DB, if not already exists
  34. function saveFoundToDB(found, cb) {
  35. console.info(found);
  36. Users.findById(found.user)
  37. .exec(function(err, result){
  38. if (err) {
  39. console.error("Error: Users collection.");
  40. console.error(err);
  41. }
  42. // User doesn't exist
  43. if (found !== undefined && !result) {
  44. Users.create({
  45. _id: found.user,
  46. name: found.name,
  47. mail: found.mail,
  48. type: found.type,
  49. gender: found.gender,
  50. // abos: '',
  51. // bookmarks: '',
  52. roles: 'user',
  53. }, function(err, done) {
  54. if (err) {
  55. console.error("User creation: Failed");
  56. console.error(err);
  57. } else {
  58. console.info("New User created!");
  59. }
  60. if (done == null) {
  61. console.error("Can not create user.");
  62. }
  63. return cb(err);
  64. });
  65. } else {
  66. return cb(err);
  67. }
  68. });
  69. }
  70. const authorization = {
  71. // Generate Error object suitible for throwing or next()ing
  72. genCheckAuthorized: function (group) {
  73. return function (req, res, next) {
  74. if (req.session === undefined || req.session.user === undefined ||
  75. req.session.roles === undefined)
  76. return next (common.genError (403, "Unauthorized"));
  77. if (req.session.roles[group] === undefined)
  78. return next (common.genError (403, "Unauthorized"));
  79. next ();
  80. }
  81. },
  82. // Login route: requires .user and .pwd params
  83. login: function (req, res, next) {
  84. var user = req.body.user || '';
  85. var pwd = req.body.pwd || '';
  86. // Helper: Return valid session Object
  87. function returnSession () {
  88. // Only export client visible parts of session object
  89. var copy = common.shallowCopy (req.session, clientVisibleSession);
  90. return res.json (copy);
  91. }
  92. // Helper: Return error
  93. function returnError () {
  94. fillSession (req, undefined, undefined, function (err) {
  95. next (common.genError (401, "Unauthorized"));
  96. });
  97. }
  98. // Check whether to just validate current session ID
  99. if (user === '' && pwd === '') {
  100. console.log ("auth revalidate: " + req.session._id);
  101. if (req.session.user === undefined)
  102. return returnError();
  103. return returnSession ();
  104. }
  105. // check local database, then ldap
  106. Users.findById (req.body.user) .exec (function (err, entry) {
  107. // If there is a local user AND it has a password associated, test against this, and only this
  108. if (entry != null && entry.pwd) {
  109. if (crypto.checkLocalAuth (entry, req.body.pwd)) {
  110. console.info(entry.roles);
  111. return fillSession (req, entry, JSON.stringify(entry.roles), returnSession);
  112. }
  113. return returnError ();
  114. }
  115. // check ldap
  116. ldap.authorize (user.toLowerCase(), pwd, function (found) {
  117. //console.log ("ldap authorize " + user + " returns " + JSON.stringify (found));
  118. // No ldap entry either -> unauthorized
  119. if (found == null) {
  120. return returnError ();
  121. }
  122. // If there is an entry w/o password, use it for roles etc.
  123. if (entry) {
  124. if (! entry.name || entry.name === "")
  125. entry.name = found.name;
  126. if (! entry.mail || entry.mail === "")
  127. entry.mail = found.mail;
  128. if (! entry.type || entry.type === "")
  129. entry.type = found.type;
  130. if (! entry.orclgender || entry.orclgender === "")
  131. entry.orclgender = found.orclgender;
  132. return fillSession (req, entry, entry.roles.length > 0 ? common.arrayToHash(entry.roles) : {user:true}, returnSession);
  133. }
  134. // Otherwise create standard user entry
  135. saveFoundToDB(found, function() {
  136. return fillSession (req, found, {user:true}, returnSession);
  137. });
  138. });
  139. });
  140. },
  141. logout: function (req, res, next) {
  142. fillSession (req, undefined, undefined, function (err) {
  143. // Session delete, exists further in db
  144. req.session.destroy(function(err) {
  145. if (err) {
  146. console.error(err);
  147. }
  148. });
  149. console.info(req.session);
  150. return res.json ({});
  151. });
  152. },
  153. init: function (_common) {
  154. common = _common;
  155. ldap.init (_common);
  156. Users = dbs.models.Users;
  157. },
  158. };
  159. module.exports = authorization;