UserManager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "../include/UserManager.h"
  2. #include "../../libs/libbcrypt/bcrypt.h"
  3. // TODO read userStorage file location from config
  4. // initialize static filename to empty string
  5. std::string UserManager::filename = "";
  6. void UserManager::init(const std::string &file) {
  7. filename = file;
  8. std::ifstream ifile(filename);
  9. if (!ifile.is_open()) {
  10. // create new file by adding a user if userStorage does not exist
  11. addUser("user", "pass");
  12. std::cout << "Created \"" << filename << "\" and added the default user" << std::endl;
  13. }
  14. ifile.close();
  15. }
  16. bool UserManager::isAllowed(const std::string &name, const std::string &pw) {
  17. std::map<std::string, std::string> user_map;
  18. readFromFile(user_map);
  19. auto it = user_map.find(name);
  20. // check if user exists and pw is valid
  21. if (it != user_map.end()) {
  22. // check bcrypt hash
  23. std::string hash = it->second;
  24. int ret = bcrypt_checkpw(pw.c_str(), hash.c_str());
  25. if (ret != 0)
  26. return false;
  27. return true;
  28. }
  29. return false;
  30. }
  31. bool UserManager::addUser(const std::string &name, const std::string &pw) {
  32. std::map<std::string, std::string> user_map;
  33. readFromFile(user_map);
  34. auto it = user_map.find(name);
  35. // if user exists, do nothing
  36. if (it != user_map.end()) {
  37. return false;
  38. }
  39. // calculate bcrypt hash
  40. std::string salt;
  41. std::string hash;
  42. salt.resize(BCRYPT_HASHSIZE);
  43. hash.resize(BCRYPT_HASHSIZE);
  44. int ret = bcrypt_gensalt(10, &salt.front());
  45. if (ret != 0)
  46. return false;
  47. ret = bcrypt_hashpw(pw.c_str(), salt.c_str(), &hash.front());
  48. if (ret != 0)
  49. return false;
  50. // remove trailing terminator chars
  51. hash.resize(hash.find('\0'));
  52. user_map.insert(std::pair<std::string, std::string>(name, hash));
  53. writeToFile(user_map);
  54. return true;
  55. }
  56. bool UserManager::deleteUser(const std::string &name, const std::string &pw) {
  57. // TODO check pw before delete
  58. std::map<std::string, std::string> user_map;
  59. readFromFile(user_map);
  60. auto it = user_map.find(name);
  61. if (it == user_map.end()) {
  62. return false;
  63. }
  64. if (it->second.compare(pw) != 0) {
  65. return false;
  66. }
  67. user_map.erase(it);
  68. writeToFile(user_map);
  69. return true;
  70. }
  71. // read content from file into given map
  72. void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
  73. std::ifstream ifile(filename);
  74. std::string line;
  75. while (getline(ifile, line)) {
  76. std::stringstream ss(line);
  77. std::string segment;
  78. std::vector<std::string> v;
  79. while (std::getline(ss, segment, ';')) {
  80. v.push_back(segment);
  81. }
  82. user_map.insert(std::pair<std::string, std::string>(v.at(0), v.at(1)));
  83. }
  84. ifile.close();
  85. }
  86. // write content from map to file
  87. void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
  88. std::ofstream file;
  89. file.open(filename);
  90. for (auto const &x : user_map) {
  91. file << x.first << ";" << x.second << std::endl;
  92. }
  93. file.close();
  94. }