Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name and unpair individual clients #2042

Merged
merged 3 commits into from
May 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor i18n usage on PIN page
xanderfrangos authored and ReenigneArcher committed May 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 6edb4b08e663f020a829b1720957f51a4de7efc4
56 changes: 27 additions & 29 deletions src_assets/common/assets/web/pin.html
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
<Navbar></Navbar>
<div id="content" class="container">
<h1 class="my-4 text-center">{{ $t('pin.pin_pairing') }}</h1>
<form action="" class="form d-flex flex-column align-items-center" id="form">
<form class="form d-flex flex-column align-items-center" id="form" @submit.prevent="registerDevice">
<div class="card flex-column d-flex p-4 mb-4">
<input type="text" pattern="\d*" :placeholder="`${$t('navbar.pin')}`" autofocus id="pin-input" class="form-control mt-2" required />
<input type="text" :placeholder="`${$t('pin.device_name')}`" id="name-input" class="form-control my-4" required />
@@ -25,40 +25,38 @@ <h1 class="my-4 text-center">{{ $t('pin.pin_pairing') }}</h1>

<script type="module">
import { createApp } from 'vue'
import i18nLocale from './locale.js'
import { initApp } from './init'
import Navbar from './Navbar.vue'
import {initApp} from "./init";

let app = createApp({
components: {
Navbar
},
inject: ['i18n'],
methods: {
registerDevice(e) {
let pin = document.querySelector("#pin-input").value;
let name = document.querySelector("#name-input").value;
document.querySelector("#status").innerHTML = "";
let b = JSON.stringify({pin: pin, name: name});
fetch("/api/pin", {method: "POST", body: b})
.then((response) => response.json())
.then((response) => {
if (response.status.toString().toLowerCase() === "true") {
document.querySelector(
"#status"
).innerHTML = `<div class="alert alert-success" role="alert">${this.i18n.t('pin.pair_success')}</div>`;
document.querySelector("#pin-input").value = "";
document.querySelector("#name-input").value = "";
} else {
document.querySelector(
"#status"
).innerHTML = `<div class="alert alert-danger" role="alert">${this.i18n.t('pin.pair_failure')}</div>`;
}
});
}
}
});

initApp(app, (async app => {
// this must be after mounting the app
const i18n = await i18nLocale()
document.querySelector("#form").addEventListener("submit", (e) => {
e.preventDefault();
let pin = document.querySelector("#pin-input").value;
let name = document.querySelector("#name-input").value;
document.querySelector("#status").innerHTML = "";
let b = JSON.stringify({ pin: pin, name: name });
fetch("/api/pin", { method: "POST", body: b })
.then((response) => response.json())
.then((response) => {
if (response.status.toString().toLowerCase() === "true") {
document.querySelector(
"#status"
).innerHTML = `<div class="alert alert-success" role="alert">${i18n.global.t('pin.pair_success')}</div>`;
document.querySelector("#pin-input").value = "";
document.querySelector("#name-input").value = "";
} else {
document.querySelector(
"#status"
).innerHTML = `<div class="alert alert-danger" role="alert">${i18n.global.t('pin.pair_failure')}</div>`;
}
});
});
}));
initApp(app);
</script>

Unchanged files with check annotations Beta

}
void
unpair(resp_https_t response, req_https_t request) {

Check warning on line 726 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L726

Added line #L726 was not covered by tests
if (!authenticate(response, request)) return;
print_req(request);

Check warning on line 729 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L729

Added line #L729 was not covered by tests
std::stringstream ss;

Check warning on line 731 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L731

Added line #L731 was not covered by tests
ss << request->content.rdbuf();
pt::ptree inputTree, outputTree;
auto g = util::fail_guard([&]() {
std::ostringstream data;

Check warning on line 737 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L737

Added line #L737 was not covered by tests
pt::write_json(data, outputTree);
response->write(data.str());
});
try {

Check warning on line 742 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L742

Added line #L742 was not covered by tests
// TODO: Input Validation
pt::read_json(ss, inputTree);
std::string uuid = inputTree.get<std::string>("uuid");
BOOST_LOG(warning) << "Unpair: "sv << e.what();
outputTree.put("status", false);
outputTree.put("error", e.what());
return;

Check warning on line 752 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L752

Added line #L752 was not covered by tests
}
}
void
listClients(resp_https_t response, req_https_t request) {

Check warning on line 757 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L757

Added line #L757 was not covered by tests
if (!authenticate(response, request)) return;
print_req(request);

Check warning on line 760 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L760

Added line #L760 was not covered by tests
pt::ptree named_certs = nvhttp::get_all_clients();

Check warning on line 762 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L762

Added line #L762 was not covered by tests
pt::ptree outputTree;
outputTree.put("status", false);
auto g = util::fail_guard([&]() {
std::ostringstream data;

Check warning on line 769 in src/confighttp.cpp

Codecov / codecov/patch

src/confighttp.cpp#L769

Added line #L769 was not covered by tests
pt::write_json(data, outputTree);
response->write(data.str());
});
root.erase("root"s);
root.put("root.uniqueid", http::unique_id);
client_t &client = client_root;

Check warning on line 198 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L198

Added line #L198 was not covered by tests
pt::ptree node;
pt::ptree named_cert_nodes;
return;
}
pt::ptree tree;

Check warning on line 228 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L228

Added line #L228 was not covered by tests
try {
pt::read_json(config::nvhttp.file_state, tree);
}
* ```
*/
bool
pin(std::string pin, std::string name) {

Check warning on line 624 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L624

Added line #L624 was not covered by tests
pt::ptree tree;
if (map_id_sess.empty()) {
return false;
getservercert(sess, tree, pin);
// set up named cert
client_t &client = client_root;

Check warning on line 651 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L651

Added line #L651 was not covered by tests
named_cert_t named_cert;
named_cert.name = name;
named_cert.cert = sess.client.cert;
auto clientID = args.find("uniqueid"s);
if (clientID != std::end(args)) {
pair_status = 1;

Check warning on line 690 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L690

Added line #L690 was not covered by tests
}
}
}
pt::ptree
get_all_clients() {
pt::ptree named_cert_nodes;
client_t &client = client_root;

Check warning on line 788 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L786-L788

Added lines #L786 - L788 were not covered by tests
for (auto &named_cert : client.named_devices) {
pt::ptree named_cert_node;
named_cert_node.put("name"s, named_cert.name);
named_cert_nodes.push_back(std::make_pair(""s, named_cert_node));
}
return named_cert_nodes;

Check warning on line 796 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L796

Added line #L796 was not covered by tests
}
void
* ```
*/
int
unpair_client(std::string uuid) {
int removed = 0;
client_t &client = client_root;

Check warning on line 1217 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L1215-L1217

Added lines #L1215 - L1217 were not covered by tests
for (auto it = client.named_devices.begin(); it != client.named_devices.end();) {
if ((*it).uuid == uuid) {
// Find matching cert and remove it
for (auto cert = client.certs.begin(); cert != client.certs.end();) {
if ((*cert) == (*it).cert) {
cert = client.certs.erase(cert);
removed++;

Check warning on line 1224 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L1223-L1224

Added lines #L1223 - L1224 were not covered by tests
}
else {
++cert;
}
// And then remove the named cert
it = client.named_devices.erase(it);
removed++;

Check warning on line 1233 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L1232-L1233

Added lines #L1232 - L1233 were not covered by tests
}
else {
++it;
}
}
save_state();
load_state();
return removed;

Check warning on line 1242 in src/nvhttp.cpp

Codecov / codecov/patch

src/nvhttp.cpp#L1240-L1242

Added lines #L1240 - L1242 were not covered by tests
}
} // namespace nvhttp