diff --git a/native/android/app/src/cpp/CommSecureStore.cpp b/native/android/app/src/cpp/CommSecureStore.cpp --- a/native/android/app/src/cpp/CommSecureStore.cpp +++ b/native/android/app/src/cpp/CommSecureStore.cpp @@ -28,13 +28,12 @@ namespace comm { -void CommSecureStore::set(const std::string key, const std::string value) - const { +void CommSecureStore::set(const std::string key, const std::string value) { NativeAndroidAccessProvider::runTask( [=]() { CommSecureStoreJavaClass::set(key, value); }); } -folly::Optional CommSecureStore::get(const std::string key) const { +folly::Optional CommSecureStore::get(const std::string key) { folly::Optional value; NativeAndroidAccessProvider::runTask( [=, &value]() { value = CommSecureStoreJavaClass::get(key); }); diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp @@ -30,17 +30,15 @@ } void DatabaseManager::clearSensitiveData() { - comm::CommSecureStore commSecureStore{}; - commSecureStore.set(commSecureStore.userID, ""); - commSecureStore.set(commSecureStore.deviceID, ""); - commSecureStore.set(commSecureStore.commServicesAccessToken, ""); + CommSecureStore::set(CommSecureStore::userID, ""); + CommSecureStore::set(CommSecureStore::deviceID, ""); + CommSecureStore::set(CommSecureStore::commServicesAccessToken, ""); SQLiteQueryExecutor::clearSensitiveData(); NotificationsCryptoModule::clearSensitiveData(); DatabaseManager::setDatabaseStatusAsWorkable(); } void DatabaseManager::initializeQueryExecutor(std::string &databasePath) { - comm::CommSecureStore commSecureStore{}; try { SQLiteQueryExecutor::initialize(databasePath); DatabaseManager::getQueryExecutor(); @@ -48,16 +46,16 @@ Logger::log("Database manager initialized"); } catch (...) { folly::Optional databaseManagerStatus = - commSecureStore.get(DATABASE_MANAGER_STATUS_KEY); + CommSecureStore::get(DATABASE_MANAGER_STATUS_KEY); if (!databaseManagerStatus.hasValue() || databaseManagerStatus.value() == DB_MANAGER_WORKABLE) { - commSecureStore.set( + CommSecureStore::set( DATABASE_MANAGER_STATUS_KEY, DB_MANAGER_FIRST_FAILURE); Logger::log("Database manager initialization issue, terminating app"); throw; } if (databaseManagerStatus.value() == DB_MANAGER_FIRST_FAILURE) { - commSecureStore.set( + CommSecureStore::set( DATABASE_MANAGER_STATUS_KEY, DB_MANAGER_SECOND_FAILURE); Logger::log( "Database manager initialization issue, app proceeding, but " @@ -68,35 +66,31 @@ } void DatabaseManager::setDatabaseStatusAsWorkable() { - comm::CommSecureStore commSecureStore{}; - commSecureStore.set(DATABASE_MANAGER_STATUS_KEY, DB_MANAGER_WORKABLE); + CommSecureStore::set(DATABASE_MANAGER_STATUS_KEY, DB_MANAGER_WORKABLE); } void DatabaseManager::indicateQueryExecutorCreation() { - comm::CommSecureStore commSecureStore{}; folly::Optional databaseManagerStatus = - commSecureStore.get(DATABASE_MANAGER_STATUS_KEY); + CommSecureStore::get(DATABASE_MANAGER_STATUS_KEY); if (!databaseManagerStatus.hasValue() || databaseManagerStatus.value() != DB_OPERATIONS_FAILURE) { // creating query executor means that schema was created without error, // but this doesn't imply that schema has a proper structure, // and operation will not crash, this case should not be overridden - commSecureStore.set(DATABASE_MANAGER_STATUS_KEY, DB_MANAGER_WORKABLE); + CommSecureStore::set(DATABASE_MANAGER_STATUS_KEY, DB_MANAGER_WORKABLE); } } bool DatabaseManager::checkIfDatabaseNeedsDeletion() { - comm::CommSecureStore commSecureStore{}; folly::Optional databaseManagerStatus = - commSecureStore.get(DATABASE_MANAGER_STATUS_KEY); + CommSecureStore::get(DATABASE_MANAGER_STATUS_KEY); return databaseManagerStatus.hasValue() && (databaseManagerStatus.value() == DB_MANAGER_SECOND_FAILURE || databaseManagerStatus.value() == DB_OPERATIONS_FAILURE); } void DatabaseManager::reportDBOperationsFailure() { - comm::CommSecureStore commSecureStore{}; - commSecureStore.set(DATABASE_MANAGER_STATUS_KEY, DB_OPERATIONS_FAILURE); + CommSecureStore::set(DATABASE_MANAGER_STATUS_KEY, DB_OPERATIONS_FAILURE); } } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -1386,9 +1386,8 @@ void SQLiteQueryExecutor::initialize(std::string &databasePath) { std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() { SQLiteQueryExecutor::sqliteFilePath = databasePath; - CommSecureStore commSecureStore{}; folly::Optional maybeEncryptionKey = - commSecureStore.get(SQLiteQueryExecutor::secureStoreEncryptionKeyID); + CommSecureStore::get(SQLiteQueryExecutor::secureStoreEncryptionKeyID); if (file_exists(databasePath) && maybeEncryptionKey) { SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value(); @@ -1399,10 +1398,9 @@ } void SQLiteQueryExecutor::assign_encryption_key() { - CommSecureStore commSecureStore{}; std::string encryptionKey = comm::crypto::Tools::generateRandomHexString( SQLiteQueryExecutor::sqlcipherEncryptionKeySize); - commSecureStore.set( + CommSecureStore::set( SQLiteQueryExecutor::secureStoreEncryptionKeyID, encryptionKey); SQLiteQueryExecutor::encryptionKey = encryptionKey; } diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h @@ -22,7 +22,6 @@ const int codeVersion{300}; std::unique_ptr cryptoThread; - CommSecureStore secureStore; const std::string secureStoreAccountDataKey = "cryptoAccountDataKey"; const std::string publicCryptoAccountID = "publicCryptoAccountID"; std::unique_ptr cryptoModule; diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -296,10 +296,10 @@ jsi::Value CommCoreModule::initializeCryptoAccount(jsi::Runtime &rt) { folly::Optional storedSecretKey = - this->secureStore.get(this->secureStoreAccountDataKey); + CommSecureStore::get(this->secureStoreAccountDataKey); if (!storedSecretKey.hasValue()) { storedSecretKey = crypto::Tools::generateRandomString(64); - this->secureStore.set( + CommSecureStore::set( this->secureStoreAccountDataKey, storedSecretKey.value()); } @@ -923,10 +923,10 @@ [this, promise, userIDStr, deviceIDStr, accessTokenStr]() { std::string error; try { - this->secureStore.set(this->secureStore.userID, userIDStr); - this->secureStore.set(this->secureStore.deviceID, deviceIDStr); - this->secureStore.set( - this->secureStore.commServicesAccessToken, accessTokenStr); + CommSecureStore::set(CommSecureStore::userID, userIDStr); + CommSecureStore::set(CommSecureStore::deviceID, deviceIDStr); + CommSecureStore::set( + CommSecureStore::commServicesAccessToken, accessTokenStr); } catch (const std::exception &e) { error = e.what(); } @@ -953,17 +953,17 @@ std::string accessToken; try { folly::Optional userIDOpt = - this->secureStore.get(this->secureStore.userID); + CommSecureStore::get(CommSecureStore::userID); if (userIDOpt.hasValue()) { userID = userIDOpt.value(); } folly::Optional deviceIDOpt = - this->secureStore.get(this->secureStore.deviceID); + CommSecureStore::get(CommSecureStore::deviceID); if (deviceIDOpt.hasValue()) { deviceID = deviceIDOpt.value(); } - folly::Optional accessTokenOpt = this->secureStore.get( - this->secureStore.commServicesAccessToken); + folly::Optional accessTokenOpt = + CommSecureStore::get(CommSecureStore::commServicesAccessToken); if (accessTokenOpt.hasValue()) { accessToken = accessTokenOpt.value(); } @@ -1014,8 +1014,8 @@ taskType job = [this, promise, accessTokenStr]() { std::string error; try { - this->secureStore.set( - this->secureStore.commServicesAccessToken, accessTokenStr); + CommSecureStore::set( + CommSecureStore::commServicesAccessToken, accessTokenStr); } catch (const std::exception &e) { error = e.what(); } @@ -1038,8 +1038,7 @@ taskType job = [this, promise]() { std::string error; try { - this->secureStore.set( - this->secureStore.commServicesAccessToken, ""); + CommSecureStore::set(CommSecureStore::commServicesAccessToken, ""); } catch (const std::exception &e) { error = e.what(); } diff --git a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp --- a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp +++ b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp @@ -139,8 +139,7 @@ } std::string NotificationsCryptoModule::getPicklingKey() { - CommSecureStore secureStore{}; - folly::Optional picklingKey = secureStore.get( + folly::Optional picklingKey = CommSecureStore::get( NotificationsCryptoModule::secureStoreNotificationsAccountDataKey); if (!picklingKey.hasValue()) { throw std::runtime_error( @@ -178,9 +177,8 @@ } // There is no reason to check if the key is already present since if we are // in this place in the code we are about to create new account - CommSecureStore secureStore{}; std::string picklingKey = crypto::Tools::generateRandomString(64); - secureStore.set( + CommSecureStore::set( NotificationsCryptoModule::secureStoreNotificationsAccountDataKey, picklingKey); diff --git a/native/cpp/CommonCpp/Tools/CommSecureStore.h b/native/cpp/CommonCpp/Tools/CommSecureStore.h --- a/native/cpp/CommonCpp/Tools/CommSecureStore.h +++ b/native/cpp/CommonCpp/Tools/CommSecureStore.h @@ -8,11 +8,11 @@ class CommSecureStore { public: - void set(const std::string key, const std::string value) const; - folly::Optional get(const std::string key) const; - const std::string commServicesAccessToken = "accessToken"; - const std::string userID = "userID"; - const std::string deviceID = "deviceID"; + static void set(const std::string key, const std::string value); + static folly::Optional get(const std::string key); + inline static const std::string commServicesAccessToken = "accessToken"; + inline static const std::string userID = "userID"; + inline static const std::string deviceID = "deviceID"; }; } // namespace comm diff --git a/native/ios/Comm/CommSecureStore.mm b/native/ios/Comm/CommSecureStore.mm --- a/native/ios/Comm/CommSecureStore.mm +++ b/native/ios/Comm/CommSecureStore.mm @@ -7,8 +7,7 @@ namespace comm { -void CommSecureStore::set(const std::string key, const std::string value) - const { +void CommSecureStore::set(const std::string key, const std::string value) { NSString *nsKey = [NSString stringWithCString:key.c_str() encoding:[NSString defaultCStringEncoding]]; @@ -18,7 +17,7 @@ [[CommSecureStoreIOSWrapper sharedInstance] set:nsKey value:nsValue]; } -folly::Optional CommSecureStore::get(const std::string key) const { +folly::Optional CommSecureStore::get(const std::string key) { NSString *nsKey = [NSString stringWithCString:key.c_str() encoding:[NSString defaultCStringEncoding]];