diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index 69ee486..1c54d47 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -95,7 +95,7 @@ namespace pEp { if (Adapter::on_sync_thread() && !msg) { semaphore.try_wait(); - PEP_STATUS status = PassphraseCache::messageToSend(passphrase_cache, Adapter::session()); + PEP_STATUS status = PassphraseCache::config_next_passphrase(); // if the cache has no valid passphrase ask the app if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) @@ -105,6 +105,11 @@ namespace pEp { return status; } + if (Adapter::on_sync_thread()) { + // a passphrase worked, reset passphrase_cache iterator + PassphraseCache::config_next_passphrase(true); + } + for (auto target : targets) { ::message *_msg = nullptr; if (msg) { diff --git a/passphrase_cache.cc b/passphrase_cache.cc index 5e629c2..fd436fd 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -1,4 +1,5 @@ #include +#include "Adapter.hh" #include "passphrase_cache.hh" pEp::PassphraseCache pEp::passphrase_cache; @@ -117,16 +118,23 @@ namespace pEp { return _which->passphrase.c_str(); } - PEP_STATUS PassphraseCache::messageToSend(PassphraseCache& cache, PEP_SESSION session) + PEP_STATUS PassphraseCache::config_next_passphrase(bool reset) { static pEp::PassphraseCache _copy; static bool new_copy = true; + + if (reset) { + new_copy = true; + return PEP_STATUS_OK; + } + if (new_copy) { - _copy = cache; + _copy = passphrase_cache; new_copy = false; } + try { - ::config_passphrase(session, _copy.latest_passphrase()); + ::config_passphrase(Adapter::session(), _copy.latest_passphrase()); return PEP_STATUS_OK; } catch (pEp::PassphraseCache::Empty&) { diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 8c5807c..345c394 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -53,26 +53,12 @@ namespace pEp { // adds the stored passphrase to the cache, which will not timeout const char *add_stored(const std::string& passphrase); - // get all passphrases in cache from latest to oldest one by each call - // this function is throwing PassphraseCache::Empty when cache is empty - // and PassphraseCache::Exhausted when no passphrases are left, then - // starts over - - const char *latest_passphrase(); - // call this function inside the messageToSend() implementation of the adapter // this function is using latest_passphrase() to test one passphrase after the // other until the cache is exhausted + // call with reset = true to reset the iterator - static PEP_STATUS messageToSend(PassphraseCache& cache, PEP_SESSION session); - - // for each passphrase call the callee until it returns true for a - // matching passphrase or no passphrases are left - // always tests empty passphrase first - // returns true if a passphrase was matching, false otherwise - - using passphrase_callee = std::function; - bool for_each_passphrase(const passphrase_callee& callee); + static PEP_STATUS config_next_passphrase(bool reset=false); // convenience functions // i.e. @@ -87,6 +73,9 @@ namespace pEp { protected: void cleanup(); void refresh(cache::iterator entry); + const char *latest_passphrase(); + using passphrase_callee = std::function; + bool for_each_passphrase(const passphrase_callee& callee); }; extern PassphraseCache passphrase_cache;