UserManager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "../include/UserManager.h"
  2. #include "../../libs/libbcrypt/bcrypt.h"
  3. // initialize static filename to empty string
  4. std::string UserManager::filename = "";
  5. void UserManager::init(const std::string &file) {
  6. filename = file;
  7. std::ifstream ifile(filename);
  8. if (!ifile.is_open()) {
  9. // create new file by adding a user if userStorage does not exist
  10. addUser("user", "pass");
  11. std::cout << "Created \"" << filename << "\" and added the default user" << std::endl;
  12. }
  13. ifile.close();
  14. }
  15. bool UserManager::isAllowed(const std::string &name, const std::string &pw) {
  16. std::map<std::string, std::string> user_map;
  17. readFromFile(user_map);
  18. auto it = user_map.find(name);
  19. // check if user exists and pw is valid
  20. if (it != user_map.end()) {
  21. // check bcrypt hash
  22. std::string hash = it->second;
  23. int ret = bcrypt_checkpw(pw.c_str(), hash.c_str());
  24. if (ret != 0)
  25. return false;
  26. return true;
  27. }
  28. return false;
  29. }
  30. bool UserManager::addUser(const std::string &name, const std::string &pw) {
  31. std::map<std::string, std::string> user_map;
  32. readFromFile(user_map);
  33. auto it = user_map.find(name);
  34. // if user exists, do nothing
  35. if (it != user_map.end()) {
  36. return false;
  37. }
  38. // calculate bcrypt hash
  39. std::string salt;
  40. std::string hash;
  41. salt.resize(BCRYPT_HASHSIZE);
  42. hash.resize(BCRYPT_HASHSIZE);
  43. int ret = bcrypt_gensalt(10, &salt.front());
  44. if (ret != 0)
  45. return false;
  46. ret = bcrypt_hashpw(pw.c_str(), salt.c_str(), &hash.front());
  47. if (ret != 0)
  48. return false;
  49. // remove trailing terminator chars
  50. hash.resize(hash.find('\0'));
  51. user_map.insert(std::pair<std::string, std::string>(name, hash));
  52. writeToFile(user_map);
  53. return true;
  54. }
  55. bool UserManager::deleteUser(const std::string &name, const std::string &pw) {
  56. std::map<std::string, std::string> user_map;
  57. readFromFile(user_map);
  58. auto it = user_map.find(name);
  59. if (it == user_map.end()) {
  60. return false;
  61. }
  62. // checking password before deleting
  63. std::string hash = it->second;
  64. if (bcrypt_checkpw(pw.c_str(), hash.c_str()) != 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. }