| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "SSOHandler.h" |
| 6 | |
| 7 | #include <QTimer> |
| 8 | |
| 9 | #include <thread> |
| 10 | |
| 11 | #include "Logging.h" |
| 12 | |
| 13 | SSOHandler::SSOHandler(QObject *) |
| 14 | { |
| 15 | QTimer::singleShot(120000, this, &SSOHandler::ssoFailed); |
| 16 | |
| 17 | using namespace httplib; |
| 18 | |
| 19 | svr.set_logger([](const Request &req, const Response &res) { |
| 20 | nhlog::net()->info("req: {}, res: {}", req.path, res.status); |
| 21 | }); |
| 22 | |
| 23 | svr.Get("/sso", [this](const Request &req, Response &res) { |
| 24 | if (req.has_param("loginToken")) { |
| 25 | auto val = req.get_param_value("loginToken"); |
| 26 | res.set_content("SSO success", "text/plain"); |
| 27 | emit ssoSuccess(val); |
| 28 | } else { |
| 29 | res.set_content("Missing loginToken for SSO login!", "text/plain"); |
| 30 | emit ssoFailed(); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | std::thread t([this]() { |
| 35 | this->port = svr.bind_to_any_port("localhost"); |
| 36 | svr.listen_after_bind(); |
| 37 | }); |
| 38 | t.detach(); |
| 39 | |
| 40 | while (!svr.is_running()) { |
| 41 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | SSOHandler::~SSOHandler() |
| 46 | { |
| 47 | svr.stop(); |
| 48 | while (svr.is_running()) { |
| 49 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | std::string |
| 54 | SSOHandler::url() const |
| 55 | { |
| 56 | return "http://localhost:" + std::to_string(port) + "/sso"; |
| 57 | } |
| 58 | |
| 59 | #include "moc_SSOHandler.cpp" |