Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 601785a
Author: Nick Logozzo <[email protected]>
Date:   Mon Dec 11 11:07:35 2023 -0500

    Keyring - Better Store

commit 5d69c41
Author: Nick Logozzo <[email protected]>
Date:   Mon Dec 11 01:25:00 2023 -0500

    Keyring - Use sqlcipher instead of sqlitecpp

commit df9e15e
Author: Nick Logozzo <[email protected]>
Date:   Sun Dec 10 22:47:15 2023 -0500

    Tests - Add Store Tests

commit c12e617
Author: Nick Logozzo <[email protected]>
Date:   Sun Dec 10 22:46:55 2023 -0500

    Update store.h
  • Loading branch information
nlogozzo committed Dec 11, 2023
1 parent 128d983 commit 0db9e17
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 148 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ find_package(Intl REQUIRED)
find_package(jsoncpp REQUIRED)
find_package(CURL REQUIRED)
find_package(maddy REQUIRED)
find_package(SQLiteCpp REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Intl::Intl JsonCpp::JsonCpp CURL::libcurl maddy::maddy SQLiteCpp)
find_package(sqlcipher REQUIRED CONFIG)
target_link_libraries(${PROJECT_NAME} PUBLIC Intl::Intl JsonCpp::JsonCpp CURL::libcurl maddy::maddy sqlcipher::sqlcipher)
if(LINUX)
find_package(PkgConfig REQUIRED)
find_package(libsecret REQUIRED CONFIG)
Expand All @@ -83,6 +83,7 @@ if(NOT SKIP_TESTS)
tests/eventtests.cpp
tests/main.cpp
tests/networktests.cpp
tests/storetests.cpp
tests/stringtests.cpp
tests/systemcredentialstests.cpp
tests/updatertests.cpp
Expand Down
1 change: 0 additions & 1 deletion conan/conanfile-linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ jsoncpp/1.9.5
libcurl/8.4.0
maddy/1.3.0
sqlcipher/4.5.1
sqlitecpp/3.3.1
glib/2.78.1
libsecret/0.20.5
libuuid/1.0.3
Expand Down
1 change: 0 additions & 1 deletion conan/conanfile-windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ jsoncpp/1.9.5
libcurl/8.4.0
maddy/1.3.0
sqlcipher/4.5.1
sqlitecpp/3.3.1
gtest/1.14.0

[generators]
Expand Down
39 changes: 20 additions & 19 deletions include/keyring/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <optional>
#include <string>
#include <vector>
#include <SQLiteCpp/SQLiteCpp.h>
#include <sqlcipher/sqlite3.h>
#include "credential.h"

namespace Nickvision::Aura::Keyring
Expand All @@ -18,16 +18,31 @@ namespace Nickvision::Aura::Keyring
class Store
{
public:
/**
* @brief Constructs a Store object. The isValid() function should be called to ensure the object is valid.
* @param name The name of the store
* @param password The password of the store
*/
Store(const std::string& name, const std::string& password);
/**
* @brief Copies a Store object.
* @parma store The object to move
* @brief Deconstructs a Store object.
*/
Store(const Store& store);
/**
* @brief Moves a Store object.
* @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.
*/
bool isValid() const;
/**
* @brief Gets the name of the store.
* @return The name of the store
Expand Down Expand Up @@ -89,13 +104,14 @@ namespace Nickvision::Aura::Keyring
* @param store The Store to move
* @return this
*/
Store& operator=(Store&& store);
Store& operator=(Store&& store) noexcept;

private:
Store(const std::string& name, const std::shared_ptr<SQLite::Database>& database);
void loadDatabase();
mutable std::mutex m_mutex;
std::shared_ptr<SQLite::Database> m_database;
std::string m_name;
std::string m_password;
sqlite3* m_database;
std::filesystem::path m_path;

public:
Expand All @@ -104,21 +120,6 @@ namespace Nickvision::Aura::Keyring
* @return The directory for stores
*/
static std::filesystem::path getStoreDir();
/**
* @brief Creates a new store.
* @param name The name of the store
* @param password The password of the store
* @param overwrite Whether or not to overwrite a store that already exists with the provided name
* @return The created Store object, std::nullopt if creation failed
*/
static std::optional<Store> create(const std::string& name, const std::string& password, bool overwrite = true);
/**
* @brief Loads a store.
* @param name The name of the store
* @param password The password of the store
* @return The loaded Store object, std::nullopt if loading failed
*/
static std::optional<Store> load(const std::string& name, const std::string& password);
/**
* @brief Gets whether or not a store exists with the provided name.
* @param name The name of the store to check
Expand Down
2 changes: 1 addition & 1 deletion src/keyring/credential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Nickvision::Aura::Keyring
}

Credential::Credential(const std::string& name, const std::string& uri, const std::string& username, const std::string& password)
: m_id{ (int)std::hash<std::string>()(StringHelpers::newGuid()) },
: m_id{ static_cast<int>(std::hash<std::string>()(StringHelpers::newGuid())) },
m_name{ name },
m_uri{ uri },
m_username{ username },
Expand Down
51 changes: 17 additions & 34 deletions src/keyring/keyring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ namespace Nickvision::Aura::Keyring
{
if (m_store.destroy())
{
return SystemCredentials::deleteCredential(m_store.getName());
(void)SystemCredentials::deleteCredential(m_store.getName()); //keyring's password may not be in system credentials
return true;
}
return false;
}
Expand All @@ -59,46 +60,27 @@ namespace Nickvision::Aura::Keyring
if (password.empty())
{
std::optional<Credential> cred{ SystemCredentials::getCredential(name) };
password = cred ? cred->getPassword() : "";
}
//If password not empty (a.k.a user-provided or system-provided), get store
if (!password.empty())
{
//Load store
std::optional<Store> storeLoad{ Store::load(name, password) };
if (storeLoad)
if (cred.has_value())
{
return { { *storeLoad } };
password = cred->getPassword();
}
//Store unable to be loaded, create a new one
else
{
std::optional<Store> storeCreate{ Store::create(name, password) };
if (storeCreate)
{
return { { *storeCreate } };
}
else
{
return std::nullopt;
}
cred = SystemCredentials::addCredential(name);
password = cred ? cred->getPassword() : "";
}
}
//Create a new store with a new password from the system credential store
else
//If password not empty (a.k.a user-provided or system-provided), get store
if (!password.empty())
{
std::optional<Credential> cred{ SystemCredentials::addCredential(name) };
if (cred)
Store store{ name, password };
if (store.isValid())
{
return { { store } };
}
else
{
std::optional<Store> storeCreate{ Store::create(name, cred->getPassword()) };
if (storeCreate)
{
return { { *storeCreate } };
}
else
{
return std::nullopt;
}
return std::nullopt;
}
}
return std::nullopt;
Expand All @@ -113,7 +95,8 @@ namespace Nickvision::Aura::Keyring
{
if (Store::destroy(name))
{
return SystemCredentials::deleteCredential(name);
(void)SystemCredentials::deleteCredential(name);
return true;
}
return false;
}
Expand Down
Loading

0 comments on commit 0db9e17

Please sign in to comment.