UserManager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. user_map.insert(std::pair<std::string, std::string>(name, hash));
  51. writeToFile(user_map);
  52. return true;
  53. }
  54. bool UserManager::deleteUser(const std::string &name, const std::string &pw) {
  55. // TODO check pw before delete
  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. if (it->second.compare(pw) != 0) {
  63. return false;
  64. }
  65. user_map.erase(it);
  66. writeToFile(user_map);
  67. return true;
  68. }
  69. // read content from file into given map
  70. void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
  71. std::ifstream ifile(filename);
  72. std::string line;
  73. while (getline(ifile, line)) {
  74. std::stringstream ss(line);
  75. std::string segment;
  76. std::vector<std::string> v;
  77. while (std::getline(ss, segment, ';')) {
  78. v.push_back(segment);
  79. }
  80. user_map.insert(std::pair<std::string, std::string>(v.at(0), v.at(1)));
  81. }
  82. ifile.close();
  83. }
  84. // write content from map to file
  85. void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
  86. std::ofstream file;
  87. file.open(filename);
  88. for (auto const &x : user_map) {
  89. file << x.first << ";" << x.second << std::endl;
  90. }
  91. file.close();
  92. }