summaryrefslogtreecommitdiffstats
path: root/src/SSOHandler.cpp
blob: 7d84404745c1b57ba4308a9efdf353ec363e4871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "SSOHandler.h"

#include <QTimer>

#include <thread>

#include "Logging.h"

SSOHandler::SSOHandler(QObject *)
{
    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();
        }
    });

    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));
    }
}

SSOHandler::~SSOHandler()
{
    svr.stop();
    while (svr.is_running()) {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
}

std::string
SSOHandler::url() const
{
    return "http://localhost:" + std::to_string(port) + "/sso";
}

#include "moc_SSOHandler.cpp"