finis base implementation
Some checks failed
buildbot/AndroidBuilder_v8Qt6 Build finished.
buildbot/LinuxCMakeBuilderQt6 Build finished.
buildbot/DocsGenerator Build finished.

This commit is contained in:
Andrei Yankovich 2024-01-13 12:15:27 +01:00
parent 19dd73e3fa
commit 85d151c7c4
7 changed files with 70 additions and 16 deletions

View File

@ -8,14 +8,15 @@
#include "QASecret.h" #include "QASecret.h"
#include <QASecret/keystorage.h> #include <QASecret/keystorage.h>
#include <SecretDB.h>
namespace QASecret { namespace QASecret {
bool init() { bool init() {
initSecretServiceResources(); initSecretServiceResources();
DBSecret::init();
KeyStorage::initService(); KeyStorage::initService(std::make_unique<KeyStorage>(DBSecret::database()));
return true; return true;
} }

View File

@ -10,33 +10,45 @@
namespace QASecret { namespace QASecret {
KeyStorage::KeyStorage() {} KeyStorage::KeyStorage(const QSharedPointer<DBSecret::IDataBase> &db) {
_db = db;
}
QByteArray KeyStorage::add(const QByteArray &value, const QString &alias) { QByteArray KeyStorage::add(const QByteArray &value, const QString &alias) {
QByteArray&& key = QCryptographicHash::hash(value, QCryptographicHash::Sha256);
_storage[key] = value;
_aliases[alias] = value;
return key; if (auto record = _db->getRecordByAlias(alias, true)) {
record->setData(value);
if (_db->saveRecord(record)) {
return record->getHash();
}
}
return {};
} }
void KeyStorage::remove(const QByteArray &key) { void KeyStorage::remove(const QByteArray &key) {
_storage.remove(key); _db->removeRecordByKey(key);
} }
void KeyStorage::remove(const QString &alias) { void KeyStorage::remove(const QString &alias) {
_aliases.remove(alias); _db->removeRecordByAlias(alias);
} }
QByteArray KeyStorage::get(const QByteArray &key) { QByteArray KeyStorage::get(const QByteArray &key) {
auto&& result = _storage.value(key); if (auto&& result = _db->getRecordByHash(key)) {
return result->getData();
return result; }
return "";
} }
QByteArray KeyStorage::get(const QString &alias) { QByteArray KeyStorage::get(const QString &alias) {
auto&& result = _aliases.value(alias); if (auto&& result = _db->getRecordByAlias(alias)) {
return result->getData();
return result; }
return "";
} }
} }

View File

@ -8,18 +8,20 @@
#ifndef KEYSTORAGE_H #ifndef KEYSTORAGE_H
#define KEYSTORAGE_H #define KEYSTORAGE_H
#include "SecretDB/idatabase.h"
#include "qaservice.h" #include "qaservice.h"
#include <QHash> #include <QHash>
namespace QASecret { namespace QASecret {
/** /**
* @brief The KeyStorage class the storage store seccrets values on the simple key - value table. Where value is secrete and key is a sha256(secret) hash. * @brief The KeyStorage class the storage store seccrets values on the simple key - value table. Where value is secrete and key is a sha256(secret) hash.
*/ */
class KeyStorage: public QuasarAppUtils::Service<KeyStorage> class KeyStorage: public QuasarAppUtils::Service<KeyStorage>
{ {
public: public:
KeyStorage(); KeyStorage(const QSharedPointer<DBSecret::IDataBase>& db);
/** /**
* @brief add adds to storage new value, and return access key. * @brief add adds to storage new value, and return access key.
@ -56,8 +58,7 @@ public:
QByteArray get(const QString& alias); QByteArray get(const QString& alias);
private: private:
QHash<QByteArray, QByteArray> _storage; QSharedPointer<DBSecret::IDataBase> _db;
QHash<QString, QByteArray> _aliases;
}; };
} }

View File

@ -39,4 +39,12 @@ bool SecretDataBase::saveRecord(const QSharedPointer<iRecord> &record) {
return db()->replaceObject(record.staticCast<Record>(), true); return db()->replaceObject(record.staticCast<Record>(), true);
} }
bool SecretDataBase::removeRecordByAlias(const QString &alias) {
return deleteById<Record>(alias, &Record::setAlias);
}
bool SecretDataBase::removeRecordByKey(const QByteArray &hash) {
return deleteById<Record>(hash, &Record::setHash);
}
} }

View File

@ -25,6 +25,8 @@ public:
QSharedPointer<iRecord> getRecordByHash(const QByteArray &hash, bool ifNotExistsCreate) override; QSharedPointer<iRecord> getRecordByHash(const QByteArray &hash, bool ifNotExistsCreate) override;
bool saveRecord(const QSharedPointer<iRecord> &record) override; bool saveRecord(const QSharedPointer<iRecord> &record) override;
bool removeRecordByAlias(const QString &alias) override;
bool removeRecordByKey(const QByteArray &hash) override;
}; };
} }
#endif // BOTDATABASE_H #endif // BOTDATABASE_H

View File

@ -46,6 +46,20 @@ public:
* @return true if the object will save * @return true if the object will save
*/ */
virtual bool saveRecord(const QSharedPointer<iRecord>& record) = 0; virtual bool saveRecord(const QSharedPointer<iRecord>& record) = 0;
/**
* @brief removeRecordByAlias This method will remove record by alias.
* @param alias alias of the record that will be removed.
* @return true if function finished succesful else false.
*/
virtual bool removeRecordByAlias(const QString& alias) = 0;
/**
* @brief removeRecordByKey This method will remove record by hash key.
* @param hash is the key of the record that will be removed.
* @return true if function finished succesful else false.
*/
virtual bool removeRecordByKey(const QByteArray& hash) = 0;
}; };
} }

View File

@ -61,4 +61,20 @@ void DBTest::test() {
QVERIFY2(result2->getAlias() == result->getAlias(), "should be some as a prev object"); QVERIFY2(result2->getAlias() == result->getAlias(), "should be some as a prev object");
QVERIFY2(result2->getHash() == result->getHash(), "should be some as a prev object"); QVERIFY2(result2->getHash() == result->getHash(), "should be some as a prev object");
QVERIFY2(result2->getData() == result->getData(), "should be some as a prev object"); QVERIFY2(result2->getData() == result->getData(), "should be some as a prev object");
database->removeRecordByAlias("test");
QVERIFY2(!database->getRecordByAlias("test"), "should be empty on first run");
QVERIFY2(!database->getRecordByHash(dataHash), "should be empty on first run");
database->saveRecord(result);
result2 = database->getRecordByAlias("test");
QVERIFY2(result2->getAlias() == result->getAlias(), "should be some as a prev object");
QVERIFY2(result2->getHash() == result->getHash(), "should be some as a prev object");
QVERIFY2(result2->getData() == result->getData(), "should be some as a prev object");
database->removeRecordByKey(dataHash);
QVERIFY2(!database->getRecordByAlias("test"), "should be empty on first run");
QVERIFY2(!database->getRecordByHash(dataHash), "should be empty on first run");
} }