Page MenuHomePhorge

D285.1771768171.diff
No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None

D285.1771768171.diff

diff --git a/README.md b/README.md
--- a/README.md
+++ b/README.md
@@ -57,7 +57,6 @@
libkazv is not planning to support these functionalities:
-- Single Sign On
- Spaces
- Stories
diff --git a/src/client/actions/auth.hpp b/src/client/actions/auth.hpp
--- a/src/client/actions/auth.hpp
+++ b/src/client/actions/auth.hpp
@@ -1,6 +1,6 @@
/*
* This file is part of libkazv.
- * SPDX-FileCopyrightText: 2020-2022 Tusooa Zhu <tusooa@kazv.moe>
+ * SPDX-FileCopyrightText: 2020-2026 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -17,6 +17,7 @@
namespace Kazv
{
ClientResult updateClient(ClientModel m, LoginAction a);
+ ClientResult updateClient(ClientModel m, MLoginTokenLoginAction a);
ClientResult updateClient(ClientModel m, TokenLoginAction a);
ClientResult processResponse(ClientModel m, LoginResponse r);
ClientResult updateClient(ClientModel m, LogoutAction a);
diff --git a/src/client/actions/auth.cpp b/src/client/actions/auth.cpp
--- a/src/client/actions/auth.cpp
+++ b/src/client/actions/auth.cpp
@@ -30,6 +30,21 @@
return { m, lager::noop };
}
+ ClientResult updateClient(ClientModel m, MLoginTokenLoginAction a)
+ {
+ m.addJob(LoginJob{a.serverUrl,
+ "m.login.token"s, // type
+ std::nullopt, // identifier
+ std::nullopt, // password
+ a.loginToken, // token, not used
+ {}, // device id, not used
+ a.deviceName.value_or("libkazv")}
+ .withData(json{
+ {"serverUrl", a.serverUrl},
+ }));
+ return { m, lager::noop };
+ }
+
ClientResult processResponse(ClientModel m, LoginResponse r)
{
if (! r.success()) {
diff --git a/src/client/client-model.hpp b/src/client/client-model.hpp
--- a/src/client/client-model.hpp
+++ b/src/client/client-model.hpp
@@ -218,6 +218,16 @@
std::string deviceId;
};
+ /**
+ * Login using the m.token.login flow.
+ */
+ struct MLoginTokenLoginAction
+ {
+ std::string serverUrl;
+ std::string loginToken;
+ std::optional<std::string> deviceName;
+ };
+
struct LogoutAction {};
struct HardLogoutAction {};
diff --git a/src/client/client.hpp b/src/client/client.hpp
--- a/src/client/client.hpp
+++ b/src/client/client.hpp
@@ -245,6 +245,27 @@
PromiseT tokenLogin(std::string homeserver, std::string username,
std::string token, std::string deviceId) const;
+ /**
+ * Login using a login token.
+ *
+ * This will create a new session on the homeserver.
+ *
+ * If the returned Promise resolves successfully, this will
+ * call `startSyncing()`.
+ *
+ * @param homeserver The base url of the homeserver. E.g. `https://tusooa.xyz`.
+ * @param loginToken The login token.
+ * @param deviceName Optionally, a custom device name. If empty, `libkazv`
+ * will be used.
+ * @return A Promise that resolves when logging in successfully, or
+ * when there is an error.
+ */
+ PromiseT mLoginTokenLogin(
+ std::string homeserver,
+ std::string loginToken,
+ std::optional<std::string> deviceName
+ ) const;
+
/**
* Get the `shouldSync` field of current ClientModel.
*
diff --git a/src/client/client.cpp b/src/client/client.cpp
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -102,6 +102,26 @@
return p1;
}
+ auto Client::mLoginTokenLogin(
+ std::string homeserver,
+ std::string loginToken,
+ std::optional<std::string> deviceName
+ ) const -> PromiseT
+ {
+ auto p1 = m_ctx.dispatch(MLoginTokenLoginAction{
+ homeserver, loginToken, deviceName});
+ p1
+ .then([that=toEventLoop()](auto stat) {
+ if (! stat.success()) {
+ return;
+ }
+ that.startSyncing();
+ });
+
+ return p1;
+ }
+
+
auto Client::tokenLogin(std::string homeserver, std::string username,
std::string token, std::string deviceId) const
-> PromiseT
diff --git a/src/client/clientfwd.hpp b/src/client/clientfwd.hpp
--- a/src/client/clientfwd.hpp
+++ b/src/client/clientfwd.hpp
@@ -23,6 +23,7 @@
struct LoginAction;
struct TokenLoginAction;
+ struct MLoginTokenLoginAction;
struct LogoutAction;
struct HardLogoutAction;
struct GetWellknownAction;
@@ -88,6 +89,7 @@
LoginAction,
TokenLoginAction,
+ MLoginTokenLoginAction,
LogoutAction,
HardLogoutAction,
GetWellknownAction,
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -86,6 +86,7 @@
client/room/read-receipt-test.cpp
client/room/undecrypted-events-test.cpp
client/encryption-benchmark-test.cpp
+ client/login-test.cpp
client/logout-test.cpp
client/room/pinned-events-test.cpp
client/get-versions-test.cpp
diff --git a/src/tests/client/client-test-util.hpp b/src/tests/client/client-test-util.hpp
--- a/src/tests/client/client-test-util.hpp
+++ b/src/tests/client/client-test-util.hpp
@@ -15,6 +15,7 @@
#include <client/client-model.hpp>
#include <base/basejob.hpp>
+#include <catch2/catch_test_macros.hpp>
using namespace Kazv;
ClientModel createTestClientModel();
diff --git a/src/tests/client/login-test.cpp b/src/tests/client/login-test.cpp
new file mode 100644
--- /dev/null
+++ b/src/tests/client/login-test.cpp
@@ -0,0 +1,29 @@
+/*
+ * This file is part of libkazv.
+ * SPDX-FileCopyrightText: 2026 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+#include "client.hpp"
+#include "action-mock-utils.hpp"
+#include "client-test-util.hpp"
+#include "actions/auth.hpp"
+#include <catch2/catch_test_macros.hpp>
+
+using namespace Kazv;
+
+TEST_CASE("MLoginTokenLoginAction", "[client][login]")
+{
+ auto [next, _] = updateClient(ClientModel{}, MLoginTokenLoginAction{"https://m.example.com", "some-token", std::nullopt});
+ assert1Job(next);
+ for1stJob(next, [](const BaseJob &j) {
+ auto url = j.url();
+ REQUIRE(url.find("/_matrix/client/v3/login") != std::string::npos);
+ auto b = json::parse(std::get<BytesBody>(j.requestBody()));
+ REQUIRE(b.at("type").template get<std::string>() == "m.login.token");
+ REQUIRE(b.at("token").template get<std::string>() == "some-token");
+ REQUIRE(!b.contains("identifier"));
+ REQUIRE(!b.contains("password"));
+ });
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Feb 22, 5:49 AM (14 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1125039
Default Alt Text
D285.1771768171.diff (6 KB)

Event Timeline