diff --git a/src/auth_service.cpp b/src/auth_service.cpp index 5a974d3..521bf0a 100644 --- a/src/auth_service.cpp +++ b/src/auth_service.cpp @@ -14,9 +14,8 @@ int main() { srv.Post("/api/users/user", get_user); srv.Post("/api/users/all", get_all_users); srv.Post("/api/users/add", add_user); - // srv.Post("/api/changepassword", change_password); + srv.Post("/api/users/user/password/change", change_password); srv.Post("/api/users/del", del_user); - // srv.Post("/api/deluserassuperuser", del_user_as_superuser); srv.Post("/api/access", user_access); srv.listen("localhost", 43243); diff --git a/src/users/users.cpp b/src/users/users.cpp index b5b6bd8..3cdec7f 100644 --- a/src/users/users.cpp +++ b/src/users/users.cpp @@ -181,6 +181,82 @@ void del_user(const httplib::Request& request, httplib::Response& response) { response.set_content("{\"status\":\"ok\"}", "application/json"); } +// functtion for change password for user +void change_password(const httplib::Request& request, httplib::Response& response) { + if (request.body == "") {response.set_content( + "{\"required\":\"[token,old_password,new_password] or if you superuser [token,userid,new_password]\"}", + "application/json" + );return;} + nlohmann::json json_body = nlohmann::json::parse(request.body); + + if (json_body["token"] == nullptr) {response.set_content( + "{\"required\":\"[token,old_password,new_password] or if you superuser [token,userid,new_password]\"}", + "application/json" + );return;} + if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) { + response.set_content("{\"required\":\"[token,old_password,new_password] or if you superuser [token,userid,new_password]\"}", "application/json"); + return; + } + if (json_body["new_password"] == nullptr) {response.set_content("{\"required\":\"[token] or if you superuser [token,userid]\"}", "application/json");return;} + if (!verify_auth(json_body["token"])) {response.set_content("{\"status\":\"403\"}", "application/json");return;} + + std::string old_password = ""; + const std::string new_password = json_body["new_password"]; + + if (json_body["userid"] == nullptr) {old_password = json_body["old_password"];} + + auto decoded_token = jwt::decode(json_body["token"]); + std::string userid = ""; + for (auto& e : decoded_token.get_payload_json()) { + if (e.first == "userId") { + userid = e.second.to_str(); + break; + } + } + if (userid == "") {response.set_content("{\"status\":\"403\"}", "application/json");return;} + + std::ifstream usersfile("users.json"); + nlohmann::json all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + + std::string changepassuserid = userid; + if (json_body["userid"] != nullptr) { + changepassuserid = json_body["userid"]; + nlohmann::json response_user_data = nullptr; + for (auto& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + } + } + if (response_user_data == nullptr) {response.set_content("{\"status\":\"403\"}", "application/json");return;} + if (response_user_data["is_superuser"] != "1") {response.set_content("{\"status\":\"403\"}", "application/json");return;} + } + + bool found = false; + if (json_body["userid"] != nullptr) { + for (int i = 0; i < all_users.size(); i++) { + if ((all_users[i]["id"] == changepassuserid)) { + all_users[i]["password"] = new_password; + found = true; + break; + } + } + } else { + for (int i = 0; i < all_users.size(); i++) { + if ((all_users[i]["id"] == changepassuserid) && (all_users[i]["password"] == old_password)) { + all_users[i]["password"] = new_password; + found = true; + break; + } + } + } + if (!found) {response.set_content("{\"status\":\"can't find this user\"}", "application/json");return;} + std::ofstream usersfilew("users.json"); + usersfilew << all_users.dump(); + usersfilew.close(); + response.set_content("{\"status\":\"ok\"}", "application/json"); +} + // function for access or reject authorization void user_access(const httplib::Request& request, httplib::Response& response) { if (request.body == "") {response.set_content("{\"required\":\"[token]\"}", "application/json");return;} diff --git a/src/users/users.hpp b/src/users/users.hpp index fbd38b7..532d813 100644 --- a/src/users/users.hpp +++ b/src/users/users.hpp @@ -15,5 +15,7 @@ void user_access(const httplib::Request& request, httplib::Response& response); void add_user(const httplib::Request& request, httplib::Response& response); // function for del user void del_user(const httplib::Request& request, httplib::Response& response); +// function for change password for user +void change_password(const httplib::Request& request, httplib::Response& response); #endif // USERS_HPP