diff --git a/src/cpp/controller/User.cpp b/src/cpp/controller/User.cpp index 500441b5d..d33bc09bc 100644 --- a/src/cpp/controller/User.cpp +++ b/src/cpp/controller/User.cpp @@ -1,4 +1,5 @@ #include "User.h" +#include "UserBackups.h" #include "sodium.h" @@ -14,6 +15,8 @@ #include "Poco/Timestamp.h" + + namespace controller { User::User(model::table::User* dbModel) : mPassword(nullptr), mGradidoKeyPair(nullptr), mCanDecryptPrivateKey(false), mGradidoCurrentBalance(0) @@ -227,6 +230,7 @@ namespace controller { return setNewPassword(authenticated_encryption); } + int User::setNewPassword(Poco::AutoPtr passwd) { std::unique_lock _lock(mSharedMutex); @@ -284,6 +288,26 @@ namespace controller { return result; } + //! \return -1 no matching entry found + //! \return -2 if user id is not set or invalid + //! \return 0 matching entry found, load as gradido key pair + int User::tryLoadPassphraseUserBackup() + { + auto user_model = getModel(); + if (user_model->getID() <= 0) return -2; + + auto backups = UserBackups::load(user_model->getID()); + if (backups.size() == 0) return -1; + for (auto it = backups.begin(); it != backups.end(); it++) { + auto key_pair = std::unique_ptr((*it)->createGradidoKeyPair()); + if (key_pair->isTheSame(user_model->getPublicKey())) { + setGradidoKeyPair(key_pair.release()); + return 0; + } + } + return -1; + } + int User::checkIfVerificationEmailsShouldBeResend(const Poco::Util::Timer& timer) { diff --git a/src/cpp/controller/User.h b/src/cpp/controller/User.h index 30242025c..12358dff4 100644 --- a/src/cpp/controller/User.h +++ b/src/cpp/controller/User.h @@ -37,6 +37,17 @@ namespace controller { // TODO: instead scheduling all, scheduling only for next day and run this function every day (own task for that) static int checkIfVerificationEmailsShouldBeResend(const Poco::Util::Timer& timer); + //! \brief try to find correct passphrase for this user from db + //! + //! select entries from user_backups db table belonging to user + //! calculate resulting public key + //! compare with public key from user + //! + //! \return -1 no matching entry found + //! \return -2 user id invalid or net set + //! \return 0 matching entry found, load as gradido key pair + int tryLoadPassphraseUserBackup(); + inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); } //! \brief try to load user from db via user_id //! \return count of found rows, should be 1 or 0