UserManager.cpp 2.8 KB

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