Skip to content

Commit

Permalink
Add JSON api to change credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Jun 9, 2024
1 parent 8161491 commit 8583853
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/bin/calaos_server/JsonApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/bin/calaos_server/JsonApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class JsonApi: public sigc::trackable
HttpClient *httpClient = nullptr;

map<string, int> playerCounts;

bool changeCredentials(string olduser, string oldpass, string newuser, string newpass);
};

#endif // JSONAPI_H
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/JsonApiHandlerHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void JsonApiHandlerHttp::processApi(const string &data, const Params &paramsGET)
}


//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");

Expand Down
23 changes: 17 additions & 6 deletions src/bin/calaos_server/JsonApiHandlerWS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void JsonApiHandlerWS::processApi(const string &data, const Params &paramsGET)
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");
Expand All @@ -111,7 +111,7 @@ void JsonApiHandlerWS::processApi(const string &data, const Params &paramsGET)

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");

Expand All @@ -137,10 +137,7 @@ void JsonApiHandlerWS::processApi(const string &data, const Params &paramsGET)
}
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;
}
Expand Down Expand Up @@ -181,6 +178,8 @@ void JsonApiHandlerWS::processApi(const string &data, const Params &paramsGET)
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();
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/bin/calaos_server/JsonApiHandlerWS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 8583853

Please sign in to comment.