add tryLoadPassphraseUserBackup function to choose between multiple user backups if more exist throw errors

This commit is contained in:
Dario 2020-06-30 12:29:15 +02:00
parent 045971e919
commit 60885ae99b
2 changed files with 35 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include "User.h" #include "User.h"
#include "UserBackups.h"
#include "sodium.h" #include "sodium.h"
@ -14,6 +15,8 @@
#include "Poco/Timestamp.h" #include "Poco/Timestamp.h"
namespace controller { namespace controller {
User::User(model::table::User* dbModel) User::User(model::table::User* dbModel)
: mPassword(nullptr), mGradidoKeyPair(nullptr), mCanDecryptPrivateKey(false), mGradidoCurrentBalance(0) : mPassword(nullptr), mGradidoKeyPair(nullptr), mCanDecryptPrivateKey(false), mGradidoCurrentBalance(0)
@ -227,6 +230,7 @@ namespace controller {
return setNewPassword(authenticated_encryption); return setNewPassword(authenticated_encryption);
} }
int User::setNewPassword(Poco::AutoPtr<AuthenticatedEncryption> passwd) int User::setNewPassword(Poco::AutoPtr<AuthenticatedEncryption> passwd)
{ {
std::unique_lock<std::shared_mutex> _lock(mSharedMutex); std::unique_lock<std::shared_mutex> _lock(mSharedMutex);
@ -284,6 +288,26 @@ namespace controller {
return result; 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<KeyPairEd25519>((*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) int User::checkIfVerificationEmailsShouldBeResend(const Poco::Util::Timer& timer)
{ {

View File

@ -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) // 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); 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); } inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); }
//! \brief try to load user from db via user_id //! \brief try to load user from db via user_id
//! \return count of found rows, should be 1 or 0 //! \return count of found rows, should be 1 or 0