diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-03-25 22:24:04 +0100 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-03-26 22:52:01 +0100 |
commit | 1b5d6216beddd17c820240dd4bdeaf9c47624cf6 (patch) | |
tree | 7372e314d6ebae4bbed7e687ac7de1c0a552561a /lib | |
parent | 7033aa5ad8152f77164a4c644837d1fe44aa0430 (diff) | |
download | libquotient-1b5d6216beddd17c820240dd4bdeaf9c47624cf6.tar.gz libquotient-1b5d6216beddd17c820240dd4bdeaf9c47624cf6.zip |
Connection: loginWithToken(); connectWithToken() -> assumeIdentity()
Part of #388 backport.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/connection.cpp | 75 | ||||
-rw-r--r-- | lib/connection.h | 23 |
2 files changed, 77 insertions, 21 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index 99d5c04a..6da04118 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -111,8 +111,10 @@ class Connection::Private .value("cache_type").toString() != "json"; bool lazyLoading = false; - void connectWithToken(const QString& userId, const QString& accessToken, - const QString& deviceId); + template <typename... LoginArgTs> + void loginToServer(LoginArgTs&&... loginArgs); + void assumeIdentity(const QString& newUserId, const QString& accessToken, + const QString& deviceId); template <typename EventT> EventT* unpackAccountData() const @@ -221,6 +223,19 @@ void Connection::resolveServer(const QString& mxidOrDomain) }); } +inline UserIdentifier makeUserIdentifier(const QString& id) +{ + return { QStringLiteral("m.id.user"), { { QStringLiteral("user"), id } } }; +} + +inline UserIdentifier make3rdPartyIdentifier(const QString& medium, + const QString& address) +{ + return { QStringLiteral("m.id.thirdparty"), + { { QStringLiteral("medium"), medium }, + { QStringLiteral("address"), address } } }; +} + void Connection::connectToServer(const QString& user, const QString& password, const QString& initialDeviceName, const QString& deviceId) @@ -230,23 +245,22 @@ void Connection::connectToServer(const QString& user, const QString& password, doConnectToServer(user, password, initialDeviceName, deviceId); }); } + void Connection::doConnectToServer(const QString& user, const QString& password, const QString& initialDeviceName, const QString& deviceId) { - auto loginJob = callApi<LoginJob>(QStringLiteral("m.login.password"), - UserIdentifier { QStringLiteral("m.id.user"), - {{ QStringLiteral("user"), user }} }, - password, /*token*/ "", deviceId, initialDeviceName); - connect(loginJob, &BaseJob::success, this, - [this, loginJob] { - d->connectWithToken(loginJob->userId(), loginJob->accessToken(), - loginJob->deviceId()); - }); - connect(loginJob, &BaseJob::failure, this, - [this, loginJob] { - emit loginError(loginJob->errorString(), loginJob->rawDataSample()); - }); + d->loginToServer(LoginFlows::Password.type, makeUserIdentifier(user), + password, /*token*/ "", deviceId, initialDeviceName); +} + +void Connection::loginWithToken(const QByteArray& loginToken, + const QString& initialDeviceName, + const QString& deviceId) +{ + d->loginToServer(LoginFlows::Token.type, + makeUserIdentifier(/*user is encoded in loginToken*/ {}), + /*password*/ "", loginToken, deviceId, initialDeviceName); } void Connection::syncLoopIteration() @@ -258,8 +272,15 @@ void Connection::connectWithToken(const QString& userId, const QString& accessToken, const QString& deviceId) { + assumeIdentity(userId, accessToken, deviceId); +} + +void Connection::assumeIdentity(const QString& userId, + const QString& accessToken, + const QString& deviceId) +{ checkAndConnect(userId, - [=] { d->connectWithToken(userId, accessToken, deviceId); }); + [=] { d->assumeIdentity(userId, accessToken, deviceId); }); } void Connection::reloadCapabilities() @@ -294,11 +315,25 @@ bool Connection::loadingCapabilities() const return d->capabilities.roomVersions.omitted(); } -void Connection::Private::connectWithToken(const QString& user, - const QString& accessToken, - const QString& deviceId) +template <typename... LoginArgTs> +void Connection::Private::loginToServer(LoginArgTs&&... loginArgs) +{ + auto loginJob = + q->callApi<LoginJob>(std::forward<LoginArgTs>(loginArgs)...); + connect(loginJob, &BaseJob::success, q, [this, loginJob] { + assumeIdentity(loginJob->userId(), loginJob->accessToken(), + loginJob->deviceId()); + }); + connect(loginJob, &BaseJob::failure, q, [this, loginJob] { + emit q->loginError(loginJob->errorString(), loginJob->rawDataSample()); + }); +} + +void Connection::Private::assumeIdentity(const QString& newUserId, + const QString& accessToken, + const QString& deviceId) { - userId = user; + userId = newUserId; q->user(); // Creates a User object for the local user data->setToken(accessToken.toLatin1()); data->setDeviceId(deviceId); diff --git a/lib/connection.h b/lib/connection.h index ad228bf0..782ffd2c 100644 --- a/lib/connection.h +++ b/lib/connection.h @@ -403,6 +403,18 @@ namespace QMatrixClient std::forward<JobArgTs>(jobArgs)...); } + /** Get a request URL for a job with specified type and arguments + * + * This calls JobT::makeRequestUrl() prepending the connection's + * homeserver to the list of arguments. + */ + template <typename JobT, typename... JobArgTs> + QUrl getUrlForApi(JobArgTs&&... jobArgs) const + { + return JobT::makeRequestUrl(homeserver(), + std::forward<JobArgTs>(jobArgs)...); + } + /** Generate a new transaction id. Transaction id's are unique within * a single Connection object */ @@ -438,7 +450,16 @@ namespace QMatrixClient void connectToServer(const QString& user, const QString& password, const QString& initialDeviceName, const QString& deviceId = {}); - void connectWithToken(const QString& userId, const QString& accessToken, + void loginWithToken(const QByteArray& loginToken, + const QString& initialDeviceName, + const QString& deviceId = {}); + void assumeIdentity(const QString& userId, const QString& accessToken, + const QString& deviceId); + /*! @deprecated + * Use assumeIdentity() if you have an access token or + * loginWithToken() if you have a login token. + */ + void connectWithToken(const QString& userId, const QString& accessToken, const QString& deviceId); /// Explicitly request capabilities from the server void reloadCapabilities(); |