Login via access token.
This commit is contained in:
@@ -44,6 +44,16 @@ Dialog {
|
|||||||
placeholderText: "Password"
|
placeholderText: "Password"
|
||||||
echoMode: TextInput.Password
|
echoMode: TextInput.Password
|
||||||
|
|
||||||
|
onAccepted: accessTokenField.forceActiveFocus()
|
||||||
|
}
|
||||||
|
|
||||||
|
AutoTextField {
|
||||||
|
Layout.fillWidth: true
|
||||||
|
|
||||||
|
id: accessTokenField
|
||||||
|
|
||||||
|
placeholderText: "Access Token (Optional)"
|
||||||
|
|
||||||
onAccepted: deviceNameField.forceActiveFocus()
|
onAccepted: deviceNameField.forceActiveFocus()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,14 +62,19 @@ Dialog {
|
|||||||
|
|
||||||
id: deviceNameField
|
id: deviceNameField
|
||||||
|
|
||||||
placeholderText: "Device Name"
|
placeholderText: "Device Name (Optional)"
|
||||||
|
|
||||||
onAccepted: root.accept()
|
onAccepted: root.accept()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function doLogin() {
|
function doLogin() {
|
||||||
spectralController.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text)
|
if (accessTokenField.text !== "") {
|
||||||
|
console.log("Login using access token.")
|
||||||
|
spectralController.loginWithAccessToken(serverField.text, usernameField.text, accessTokenField.text, deviceNameField.text)
|
||||||
|
} else {
|
||||||
|
spectralController.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onClosed: destroy()
|
onClosed: destroy()
|
||||||
|
|||||||
@@ -103,6 +103,39 @@ void Controller::loginWithCredentials(QString serverAddr,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Controller::loginWithAccessToken(QString serverAddr,
|
||||||
|
QString user,
|
||||||
|
QString token,
|
||||||
|
QString deviceName) {
|
||||||
|
if (!user.isEmpty() && !token.isEmpty()) {
|
||||||
|
QUrl serverUrl(serverAddr);
|
||||||
|
|
||||||
|
Connection* conn = new Connection(this);
|
||||||
|
if (serverUrl.isValid()) {
|
||||||
|
conn->setHomeserver(serverUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
connect(conn, &Connection::connected, [=] {
|
||||||
|
AccountSettings account(conn->userId());
|
||||||
|
account.setKeepLoggedIn(true);
|
||||||
|
account.clearAccessToken(); // Drop the legacy - just in case
|
||||||
|
account.setHomeserver(conn->homeserver());
|
||||||
|
account.setDeviceId(conn->deviceId());
|
||||||
|
account.setDeviceName(deviceName);
|
||||||
|
if (!saveAccessTokenToKeyChain(account, conn->accessToken()))
|
||||||
|
qWarning() << "Couldn't save access token";
|
||||||
|
account.sync();
|
||||||
|
addConnection(conn);
|
||||||
|
setConnection(conn);
|
||||||
|
});
|
||||||
|
connect(conn, &Connection::networkError,
|
||||||
|
[=](QString error, QString, int, int) {
|
||||||
|
emit errorOccured("Network Error", error);
|
||||||
|
});
|
||||||
|
conn->connectWithToken(user, token, deviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Controller::logout(Connection* conn) {
|
void Controller::logout(Connection* conn) {
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
qCritical() << "Attempt to logout null connection";
|
qCritical() << "Attempt to logout null connection";
|
||||||
@@ -112,16 +145,24 @@ void Controller::logout(Connection* conn) {
|
|||||||
SettingsGroup("Accounts").remove(conn->userId());
|
SettingsGroup("Accounts").remove(conn->userId());
|
||||||
QFile(accessTokenFileName(AccountSettings(conn->userId()))).remove();
|
QFile(accessTokenFileName(AccountSettings(conn->userId()))).remove();
|
||||||
|
|
||||||
auto job = conn->callApi<LogoutJob>();
|
QKeychain::DeletePasswordJob job(qAppName());
|
||||||
connect(job, &LogoutJob::finished, conn, [=] {
|
job.setAutoDelete(true);
|
||||||
|
job.setKey(conn->userId());
|
||||||
|
QEventLoop loop;
|
||||||
|
QKeychain::DeletePasswordJob::connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
|
||||||
|
job.start();
|
||||||
|
loop.exec();
|
||||||
|
|
||||||
|
auto logoutJob = conn->callApi<LogoutJob>();
|
||||||
|
connect(logoutJob, &LogoutJob::finished, conn, [=] {
|
||||||
conn->stopSync();
|
conn->stopSync();
|
||||||
emit conn->stateChanged();
|
emit conn->stateChanged();
|
||||||
emit conn->loggedOut();
|
emit conn->loggedOut();
|
||||||
if (!m_connections.isEmpty())
|
if (!m_connections.isEmpty())
|
||||||
setConnection(m_connections[0]);
|
setConnection(m_connections[0]);
|
||||||
});
|
});
|
||||||
connect(job, &LogoutJob::failure, this, [=] {
|
connect(logoutJob, &LogoutJob::failure, this, [=] {
|
||||||
emit errorOccured("Server-side Logout Failed", job->errorString());
|
emit errorOccured("Server-side Logout Failed", logoutJob->errorString());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class Controller : public QObject {
|
|||||||
~Controller();
|
~Controller();
|
||||||
|
|
||||||
Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString);
|
Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString);
|
||||||
|
Q_INVOKABLE void loginWithAccessToken(QString, QString, QString, QString);
|
||||||
|
|
||||||
QVector<Connection*> connections() { return m_connections; }
|
QVector<Connection*> connections() { return m_connections; }
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include "events/accountdataevents.h"
|
#include "events/accountdataevents.h"
|
||||||
#include "events/roommessageevent.h"
|
#include "events/roommessageevent.h"
|
||||||
#include "events/typingevent.h"
|
#include "events/typingevent.h"
|
||||||
|
#include "events/reactionevent.h"
|
||||||
#include "jobs/downloadfilejob.h"
|
#include "jobs/downloadfilejob.h"
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
@@ -119,13 +120,13 @@ QString SpectralRoom::lastEvent() {
|
|||||||
for (auto i = messageEvents().rbegin(); i < messageEvents().rend(); i++) {
|
for (auto i = messageEvents().rbegin(); i < messageEvents().rend(); i++) {
|
||||||
const RoomEvent* evt = i->get();
|
const RoomEvent* evt = i->get();
|
||||||
|
|
||||||
if (is<RedactionEvent>(*evt))
|
if (is<RedactionEvent>(*evt) || is<ReactionEvent>(*evt))
|
||||||
continue;
|
continue;
|
||||||
if (evt->isRedacted())
|
if (evt->isRedacted())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (evt->isStateEvent() &&
|
if (evt->isStateEvent() &&
|
||||||
static_cast<const StateEventBase*>(evt)->repeatsState())
|
static_cast<const StateEventBase&>(*evt).repeatsState())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (connection()->isIgnored(user(evt->senderId())))
|
if (connection()->isIgnored(user(evt->senderId())))
|
||||||
|
|||||||
Reference in New Issue
Block a user