Skip to content

Commit

Permalink
Keyring - Better Internal Store
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Dec 12, 2023
1 parent 820f117 commit 5789026
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 73 deletions.
4 changes: 2 additions & 2 deletions include/events/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ namespace Nickvision::Aura::Events
* @param e The Event to move
* @return this
*/
Event& operator=(Event&& e)
Event& operator=(Event&& e) noexcept
{
if (this != &e)
{
std::lock_guard<std::mutex> lock{ m_mutex };
std::lock_guard<std::mutex> lock2{ e.m_mutex };
std::swap(m_handlers, e.m_handlers);
m_handlers = std::move(e.m_handlers);
}
return *this;
}
Expand Down
7 changes: 1 addition & 6 deletions include/keyring/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ namespace Nickvision::Aura::Keyring
* @parma store The object to move
*/
Store(Store&& store) noexcept;
/**
* @brief Deconstructs a Store object.
*/
~Store();
/**
* @brief Gets whether or not the store object is valid.
*/
Expand Down Expand Up @@ -107,11 +103,10 @@ namespace Nickvision::Aura::Keyring
Store& operator=(Store&& store) noexcept;

private:
void loadDatabase();
mutable std::mutex m_mutex;
std::string m_name;
std::string m_password;
sqlite3* m_database;
std::shared_ptr<sqlite3> m_database;
std::filesystem::path m_path;

public:
Expand Down
109 changes: 44 additions & 65 deletions src/keyring/store.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "keyring/store.h"
#include <iostream>
#include "userdirectories.h"

namespace Nickvision::Aura::Keyring
Expand All @@ -23,41 +24,58 @@ namespace Nickvision::Aura::Keyring
m_database{ nullptr },
m_path{ getPathFromName(name) }
{
loadDatabase();
sqlite3* database{ nullptr };
if (sqlite3_open_v2(m_path.string().c_str(), &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr) == SQLITE_OK)
{
if (sqlite3_key(database, m_password.c_str(), static_cast<int>(m_password.size())) == SQLITE_OK)
{
if (sqlite3_exec(database, "CREATE TABLE IF NOT EXISTS credentials (id TEXT PRIMARY KEY, name TEXT, uri TEXT, username TEXT, password TEXT)", nullptr, nullptr, nullptr) == SQLITE_OK)
{
m_database = { database, [](sqlite3* sql)
{
sqlite3_close(sql);
}};
return;
}
else
{
std::cerr << "[STORE] Unable to exec create table command. Key may be invalid." << std::endl;
}
}
else
{
std::cerr << "[STORE] Unable to key the database." << std::endl;
}
sqlite3_close(database);
}
else
{
std::cerr << "[STORE] Unable to open the database." << std::endl;
}
}

Store::Store(const Store& store)
{
std::lock_guard<std::mutex> lock{ store.m_mutex };
m_name = store.m_name;
m_password = store.m_password;
m_database = nullptr;
m_database = store.m_database;
m_path = store.m_path;
loadDatabase();
}

Store::Store(Store&& store) noexcept
{
std::lock_guard<std::mutex> lock{ store.m_mutex };
m_name = std::move(store.m_name);
m_password = std::move(store.m_password);
m_database = nullptr;
m_database = std::move(store.m_database);
m_path = std::move(store.m_path);
loadDatabase();
}

Store::~Store()
{
if (m_database)
{
sqlite3_close(m_database);
}
}

bool Store::isValid() const
{
std::lock_guard<std::mutex> lock{ m_mutex };
return m_database;
return m_database ? true : false;
}

const std::string& Store::getName() const
Expand All @@ -79,7 +97,7 @@ namespace Nickvision::Aura::Keyring
if (m_database)
{
sqlite3_stmt* statement;
if (sqlite3_prepare_v2(m_database, "SELECT * FROM credentials", -1, &statement, nullptr) == SQLITE_OK)
if (sqlite3_prepare_v2(m_database.get(), "SELECT * FROM credentials", -1, &statement, nullptr) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
Expand All @@ -98,7 +116,7 @@ namespace Nickvision::Aura::Keyring
if (m_database)
{
sqlite3_stmt* statement;
if (sqlite3_prepare_v2(m_database, "SELECT * FROM credentials where id = ?", -1, &statement, nullptr) == SQLITE_OK)
if (sqlite3_prepare_v2(m_database.get(), "SELECT * FROM credentials where id = ?", -1, &statement, nullptr) == SQLITE_OK)
{
if (sqlite3_bind_int(statement, 1, id) == SQLITE_OK)
{
Expand All @@ -120,7 +138,7 @@ namespace Nickvision::Aura::Keyring
if (m_database)
{
sqlite3_stmt* statement;
if (sqlite3_prepare_v2(m_database, "SELECT * FROM credentials where name = ?", -1, &statement, nullptr) == SQLITE_OK)
if (sqlite3_prepare_v2(m_database.get(), "SELECT * FROM credentials where name = ?", -1, &statement, nullptr) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, name.c_str(), -1, SQLITE_TRANSIENT) == SQLITE_OK)
{
Expand All @@ -142,7 +160,7 @@ namespace Nickvision::Aura::Keyring
if (m_database)
{
sqlite3_stmt* statement;
if (sqlite3_prepare_v2(m_database, "INSERT INTO credentials (id, name, uri, username, password) VALUES (?,?,?,?,?)", -1, &statement, nullptr) == SQLITE_OK)
if (sqlite3_prepare_v2(m_database.get(), "INSERT INTO credentials (id, name, uri, username, password) VALUES (?,?,?,?,?)", -1, &statement, nullptr) == SQLITE_OK)
{
if (sqlite3_bind_int(statement, 1, credential.getId()) == SQLITE_OK)
{
Expand Down Expand Up @@ -173,7 +191,7 @@ namespace Nickvision::Aura::Keyring
if (m_database)
{
sqlite3_stmt* statement;
if (sqlite3_prepare_v2(m_database, "UPDATE credentials SET name = ?, uri = ?, username = ?, password = ? where id = ?", -1, &statement, nullptr) == SQLITE_OK)
if (sqlite3_prepare_v2(m_database.get(), "UPDATE credentials SET name = ?, uri = ?, username = ?, password = ? where id = ?", -1, &statement, nullptr) == SQLITE_OK)
{
if (sqlite3_bind_int(statement, 5, credential.getId()) == SQLITE_OK)
{
Expand Down Expand Up @@ -204,7 +222,7 @@ namespace Nickvision::Aura::Keyring
if (m_database)
{
sqlite3_stmt* statement;
if (sqlite3_prepare_v2(m_database, "DELETE FROM credentials WHERE id = ?", -1, &statement, nullptr) == SQLITE_OK)
if (sqlite3_prepare_v2(m_database.get(), "DELETE FROM credentials WHERE id = ?", -1, &statement, nullptr) == SQLITE_OK)
{
if (sqlite3_bind_int(statement, 1, id) == SQLITE_OK)
{
Expand All @@ -221,7 +239,7 @@ namespace Nickvision::Aura::Keyring
std::lock_guard<std::mutex> lock{ m_mutex };
if (m_database)
{
sqlite3_close(m_database);
sqlite3_close(m_database.get());
m_database = nullptr;
}
return std::filesystem::exists(m_path) ? std::filesystem::remove(m_path) : true;
Expand All @@ -235,13 +253,8 @@ namespace Nickvision::Aura::Keyring
std::lock_guard<std::mutex> lock2{ store.m_mutex };
m_name = store.m_name;
m_password = store.m_password;
if (m_database)
{
sqlite3_close(m_database);
}
m_database = nullptr;
m_database = store.m_database;
m_path = store.m_path;
loadDatabase();
}
return *this;
}
Expand All @@ -252,48 +265,14 @@ namespace Nickvision::Aura::Keyring
{
std::lock_guard<std::mutex> lock{ m_mutex };
std::lock_guard<std::mutex> lock2{ store.m_mutex };
std::swap(m_name, store.m_name);
std::swap(m_password, store.m_password);
if (m_database)
{
sqlite3_close(m_database);
}
m_database = nullptr;
std::swap(m_path, store.m_path);
loadDatabase();
m_name = std::move(store.m_name);
m_password = std::move(store.m_password);
m_database = std::move(store.m_database);
m_path = std::move(store.m_path);
}
return *this;
}

void Store::loadDatabase()
{
std::lock_guard<std::mutex> lock{ m_mutex };
if (sqlite3_open_v2(m_path.string().c_str(), &m_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr) == SQLITE_OK)
{
if (sqlite3_key(m_database, m_password.c_str(), static_cast<int>(m_password.size())) == SQLITE_OK)
{
if (sqlite3_exec(m_database, "CREATE TABLE IF NOT EXISTS credentials (id TEXT PRIMARY KEY, name TEXT, uri TEXT, username TEXT, password TEXT)", nullptr, nullptr, nullptr) == SQLITE_OK)
{
return;
}
else
{
std::cerr << "[STORE] Unable to exec create table command. Key may be invalid." << std::endl;
}
}
else
{
std::cerr << "[STORE] Unable to key the database." << std::endl;
}
sqlite3_close(m_database);
}
else
{
std::cerr << "[STORE] Unable to open the database." << std::endl;
}
m_database = nullptr;
}

bool Store::exists(const std::string& name)
{
return std::filesystem::exists(getPathFromName(name));
Expand Down

0 comments on commit 5789026

Please sign in to comment.