Browse Source

include stored passphrase in delivery

synchronous Release_2.1.0-RC6
Volker Birk 5 years ago
parent
commit
dd7a29d312
  1. 20
      passphrase_cache.cc
  2. 1
      passphrase_cache.hh

20
passphrase_cache.cc

@ -9,12 +9,12 @@ namespace pEp {
{ } { }
PassphraseCache::PassphraseCache(size_t max_size, duration timeout) : PassphraseCache::PassphraseCache(size_t max_size, duration timeout) :
_max_size{max_size}, _timeout{timeout}, _which{_cache.end()} _max_size{max_size}, _timeout{timeout}, first_time(true)
{ } { }
PassphraseCache::PassphraseCache(const PassphraseCache& second) : PassphraseCache::PassphraseCache(const PassphraseCache& second) :
_cache{second._cache}, _max_size{second._max_size}, _cache{second._cache}, _max_size{second._max_size},
_timeout{second._timeout}, _which{_cache.end()} _timeout{second._timeout}, first_time(true)
{ {
cleanup(); cleanup();
} }
@ -31,7 +31,6 @@ namespace pEp {
const char *PassphraseCache::add(const std::string& passphrase) const char *PassphraseCache::add(const std::string& passphrase)
{ {
assert(_which == _cache.end()); // never modify while iterating
std::lock_guard<std::mutex> lock(_mtx); std::lock_guard<std::mutex> lock(_mtx);
if (!passphrase.empty()) { if (!passphrase.empty()) {
@ -83,29 +82,32 @@ namespace pEp {
void PassphraseCache::cleanup() void PassphraseCache::cleanup()
{ {
assert(_which == _cache.end()); // never modify while iterating
while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout) while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout)
_cache.pop_front(); _cache.pop_front();
} }
void PassphraseCache::refresh(cache::iterator entry) void PassphraseCache::refresh(cache::iterator entry)
{ {
assert(_which == _cache.end()); // never modify while iterating
entry->tp = clock::now(); entry->tp = clock::now();
_cache.splice(_cache.end(), _cache, entry); _cache.splice(_cache.end(), _cache, entry);
} }
const char *PassphraseCache::latest_passphrase() const char *PassphraseCache::latest_passphrase()
{ {
std::lock_guard<std::mutex> lock(_mtx); if (first_time) {
cleanup();
if (_cache.empty()) {
_which = _cache.end(); _which = _cache.end();
first_time = false;
return _stored.c_str();
}
if (_cache.empty()) {
first_time = true;
throw Empty(); throw Empty();
} }
if (_which == _cache.begin()) { if (_which == _cache.begin()) {
_which = _cache.end(); first_time = true;
throw Exhausted(); throw Exhausted();
} }

1
passphrase_cache.hh

@ -31,6 +31,7 @@ namespace pEp {
duration _timeout; duration _timeout;
cache::iterator _which; cache::iterator _which;
bool first_time;
public: public:
struct Empty : public std::underflow_error { struct Empty : public std::underflow_error {

Loading…
Cancel
Save