Skip to content

Commit

Permalink
API to unpair clients
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Jan 20, 2024
1 parent 4b9f9d0 commit c8d74f2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,37 @@ namespace confighttp {
outputTree.put("status", true);
}

void
unpairClient(resp_https_t response, req_https_t request) {
if (!authenticate(response, request)) return;

print_req(request);

std::stringstream ss;
ss << request->content.rdbuf();

pt::ptree inputTree, outputTree;

auto g = util::fail_guard([&]() {
std::ostringstream data;
pt::write_json(data, outputTree);
response->write(data.str());
});

try {
// TODO: Input Validation
pt::read_json(ss, inputTree);
std::string uniqueid = inputTree.get<std::string>("uniqueid");
outputTree.put("status", nvhttp::unpair_client(uniqueid));
}
catch (std::exception &e) {
BOOST_LOG(warning) << "UnpairClient: "sv << e.what();
outputTree.put("status", false);
outputTree.put("error", e.what());
return;
}
}

void
listClients(resp_https_t response, req_https_t request) {
if (!authenticate(response, request)) return;
Expand Down Expand Up @@ -769,6 +800,7 @@ namespace confighttp {
server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
server.resource["^/api/clients/unpair$"]["POST"] = unpairAll;
server.resource["^/api/clients/list$"]["GET"] = listClients;
server.resource["^/api/clients/unpair-single$"]["POST"] = unpairClient;
server.resource["^/api/apps/close$"]["POST"] = closeApp;
server.resource["^/api/covers/upload$"]["POST"] = uploadCover;
server.resource["^/images/sunshine.ico$"]["GET"] = getFaviconImage;
Expand Down
23 changes: 23 additions & 0 deletions src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,4 +1153,27 @@ namespace nvhttp {
map_id_client.clear();
save_state();
}

/**
* @brief Remove all single client.
*
* EXAMPLES:
* ```cpp
* nvhttp::unpair_client("4D7BB2DD-5704-A405-B41C-891A022932E1");
* ```
*/
bool
unpair_client(std::string uniqueID) {
for (auto &[_, client] : map_id_client) {
for (auto it = client.named_certs.begin(); it != client.named_certs.end();)
{
if ((*it).uniqueID == uniqueID)
it = client.named_certs.erase(it);
else
++it;
}
}
save_state();
return true;
}
} // namespace nvhttp
2 changes: 2 additions & 0 deletions src/nvhttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ namespace nvhttp {
start();
bool
pin(std::string pin, std::string name);
bool
unpair_client(std::string uniqueid);
boost::property_tree::ptree
get_all_clients();
void
Expand Down

0 comments on commit c8d74f2

Please sign in to comment.