try to recover priv key from user backup in SigningTransaction if private key is missing

This commit is contained in:
Dario 2020-07-15 14:26:29 +02:00
parent 15d7cf5e61
commit 1b3cd9701c
3 changed files with 19 additions and 5 deletions

View File

@ -295,7 +295,7 @@ namespace controller {
//! \return -1 no matching entry found
//! \return -2 if user id is not set or invalid
//! \return 0 matching entry found
int User::tryLoadPassphraseUserBackup()
int User::tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr)
{
auto user_model = getModel();
if (user_model->getID() <= 0) return -2;
@ -308,7 +308,11 @@ namespace controller {
continue;
}
auto key_pair = std::unique_ptr<KeyPairEd25519>(user_backup->createGradidoKeyPair());
if (key_pair->isTheSame(user_model->getPublicKey())) {
if (createdKeyPair) {
*createdKeyPair = key_pair.get();
}
return 0;
}
}

View File

@ -46,7 +46,7 @@ namespace controller {
//! \return -1 no matching entry found
//! \return -2 user id invalid or net set
//! \return 0 matching entry found
int tryLoadPassphraseUserBackup();
int tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr);
inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); }
//! \brief try to load user from db via user_id

View File

@ -71,11 +71,21 @@ int SigningTransaction::run() {
//auto privKey = mUser->getPrivKey();
//if (!mUser->hasPrivKey()) {
auto gradido_key_pair = mNewUser->getGradidoKeyPair();
if(!gradido_key_pair || !gradido_key_pair->hasPrivateKey()) {
KeyPairEd25519** key_pair_ptr = nullptr;
if (!mNewUser->tryLoadPassphraseUserBackup(key_pair_ptr)) {
if(mNewUser->setGradidoKeyPair(*key_pair_ptr))
{
mNewUser->getModel()->updatePrivkey();
}
}
else {
addError(new Error("SigningTransaction", "user cannot decrypt private key"));
sendErrorsAsEmail();
return -2;
}
}
// get body bytes
model::messages::gradido::Transaction transaction;
auto bodyBytes = transaction.mutable_bodybytes();