Switch to using QHttpServer for SSO flow

This commit is contained in:
Loren Burkholder 2023-06-28 14:40:16 -04:00
parent fa653bc078
commit 63fce40cfd
4 changed files with 19 additions and 5167 deletions

View file

@ -63,7 +63,6 @@ option(USE_BUNDLED_COEURL "Use a bundled version of the Curl wrapper"
option(USE_BUNDLED_LIBEVENT "Use the bundled version of libevent." ${HUNTER_ENABLED}) option(USE_BUNDLED_LIBEVENT "Use the bundled version of libevent." ${HUNTER_ENABLED})
option(USE_BUNDLED_LIBCURL "Use the bundled version of libcurl." ${HUNTER_ENABLED}) option(USE_BUNDLED_LIBCURL "Use the bundled version of libcurl." ${HUNTER_ENABLED})
option(USE_BUNDLED_RE2 "Use the bundled version of re2." ${HUNTER_ENABLED}) option(USE_BUNDLED_RE2 "Use the bundled version of re2." ${HUNTER_ENABLED})
option(USE_BUNDLED_CPPHTTPLIB "Use the bundled version of cpp-httplib." ON)
option(USE_BUNDLED_BLURHASH "Use the bundled version of blurhash." ON) option(USE_BUNDLED_BLURHASH "Use the bundled version of blurhash." ON)
include(CMakeDependentOption) include(CMakeDependentOption)
@ -243,7 +242,7 @@ endif()
# #
# Discover Qt dependencies. # Discover Qt dependencies.
# #
find_package(Qt6 6.5 COMPONENTS Core Widgets Gui LinguistTools Svg Multimedia Qml QuickControls2 REQUIRED) find_package(Qt6 6.5 COMPONENTS Core Widgets Gui LinguistTools Svg Multimedia Qml QuickControls2 HttpServer REQUIRED)
#find_package(Qt6QuickCompiler) #find_package(Qt6QuickCompiler)
find_package(Qt6DBus) find_package(Qt6DBus)
@ -834,14 +833,6 @@ endif()
target_include_directories(nheko PRIVATE src includes src/timeline/ src/ui/ src/encryption/ src/voip/) target_include_directories(nheko PRIVATE src includes src/timeline/ src/ui/ src/encryption/ src/voip/)
if (USE_BUNDLED_CPPHTTPLIB)
target_include_directories(nheko PRIVATE third_party/cpp-httplib-0.5.12)
target_sources(nheko PRIVATE third_party/cpp-httplib-0.5.12/httplib.h)
else()
find_package(httplib REQUIRED)
target_link_libraries(nheko PRIVATE httplib::httplib)
endif()
if (USE_BUNDLED_BLURHASH) if (USE_BUNDLED_BLURHASH)
target_include_directories(nheko PRIVATE third_party/blurhash) target_include_directories(nheko PRIVATE third_party/blurhash)
set(BLURHASH_SRC_FILES set(BLURHASH_SRC_FILES
@ -878,6 +869,7 @@ target_link_libraries(nheko PRIVATE
Qt::Multimedia Qt::Multimedia
Qt::Qml Qt::Qml
Qt::QuickControls2 Qt::QuickControls2
Qt::HttpServer
qt6keychain qt6keychain
nlohmann_json::nlohmann_json nlohmann_json::nlohmann_json
lmdbxx::lmdbxx lmdbxx::lmdbxx

View file

@ -4,50 +4,33 @@
#include "SSOHandler.h" #include "SSOHandler.h"
#include <QHttpServerResponse>
#include <QTimer> #include <QTimer>
#include <thread>
#include "Logging.h"
SSOHandler::SSOHandler(QObject *) SSOHandler::SSOHandler(QObject *)
: server{new QHttpServer}
{ {
QTimer::singleShot(120000, this, &SSOHandler::ssoFailed); QTimer::singleShot(120000, this, &SSOHandler::ssoFailed);
using namespace httplib; server->route("/sso", [this](const QHttpServerRequest &req) {
if (req.query().hasQueryItem(QStringLiteral("loginToken"))) {
svr.set_logger([](const Request &req, const Response &res) { emit ssoSuccess(req.query().queryItemValue(QStringLiteral("loginToken")).toStdString());
nhlog::net()->info("req: {}, res: {}", req.path, res.status); return tr("SSO success");
});
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 { } else {
res.set_content("Missing loginToken for SSO login!", "text/plain");
emit ssoFailed(); emit ssoFailed();
return tr("Missing loginToken for SSO login!");
} }
}); });
server->listen();
std::thread t([this]() { if (server->serverPorts().size() > 0)
this->port = svr.bind_to_any_port("localhost"); this->port = server->serverPorts().first();
svr.listen_after_bind();
});
t.detach();
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} }
SSOHandler::~SSOHandler() SSOHandler::~SSOHandler()
{ {
svr.stop(); // work around capturing a member of a deleted object
while (svr.is_running()) { auto s = server;
std::this_thread::sleep_for(std::chrono::milliseconds(1)); QTimer::singleShot(1000, [s] { s->deleteLater(); });
}
} }
std::string std::string

View file

@ -2,9 +2,11 @@
// //
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
#include "httplib.h" // #include "httplib.h"
#include <QHttpServer>
#include <QObject> #include <QObject>
#include <string> #include <string>
class SSOHandler final : public QObject class SSOHandler final : public QObject
@ -23,6 +25,6 @@ signals:
void ssoFailed(); void ssoFailed();
private: private:
httplib::Server svr; QHttpServer *server;
int port = 0; int port = 0;
}; };

File diff suppressed because it is too large Load diff