Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 3ae45c8

Browse files
committed
added import/export functions (untested)
1 parent 9ad0787 commit 3ae45c8

13 files changed

+132
-11
lines changed

src/datasync/authenticator.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
#include "authenticator.h"
22
#include "remoteconnector.h"
33

4+
#include <QtCore/QBuffer>
5+
46
using namespace QtDataSync;
57

68
Authenticator::Authenticator(QObject *parent) :
79
QObject(parent)
810
{}
911

12+
QByteArray Authenticator::exportUserData() const
13+
{
14+
QBuffer buffer;
15+
buffer.open(QIODevice::WriteOnly);
16+
exportUserData(&buffer);
17+
auto res = buffer.data();
18+
buffer.close();
19+
return res;
20+
}
21+
22+
GenericTask<void> Authenticator::importUserData(QByteArray data)
23+
{
24+
QBuffer buffer(&data);
25+
buffer.open(QIODevice::ReadOnly);
26+
auto res = importUserData(&buffer);
27+
buffer.close();
28+
return res;
29+
}
30+
1031
GenericTask<void> Authenticator::resetIdentity(const QVariant &extraData, bool resetLocalStore)
1132
{
1233
QFutureInterface<QVariant> futureInterface;

src/datasync/authenticator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <QtCore/qobject.h>
88
#include <QtCore/qpointer.h>
9+
#include <QtCore/qiodevice.h>
910

1011
#include <functional>
1112
#include <type_traits>
@@ -23,6 +24,15 @@ class Q_DATASYNC_EXPORT Authenticator : public QObject
2324
//! Constructor
2425
explicit Authenticator(QObject *parent = nullptr);
2526

27+
//! Export all user-related remote data to the given device
28+
virtual void exportUserData(QIODevice *device) const = 0;
29+
//! Export all user-related remote data
30+
QByteArray exportUserData() const;
31+
//! Import user-related remote data from the given device
32+
virtual GenericTask<void> importUserData(QIODevice *device) = 0;
33+
//! Import user-related remote data
34+
GenericTask<void> importUserData(QByteArray data);
35+
2636
protected:
2737
//! Call this method to reset the users identity.
2838
GenericTask<void> resetIdentity(const QVariant &extraData = {}, bool resetLocalStore = true);

src/datasync/encryptor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Encryptor : public QObject
2121
//! Called from the engine to finalize the encryptor
2222
virtual void finalize();
2323

24-
//! Returns the current encryption key
24+
//! Returns the current encryption key (must be thread-safe)
2525
virtual QByteArray key() const = 0;
2626
//! Sets the encryption key
2727
virtual void setKey(const QByteArray &key) = 0;

src/datasync/exceptions.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ QException *SetupLockedException::clone() const
5151
return new SetupLockedException(this);
5252
}
5353

54+
InvalidDataException::InvalidDataException(const QString &what) :
55+
_what(what.toUtf8())
56+
{}
57+
58+
InvalidDataException::InvalidDataException(const QByteArray &what) :
59+
_what(what)
60+
{}
61+
62+
const char *InvalidDataException::what() const noexcept
63+
{
64+
return _what.constData();
65+
}
66+
67+
void InvalidDataException::raise() const
68+
{
69+
throw *this;
70+
}
71+
72+
QException *InvalidDataException::clone() const
73+
{
74+
return new InvalidDataException(_what);
75+
}
76+
5477
DataSyncException::DataSyncException(const QString &what) :
5578
QException(),
5679
_what(what.toUtf8())

src/datasync/exceptions.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ class Q_DATASYNC_EXPORT SetupLockedException : public SetupException
5858
SetupLockedException(const SetupException *cloneFrom);
5959
};
6060

61+
//! Exception thrown if imported user data is invalid
62+
class Q_DATASYNC_EXPORT InvalidDataException : public QException
63+
{
64+
public:
65+
//! Constructor with error message
66+
InvalidDataException(const QString &what);
67+
68+
public:
69+
//! @inherit{std::exception::what}
70+
const char *what() const noexcept final;
71+
72+
//! @inherit{QException::raise}
73+
void raise() const final;
74+
//! @inherit{QException::clone}
75+
QException *clone() const final;
76+
77+
private:
78+
InvalidDataException(const QByteArray &what);
79+
const QByteArray _what;
80+
};
81+
6182
//! Exception thrown if something goes wrong when using the async store
6283
class Q_DATASYNC_EXPORT DataSyncException : public QException
6384
{

src/datasync/qtinyaesencryptor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ using namespace QtDataSync;
99
QTinyAesEncryptor::QTinyAesEncryptor(QObject *parent) :
1010
Encryptor(parent),
1111
_defaults(nullptr),
12-
_key()
12+
_key(),
13+
_keyMutex()
1314
{}
1415

1516
void QTinyAesEncryptor::initialize(Defaults *defaults)
@@ -25,11 +26,13 @@ void QTinyAesEncryptor::initialize(Defaults *defaults)
2526

2627
QByteArray QTinyAesEncryptor::key() const
2728
{
29+
QMutexLocker _(&_keyMutex);
2830
return _key;
2931
}
3032

3133
void QTinyAesEncryptor::setKey(const QByteArray &key)
3234
{
35+
QMutexLocker _(&_keyMutex);
3336
if((quint32)_key.size() != QTinyAes::KEYSIZE)
3437
throw InvalidKeyException();
3538
_key = key;

src/datasync/qtinyaesencryptor_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "encryptor.h"
66

77
#include <QtCore/QException>
8+
#include <QtCore/QMutex>
89

910
namespace QtDataSync {
1011

@@ -42,6 +43,7 @@ class Q_DATASYNC_EXPORT QTinyAesEncryptor : public Encryptor
4243
private:
4344
Defaults *_defaults;
4445
QByteArray _key;
46+
mutable QMutex _keyMutex;
4547
};
4648

4749
}

src/datasync/remoteconnector.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class RemoteConnectorPrivate {
1010
RemoteConnectorPrivate();
1111

1212
Defaults *defaults;
13+
Encryptor *cryptor;
1314
};
1415
}
1516

@@ -22,13 +23,19 @@ RemoteConnector::RemoteConnector(QObject *parent) :
2223

2324
RemoteConnector::~RemoteConnector() {}
2425

25-
void RemoteConnector::initialize(Defaults *defaults, Encryptor *)
26+
void RemoteConnector::initialize(Defaults *defaults, Encryptor *cryptor)
2627
{
2728
d->defaults = defaults;
29+
d->cryptor = cryptor;
2830
}
2931

3032
void RemoteConnector::finalize() {}
3133

34+
Encryptor *RemoteConnector::cryptor() const
35+
{
36+
return d->cryptor;
37+
}
38+
3239
void RemoteConnector::resetUserId(QFutureInterface<QVariant> futureInterface, const QVariant &extraData, bool resetLocalStore)
3340
{
3441
if(resetLocalStore)//resync is always done, only clear explicitly needed
@@ -61,5 +68,6 @@ QByteArray RemoteConnector::getDeviceId() const
6168

6269

6370
RemoteConnectorPrivate::RemoteConnectorPrivate() :
64-
defaults(nullptr)
71+
defaults(nullptr),
72+
cryptor(nullptr)
6573
{}

src/datasync/remoteconnector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class Q_DATASYNC_EXPORT RemoteConnector : public QObject
4040
//! Called from the engine to finalize the connector
4141
virtual void finalize();
4242

43+
//! Returns the encryptor this connector uses (can be nullptr)
44+
virtual Encryptor *cryptor() const;
45+
4346
//! Method to create an authenticator for this connector
4447
virtual Authenticator *createAuthenticator(Defaults *defaults, QObject *parent) = 0;
4548

src/datasync/wsauthenticator.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "wsauthenticator_p.h"
44
#include "wsremoteconnector_p.h"
55

6+
#include <QtCore/QJsonDocument>
7+
68
using namespace QtDataSync;
79

810
WsAuthenticator::WsAuthenticator(WsRemoteConnector *connector, Defaults *defaults, QObject *parent) :
@@ -18,6 +20,34 @@ WsAuthenticator::WsAuthenticator(WsRemoteConnector *connector, Defaults *default
1820

1921
WsAuthenticator::~WsAuthenticator() {}
2022

23+
void WsAuthenticator::exportUserData(QIODevice *device) const
24+
{
25+
QJsonObject data;
26+
data[QStringLiteral("key")] = QString::fromUtf8(d->connector->cryptor()->key().toBase64());
27+
data[QStringLiteral("identity")] = QString::fromUtf8(userIdentity());
28+
29+
device->write(QJsonDocument(data).toJson(QJsonDocument::Indented));
30+
}
31+
32+
GenericTask<void> WsAuthenticator::importUserData(QIODevice *device)
33+
{
34+
QJsonParseError error;
35+
auto data = QJsonDocument::fromJson(device->readAll(), &error);
36+
if(error.error != QJsonParseError::NoError) {
37+
QFutureInterface<QVariant> futureInterface;
38+
futureInterface.reportStarted();
39+
futureInterface.reportException(InvalidDataException(error.errorString()));
40+
futureInterface.reportFinished();
41+
return futureInterface;
42+
}
43+
44+
auto obj = data.object();
45+
auto key = QByteArray::fromBase64(obj[QStringLiteral("key")].toString().toUtf8());
46+
QMetaObject::invokeMethod(d->connector->cryptor(), "setKey", Qt::QueuedConnection,
47+
Q_ARG(QByteArray, key));
48+
return setUserIdentity(obj[QStringLiteral("identity")].toString().toUtf8());
49+
}
50+
2151
bool WsAuthenticator::isRemoteEnabled() const
2252
{
2353
return d->settings->value(WsRemoteConnector::keyRemoteEnabled, true).toBool();

0 commit comments

Comments
 (0)