Lightweight node app to use in place of plex to self host your video and movie collections. Running on just under 50MB of ram this is ideal for people looking to host videos on minimal hardware.
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.

150 lines
4.1 KiB

  1. /** Crypto package used for hashing */
  2. const crypto = require('crypto');
  3. /**
  4. * Helper function to generate a hashed password
  5. * from a given plain text password.
  6. *
  7. * This uses 64 bits of entropy as the random salt
  8. * and uses sha256 hashing method to hash the password
  9. * combined with the salt.
  10. *
  11. * @param password
  12. * @returns {Object pass: hashedPassword, salt: salt used to hash}
  13. */
  14. const createHashedPasswordObject = function(password)
  15. {
  16. const randBuff = crypto.randomBytes(64);
  17. const salt = crypto.createHash('sha256').update(randBuff).digest('hex');
  18. const hashPass = hashPassword(password, salt);
  19. var hashPassObject = new Object();
  20. hashPassObject.pass = hashPass;
  21. hashPassObject.salt = salt;
  22. return hashPassObject;
  23. };
  24. /**
  25. * Hashes a pasword with a aprticular salt
  26. * using the crypto library
  27. *
  28. * @param password
  29. * @param salt
  30. */
  31. const hashPassword = function(password, salt)
  32. {
  33. return crypto.createHash('sha256')
  34. .update(password + salt)
  35. .digest('hex');
  36. };
  37. /**
  38. * Fetches the index of the user in the configuration. If the
  39. * user does not exists a -1 is returned.
  40. */
  41. const getIndexOfUser = function(username, configuration)
  42. {
  43. for(var i = 0; i < configuration.users.length; i++)
  44. {
  45. if (username === configuration.users[i].username)
  46. {
  47. if(username === configuration.users[i].username)
  48. {
  49. return i;
  50. }
  51. }
  52. }
  53. return -1;
  54. };
  55. module.exports =
  56. {
  57. /**
  58. * Checks to see if there was a valid login attempt
  59. *
  60. * @param username
  61. * @param password
  62. * @param configuration
  63. * @returns {boolean}
  64. */
  65. checkLogin: function(username, password, configuration)
  66. {
  67. const userIndex = getIndexOfUser(username, configuration);
  68. if(userIndex === -1)
  69. return false;
  70. const hashedPassword = hashPassword(password, configuration.users[userIndex].salt);
  71. return configuration.users[userIndex].password == hashedPassword;
  72. },
  73. /**
  74. * Adds a user to the configuration
  75. *
  76. * @param username
  77. * @param password
  78. * @param configuration
  79. * @returns {boolean}
  80. */
  81. addUser: function(username, password, configuration)
  82. {
  83. const userIndex = getIndexOfUser(username, configuration);
  84. if(userIndex !== -1)
  85. return false; // user already exists
  86. var newUser = new Object();
  87. newUser.username = username;
  88. if(configuration.users.length === 0)
  89. newUser.id = 1;
  90. else
  91. newUser.id = configuration.users[configuration.users.length -1].id + 1;
  92. const passObject = createHashedPasswordObject(password);
  93. newUser.salt = passObject.salt;
  94. newUser.password = passObject.pass;
  95. configuration.users.push(newUser);
  96. return true;
  97. },
  98. /**
  99. * Edits a user based on their id
  100. *
  101. * @param id
  102. * @param userName
  103. * @param password
  104. * @param configuration
  105. */
  106. editUser: function(id, userName, password, configuration)
  107. {
  108. for(var i = 0; i < configuration.users.length; i++)
  109. {
  110. if (configuration.users[i].id + "" === id)
  111. {
  112. configuration.users[i].username = userName;
  113. var passObj = createHashedPasswordObject(password);
  114. configuration.users[i].salt = passObj.salt;
  115. configuration.users[i].password = passObj.pass;
  116. }
  117. }
  118. },
  119. /**
  120. * Removes a user account from the configuration
  121. * @param id
  122. * @param configuration
  123. */
  124. removeUser: function(id, configuration)
  125. {
  126. configuration.users = configuration.users.filter(function(value, index, arr)
  127. {
  128. return value.id + "" !== id
  129. });
  130. }
  131. };