Added additional UserServices functionality
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
#include "userservices.h"
|
||||
#include "common/komplex_global.h"
|
||||
#include "coreservices.h"
|
||||
#include <QtConcurrent/qtconcurrentrun.h>
|
||||
#include <exception>
|
||||
#include <qcontainerfwd.h>
|
||||
#include <qstandardpaths.h>
|
||||
#include <qstringview.h>
|
||||
|
||||
UserServices::UserServices(QObject *parent) : QObject(parent)
|
||||
{
|
||||
@@ -8,6 +14,10 @@ UserServices::UserServices(QObject *parent) : QObject(parent)
|
||||
|
||||
UserServices::~UserServices()
|
||||
{
|
||||
if(m_wallet)
|
||||
{
|
||||
m_wallet->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
auto UserServices::setUserCredentials(const UserCredentials &credentials) -> void
|
||||
@@ -24,12 +34,135 @@ auto UserServices::errorString() const -> const QString &
|
||||
auto UserServices::setErrorString(const QString &errorString) -> void
|
||||
{
|
||||
if (m_errorString == errorString)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_errorString = errorString;
|
||||
emit errorStringChanged();
|
||||
}
|
||||
|
||||
auto UserServices::instance() const -> QWeakPointer<UserServices>
|
||||
auto UserServices::savePassword(const QString &username, const QByteArray &password) -> void
|
||||
{
|
||||
QFuture saveAttempt = m_wallet->write
|
||||
(
|
||||
KOMPLEX_KEYCHAIN_SERVICE_NAME,
|
||||
username,
|
||||
password
|
||||
);
|
||||
}
|
||||
|
||||
auto UserServices::readPassword(const QString &username) -> QFuture<QByteArray>
|
||||
{
|
||||
return m_wallet->read(KOMPLEX_KEYCHAIN_SERVICE_NAME, username);
|
||||
}
|
||||
|
||||
auto UserServices::readToken(const QString &username) -> QFuture<UserCredentials>
|
||||
{
|
||||
return QtConcurrent::run
|
||||
(
|
||||
[this, username] () -> UserCredentials
|
||||
{
|
||||
UserCredentials credentials;
|
||||
|
||||
QFuture readAttempt = m_wallet->read
|
||||
(
|
||||
KOMPLEX_KEYCHAIN_SERVICE_NAME,
|
||||
username + QStringLiteral("_token")
|
||||
);
|
||||
|
||||
QString _username = username;
|
||||
|
||||
readAttempt
|
||||
.then
|
||||
(
|
||||
[this, &credentials, _username] (const QByteArray &value) -> void
|
||||
{
|
||||
auto parts = value.split(PART_SEPARATOR);
|
||||
QDateTime expiry;
|
||||
|
||||
if(parts.count() == 1)
|
||||
{
|
||||
setErrorString(QStringLiteral("Token malformed"));
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
if(parts[0].length() != sizeof(qint64))
|
||||
{
|
||||
setErrorString(QStringLiteral("Token expiry malformed"));
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
QByteArray numericData = parts[0];
|
||||
qint64 numericConversion = 0;
|
||||
|
||||
for(int i = 0; i < sizeof(qint64); ++i)
|
||||
{
|
||||
numericConversion |=
|
||||
(
|
||||
static_cast<qint64>
|
||||
(
|
||||
(
|
||||
static_cast<qint8>(numericData[i]) &
|
||||
0xFF
|
||||
)
|
||||
<< (i * sizeof(qint8))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
expiry = QDateTime::fromMSecsSinceEpoch(numericConversion);
|
||||
|
||||
if(parts[1].isEmpty() && parts.count() == 3)
|
||||
{
|
||||
parts[1] = parts[2];
|
||||
}
|
||||
|
||||
credentials = UserCredentials
|
||||
{
|
||||
.username = _username.toUtf8(),
|
||||
.sessionToken = parts[1],
|
||||
.expiry = expiry
|
||||
};
|
||||
}
|
||||
)
|
||||
.onFailed
|
||||
(
|
||||
[this] () -> void
|
||||
{
|
||||
setErrorString(m_wallet->errorString());
|
||||
throw std::exception();
|
||||
}
|
||||
);
|
||||
|
||||
readAttempt.waitForFinished();
|
||||
|
||||
return credentials;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
auto UserServices::saveToken(const QString &username, const QByteArray &token, const QDateTime &expiry) -> void
|
||||
{
|
||||
QByteArray data;
|
||||
qint64 numericData = expiry.toMSecsSinceEpoch();
|
||||
|
||||
for(int i = 0; i < sizeof(qint64); ++i)
|
||||
{
|
||||
data[i] = 0xFF & static_cast<qint8>(numericData >> (i * sizeof(qint8)));
|
||||
}
|
||||
|
||||
data += PART_SEPARATOR + token;
|
||||
|
||||
QFuture saveAttempt = m_wallet->write
|
||||
(
|
||||
KOMPLEX_KEYCHAIN_SERVICE_NAME,
|
||||
username + QString("_token"),
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
auto UserServices::instance() -> QWeakPointer<UserServices>
|
||||
{
|
||||
if(!s_pointer)
|
||||
{
|
||||
@@ -53,16 +186,19 @@ auto UserServices::login(const QString &username, const QByteArray &password) ->
|
||||
{
|
||||
return QtConcurrent::run
|
||||
(
|
||||
[this, username = std::move(username), password = std::move(password)] () -> UserCredentials
|
||||
[this, username, password] () -> UserCredentials
|
||||
{
|
||||
QSharedPointer<QNetworkAccessManager> manager = CoreServices::networkAccessManager().toStrongRef();
|
||||
|
||||
if(!manager)
|
||||
{
|
||||
Q_EMIT loginFailed();
|
||||
return {};
|
||||
setErrorString(QStringLiteral("Network manager reference deleted"));
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
saveDefaultUsername(username);
|
||||
|
||||
QNetworkRequest request
|
||||
(
|
||||
QUrl
|
||||
@@ -139,10 +275,77 @@ auto UserServices::login(const QString &username, const QByteArray &password) ->
|
||||
.expiry = QDateTime::currentDateTime()
|
||||
};
|
||||
|
||||
savePassword(username, password);
|
||||
saveToken(username, credentials.sessionToken, credentials.expiry);
|
||||
setUserCredentials(credentials);
|
||||
|
||||
Q_EMIT loginComplete();
|
||||
return credentials;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
auto UserServices::saveDefaultUsername(const QString &username) -> void
|
||||
{
|
||||
QDir configDirectory(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
|
||||
|
||||
if(!configDirectory.exists())
|
||||
{
|
||||
if(!configDirectory.mkpath(configDirectory.absolutePath()))
|
||||
{
|
||||
setErrorString(QStringLiteral("Could not make config directory"));
|
||||
throw std::exception();
|
||||
}
|
||||
}
|
||||
|
||||
QFile usernameFile(configDirectory.absoluteFilePath(QStringLiteral("uname")));
|
||||
|
||||
if(!usernameFile.open(QFile::ReadWrite | QFile::Truncate))
|
||||
{
|
||||
setErrorString(QStringLiteral("Could not open uname file"));
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
QByteArray data = username.toUtf8().toBase64();
|
||||
|
||||
if(usernameFile.write(data) != data.length())
|
||||
{
|
||||
setErrorString(QStringLiteral("Could not write uname file"));
|
||||
}
|
||||
|
||||
usernameFile.close();
|
||||
}
|
||||
|
||||
auto UserServices::defaultUsername() -> QFuture<QString>
|
||||
{
|
||||
return QtConcurrent::run
|
||||
(
|
||||
[this] () -> QString
|
||||
{
|
||||
QDir configDirectory(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
|
||||
|
||||
if(!configDirectory.exists())
|
||||
{
|
||||
if(!configDirectory.mkpath(configDirectory.absolutePath()))
|
||||
{
|
||||
setErrorString(QStringLiteral("Could not make config directory"));
|
||||
throw std::exception();
|
||||
}
|
||||
}
|
||||
|
||||
QFile usernameFile(configDirectory.absoluteFilePath(QStringLiteral("uname")));
|
||||
|
||||
if(!usernameFile.open(QFile::ReadOnly))
|
||||
{
|
||||
setErrorString(QStringLiteral("Could not open uname file"));
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
QByteArray data = QByteArray::fromBase64(usernameFile.readAll());
|
||||
|
||||
usernameFile.close();
|
||||
|
||||
return QString(data);
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user