mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-27 13:38:49 +03:00
Switch to using QHttpServer for SSO flow
This commit is contained in:
parent
fa653bc078
commit
63fce40cfd
4 changed files with 19 additions and 5167 deletions
|
@ -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_LIBCURL "Use the bundled version of libcurl." ${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)
|
||||
|
||||
include(CMakeDependentOption)
|
||||
|
@ -243,7 +242,7 @@ endif()
|
|||
#
|
||||
# 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(Qt6DBus)
|
||||
|
||||
|
@ -834,14 +833,6 @@ endif()
|
|||
|
||||
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)
|
||||
target_include_directories(nheko PRIVATE third_party/blurhash)
|
||||
set(BLURHASH_SRC_FILES
|
||||
|
@ -878,6 +869,7 @@ target_link_libraries(nheko PRIVATE
|
|||
Qt::Multimedia
|
||||
Qt::Qml
|
||||
Qt::QuickControls2
|
||||
Qt::HttpServer
|
||||
qt6keychain
|
||||
nlohmann_json::nlohmann_json
|
||||
lmdbxx::lmdbxx
|
||||
|
|
|
@ -4,50 +4,33 @@
|
|||
|
||||
#include "SSOHandler.h"
|
||||
|
||||
#include <QHttpServerResponse>
|
||||
#include <QTimer>
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "Logging.h"
|
||||
|
||||
SSOHandler::SSOHandler(QObject *)
|
||||
: server{new QHttpServer}
|
||||
{
|
||||
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);
|
||||
server->route("/sso", [this](const QHttpServerRequest &req) {
|
||||
if (req.query().hasQueryItem(QStringLiteral("loginToken"))) {
|
||||
emit ssoSuccess(req.query().queryItemValue(QStringLiteral("loginToken")).toStdString());
|
||||
return tr("SSO success");
|
||||
} else {
|
||||
res.set_content("Missing loginToken for SSO login!", "text/plain");
|
||||
emit ssoFailed();
|
||||
return tr("Missing loginToken for SSO login!");
|
||||
}
|
||||
});
|
||||
|
||||
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));
|
||||
}
|
||||
server->listen();
|
||||
if (server->serverPorts().size() > 0)
|
||||
this->port = server->serverPorts().first();
|
||||
}
|
||||
|
||||
SSOHandler::~SSOHandler()
|
||||
{
|
||||
svr.stop();
|
||||
while (svr.is_running()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
// work around capturing a member of a deleted object
|
||||
auto s = server;
|
||||
QTimer::singleShot(1000, [s] { s->deleteLater(); });
|
||||
}
|
||||
|
||||
std::string
|
||||
|
|
|
@ -2,9 +2,11 @@
|
|||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "httplib.h"
|
||||
// #include "httplib.h"
|
||||
|
||||
#include <QHttpServer>
|
||||
#include <QObject>
|
||||
|
||||
#include <string>
|
||||
|
||||
class SSOHandler final : public QObject
|
||||
|
@ -23,6 +25,6 @@ signals:
|
|||
void ssoFailed();
|
||||
|
||||
private:
|
||||
httplib::Server svr;
|
||||
QHttpServer *server;
|
||||
int port = 0;
|
||||
};
|
||||
|
|
5125
third_party/cpp-httplib-0.5.12/httplib.h
vendored
5125
third_party/cpp-httplib-0.5.12/httplib.h
vendored
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue