From 85838538ce27940b16d03af2c49ca00f06c675b2 Mon Sep 17 00:00:00 2001 From: Raoul Hecky Date: Sun, 9 Jun 2024 15:09:53 +0000 Subject: [PATCH] Add JSON api to change credentials --- src/bin/calaos_server/JsonApi.cpp | 26 ++++++++++++++++++++ src/bin/calaos_server/JsonApi.h | 2 ++ src/bin/calaos_server/JsonApiHandlerHttp.cpp | 2 +- src/bin/calaos_server/JsonApiHandlerWS.cpp | 23 ++++++++++++----- src/bin/calaos_server/JsonApiHandlerWS.h | 1 + 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/bin/calaos_server/JsonApi.cpp b/src/bin/calaos_server/JsonApi.cpp index 57eaa787..b9ff99d3 100644 --- a/src/bin/calaos_server/JsonApi.cpp +++ b/src/bin/calaos_server/JsonApi.cpp @@ -1668,3 +1668,29 @@ bool JsonApi::registerPushToken(const Params &jParam) return true; } +bool JsonApi::changeCredentials(string olduser, string oldpass, string newuser, string newpass) +{ + //get actual user/pass + string user = Utils::get_config_option("calaos_user"); + string pass = Utils::get_config_option("calaos_password"); + + if (Utils::get_config_option("cn_user") != "" && + Utils::get_config_option("cn_pass") != "") + { + user = Utils::get_config_option("cn_user"); + pass = Utils::get_config_option("cn_pass"); + } + + if (user != olduser || pass != oldpass) + return false; //wrong old user/pass + + //change user/pass + Utils::set_config_option("cn_user", newuser); + Utils::set_config_option("cn_pass", newpass); + //remove old entries if needed + Utils::del_config_option("calaos_user"); + Utils::del_config_option("calaos_password"); + sync(); + + return true; +} diff --git a/src/bin/calaos_server/JsonApi.h b/src/bin/calaos_server/JsonApi.h index b066117a..2cd98aec 100644 --- a/src/bin/calaos_server/JsonApi.h +++ b/src/bin/calaos_server/JsonApi.h @@ -117,6 +117,8 @@ class JsonApi: public sigc::trackable HttpClient *httpClient = nullptr; map playerCounts; + + bool changeCredentials(string olduser, string oldpass, string newuser, string newpass); }; #endif // JSONAPI_H diff --git a/src/bin/calaos_server/JsonApiHandlerHttp.cpp b/src/bin/calaos_server/JsonApiHandlerHttp.cpp index d8f0c51c..450e58f7 100644 --- a/src/bin/calaos_server/JsonApiHandlerHttp.cpp +++ b/src/bin/calaos_server/JsonApiHandlerHttp.cpp @@ -83,7 +83,7 @@ void JsonApiHandlerHttp::processApi(const string &data, const Params ¶msGET) } - //check for if username/password matches + //check if username/password matches string user = Utils::get_config_option("calaos_user"); string pass = Utils::get_config_option("calaos_password"); diff --git a/src/bin/calaos_server/JsonApiHandlerWS.cpp b/src/bin/calaos_server/JsonApiHandlerWS.cpp index 560a2a06..1f8fc27b 100644 --- a/src/bin/calaos_server/JsonApiHandlerWS.cpp +++ b/src/bin/calaos_server/JsonApiHandlerWS.cpp @@ -100,7 +100,7 @@ void JsonApiHandlerWS::processApi(const string &data, const Params ¶msGET) free(d); } - //decode the json root object into jsonParam + //decode the json root object into Params jansson_decode_object(jroot, jsonRoot); json_t *jdata = json_object_get(jroot, "data"); @@ -111,7 +111,7 @@ void JsonApiHandlerWS::processApi(const string &data, const Params ¶msGET) if (jsonRoot["msg"] == "login") { - //check for if username/password matches + //check if username/password matches string user = Utils::get_config_option("calaos_user"); string pass = Utils::get_config_option("calaos_password"); @@ -137,10 +137,7 @@ void JsonApiHandlerWS::processApi(const string &data, const Params ¶msGET) } else { - json_t *jret = json_object(); - json_object_set_new(jret, "success", json_string("true")); - - sendJson("login", jret, jsonRoot["msg_id"]); + sendJson("login", {{ "success", "true" }}, jsonRoot["msg_id"]); loggedin = true; } @@ -181,6 +178,8 @@ void JsonApiHandlerWS::processApi(const string &data, const Params ¶msGET) processEventLog(jsonData, jsonRoot["msg_id"]); else if (jsonRoot["msg"] == "register_push") processRegisterPush(jsonData, jsonRoot["msg_id"]); + else if (jsonRoot["msg"] == "settings") + processSettings(jsonData, jsonRoot["msg_id"]); // else if (jsonParam["action"] == "get_cover") // processGetCover(); @@ -471,3 +470,15 @@ void JsonApiHandlerWS::processRegisterPush(const Params &jsonReq, const string & } } +void JsonApiHandlerWS::processSettings(const Params &jsonReq, const string &client_id) +{ + if (jsonReq["action"] == "change_cred") + { + bool ok = changeCredentials(jsonReq["old_user"], jsonReq["old_pw"], jsonReq["new_user"], jsonReq["new_pw"]); + if (ok) + loggedin = false; //user must login again with new password + + Json ret = {{ "success", ok?"true":"false" }}; + sendJson("settings", ret, client_id); + } +} diff --git a/src/bin/calaos_server/JsonApiHandlerWS.h b/src/bin/calaos_server/JsonApiHandlerWS.h index 9909a95d..b23a860f 100644 --- a/src/bin/calaos_server/JsonApiHandlerWS.h +++ b/src/bin/calaos_server/JsonApiHandlerWS.h @@ -59,6 +59,7 @@ class JsonApiHandlerWS: public JsonApi void processSetTimerange(json_t *jdata, const string &client_id = string()); void processEventLog(const Params &jsonReq, const string &client_id = string()); void processRegisterPush(const Params &jsonReq, const string &client_id = string()); + void processSettings(const Params &jsonReq, const string &client_id = string()); void processAudio(json_t *jdata, const string &client_id = string()); void processAudioDb(json_t *jdata, const string &client_id = string());