Merge branch 'qtkeychain' into 'develop'
Keychain support See merge request b0/spectral!55
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -4,3 +4,6 @@
|
|||||||
[submodule "include/libQuotient"]
|
[submodule "include/libQuotient"]
|
||||||
path = include/libQuotient
|
path = include/libQuotient
|
||||||
url = https://github.com/quotient-im/libQuotient.git
|
url = https://github.com/quotient-im/libQuotient.git
|
||||||
|
[submodule "include/qtkeychain"]
|
||||||
|
path = include/qtkeychain
|
||||||
|
url = https://github.com/frankosterfeld/qtkeychain.git
|
||||||
|
|||||||
Submodule include/libQuotient updated: 93f0c8fe89...d3ddd394e8
1
include/qtkeychain
Submodule
1
include/qtkeychain
Submodule
Submodule include/qtkeychain added at e7118623a4
@@ -34,6 +34,8 @@ $$USE_SYSTEM_SORTFILTERPROXYMODEL {
|
|||||||
include(include/SortFilterProxyModel/SortFilterProxyModel.pri)
|
include(include/SortFilterProxyModel/SortFilterProxyModel.pri)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
include(include/qtkeychain/qt5keychain.pri)
|
||||||
|
|
||||||
INCLUDEPATH += include/hoedown
|
INCLUDEPATH += include/hoedown
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
include/hoedown/autolink.h \
|
include/hoedown/autolink.h \
|
||||||
|
|||||||
@@ -31,6 +31,8 @@
|
|||||||
#include <QtNetwork/QAuthenticator>
|
#include <QtNetwork/QAuthenticator>
|
||||||
#include <QtNetwork/QNetworkReply>
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
|
#include <keychain.h>
|
||||||
|
|
||||||
Controller::Controller(QObject* parent) : QObject(parent) {
|
Controller::Controller(QObject* parent) : QObject(parent) {
|
||||||
QApplication::setQuitOnLastWindowClosed(false);
|
QApplication::setQuitOnLastWindowClosed(false);
|
||||||
|
|
||||||
@@ -74,7 +76,7 @@ void Controller::loginWithCredentials(QString serverAddr,
|
|||||||
account.setHomeserver(conn->homeserver());
|
account.setHomeserver(conn->homeserver());
|
||||||
account.setDeviceId(conn->deviceId());
|
account.setDeviceId(conn->deviceId());
|
||||||
account.setDeviceName(deviceName);
|
account.setDeviceName(deviceName);
|
||||||
if (!saveAccessToken(account, conn->accessToken()))
|
if (!saveAccessTokenToKeyChain(account, conn->accessToken()))
|
||||||
qWarning() << "Couldn't save access token";
|
qWarning() << "Couldn't save access token";
|
||||||
account.sync();
|
account.sync();
|
||||||
addConnection(conn);
|
addConnection(conn);
|
||||||
@@ -147,7 +149,7 @@ void Controller::invokeLogin() {
|
|||||||
for (const auto& accountId : accounts) {
|
for (const auto& accountId : accounts) {
|
||||||
AccountSettings account{accountId};
|
AccountSettings account{accountId};
|
||||||
if (!account.homeserver().isEmpty()) {
|
if (!account.homeserver().isEmpty()) {
|
||||||
auto accessToken = loadAccessToken(account);
|
auto accessToken = loadAccessTokenFromKeyChain(account);
|
||||||
|
|
||||||
auto c = new Connection(account.homeserver(), this);
|
auto c = new Connection(account.homeserver(), this);
|
||||||
auto deviceName = account.deviceName();
|
auto deviceName = account.deviceName();
|
||||||
@@ -170,7 +172,7 @@ void Controller::invokeLogin() {
|
|||||||
emit initiated();
|
emit initiated();
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray Controller::loadAccessToken(const AccountSettings& account) {
|
QByteArray Controller::loadAccessTokenFromFile(const AccountSettings& account) {
|
||||||
QFile accountTokenFile{accessTokenFileName(account)};
|
QFile accountTokenFile{accessTokenFileName(account)};
|
||||||
if (accountTokenFile.open(QFile::ReadOnly)) {
|
if (accountTokenFile.open(QFile::ReadOnly)) {
|
||||||
if (accountTokenFile.size() < 1024)
|
if (accountTokenFile.size() < 1024)
|
||||||
@@ -186,8 +188,49 @@ QByteArray Controller::loadAccessToken(const AccountSettings& account) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Controller::saveAccessToken(const AccountSettings& account,
|
QByteArray Controller::loadAccessTokenFromKeyChain(
|
||||||
const QByteArray& accessToken) {
|
const AccountSettings& account) {
|
||||||
|
qDebug() << "Read the access token from the keychain for "
|
||||||
|
<< account.userId();
|
||||||
|
QKeychain::ReadPasswordJob job(qAppName());
|
||||||
|
job.setAutoDelete(false);
|
||||||
|
job.setKey(account.userId());
|
||||||
|
QEventLoop loop;
|
||||||
|
QKeychain::ReadPasswordJob::connect(&job, &QKeychain::Job::finished, &loop,
|
||||||
|
&QEventLoop::quit);
|
||||||
|
job.start();
|
||||||
|
loop.exec();
|
||||||
|
|
||||||
|
if (job.error() == QKeychain::Error::NoError) {
|
||||||
|
return job.binaryData();
|
||||||
|
}
|
||||||
|
|
||||||
|
qWarning() << "Could not read the access token from the keychain: "
|
||||||
|
<< qPrintable(job.errorString());
|
||||||
|
// no access token from the keychain, try token file
|
||||||
|
auto accessToken = loadAccessTokenFromFile(account);
|
||||||
|
if (job.error() == QKeychain::Error::EntryNotFound) {
|
||||||
|
if (!accessToken.isEmpty()) {
|
||||||
|
qDebug() << "Migrating the access token from file to the keychain for "
|
||||||
|
<< account.userId();
|
||||||
|
bool removed = false;
|
||||||
|
bool saved = saveAccessTokenToKeyChain(account, accessToken);
|
||||||
|
if (saved) {
|
||||||
|
QFile accountTokenFile{accessTokenFileName(account)};
|
||||||
|
removed = accountTokenFile.remove();
|
||||||
|
}
|
||||||
|
if (!(saved && removed)) {
|
||||||
|
qDebug() << "Migrating the access token from the file to the keychain "
|
||||||
|
"failed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Controller::saveAccessTokenToFile(const AccountSettings& account,
|
||||||
|
const QByteArray& accessToken) {
|
||||||
// (Re-)Make a dedicated file for access_token.
|
// (Re-)Make a dedicated file for access_token.
|
||||||
QFile accountTokenFile{accessTokenFileName(account)};
|
QFile accountTokenFile{accessTokenFileName(account)};
|
||||||
accountTokenFile.remove(); // Just in case
|
accountTokenFile.remove(); // Just in case
|
||||||
@@ -203,6 +246,28 @@ bool Controller::saveAccessToken(const AccountSettings& account,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Controller::saveAccessTokenToKeyChain(const AccountSettings& account,
|
||||||
|
const QByteArray& accessToken) {
|
||||||
|
qDebug() << "Save the access token to the keychain for " << account.userId();
|
||||||
|
QKeychain::WritePasswordJob job(qAppName());
|
||||||
|
job.setAutoDelete(false);
|
||||||
|
job.setKey(account.userId());
|
||||||
|
job.setBinaryData(accessToken);
|
||||||
|
QEventLoop loop;
|
||||||
|
QKeychain::WritePasswordJob::connect(&job, &QKeychain::Job::finished, &loop,
|
||||||
|
&QEventLoop::quit);
|
||||||
|
job.start();
|
||||||
|
loop.exec();
|
||||||
|
|
||||||
|
if (job.error()) {
|
||||||
|
qWarning() << "Could not save access token to the keychain: "
|
||||||
|
<< qPrintable(job.errorString());
|
||||||
|
return saveAccessTokenToFile(account, accessToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void Controller::joinRoom(Connection* c, const QString& alias) {
|
void Controller::joinRoom(Connection* c, const QString& alias) {
|
||||||
JoinRoomJob* joinRoomJob = c->joinRoom(alias);
|
JoinRoomJob* joinRoomJob = c->joinRoom(alias);
|
||||||
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::failure, [=] {
|
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::failure, [=] {
|
||||||
|
|||||||
@@ -70,8 +70,12 @@ class Controller : public QObject {
|
|||||||
QVector<Connection*> m_connections;
|
QVector<Connection*> m_connections;
|
||||||
QPointer<Connection> m_connection;
|
QPointer<Connection> m_connection;
|
||||||
|
|
||||||
QByteArray loadAccessToken(const AccountSettings& account);
|
QByteArray loadAccessTokenFromFile(const AccountSettings& account);
|
||||||
bool saveAccessToken(const AccountSettings& account,
|
QByteArray loadAccessTokenFromKeyChain(const AccountSettings& account);
|
||||||
|
|
||||||
|
bool saveAccessTokenToFile(const AccountSettings& account,
|
||||||
|
const QByteArray& accessToken);
|
||||||
|
bool saveAccessTokenToKeyChain(const AccountSettings& account,
|
||||||
const QByteArray& accessToken);
|
const QByteArray& accessToken);
|
||||||
void loadSettings();
|
void loadSettings();
|
||||||
void saveSettings() const;
|
void saveSettings() const;
|
||||||
|
|||||||
Reference in New Issue
Block a user