matrixion/src/SSOHandler.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-05-10 00:31:00 +03:00
#include "SSOHandler.h"
#include <QTimer>
#include <thread>
#include "Logging.h"
SSOHandler::SSOHandler(QObject *)
{
2021-09-18 01:22:33 +03:00
QTimer::singleShot(120000, this, &SSOHandler::ssoFailed);
using namespace httplib;
svr.set_logger([](const Request &req, const Response &res) {
nhlog::net()->info("req: {}, res: {}", req.path, res.status);
});
svr.Get("/sso", [this](const Request &req, Response &res) {
if (req.has_param("loginToken")) {
auto val = req.get_param_value("loginToken");
res.set_content("SSO success", "text/plain");
emit ssoSuccess(val);
} else {
res.set_content("Missing loginToken for SSO login!", "text/plain");
emit ssoFailed();
2020-05-10 00:31:00 +03:00
}
2021-09-18 01:22:33 +03:00
});
std::thread t([this]() {
this->port = svr.bind_to_any_port("localhost");
svr.listen_after_bind();
});
t.detach();
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
2020-05-10 00:31:00 +03:00
}
SSOHandler::~SSOHandler()
{
2021-09-18 01:22:33 +03:00
svr.stop();
while (svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
2020-05-10 00:31:00 +03:00
}
std::string
SSOHandler::url() const
{
2021-09-18 01:22:33 +03:00
return "http://localhost:" + std::to_string(port) + "/sso";
2020-05-10 00:31:00 +03:00
}