You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

#include "passphrase_cache.hh"
namespace pEp {
const char *PassphraseCache::add(std::string passphrase)
{
std::lock_guard<std::mutex> lock(_mtx);
if (passphrase != "") {
while (_cache.size() >= _max_size)
_cache.pop_front();
_cache.emplace_back(cache_entry(passphrase, clock::now()));
}
return passphrase.c_str();
}
bool PassphraseCache::for_each_passphrase(const passphrase_callee& callee)
{
std::lock_guard<std::mutex> lock(_mtx);
cleanup();
if (callee(""))
return true;
for (auto entry=_cache.begin(); entry!=_cache.end(); ++entry) {
if (callee(entry->passphrase)) {
refresh(entry);
return true;
}
}
return false;
}
void PassphraseCache::cleanup()
{
while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout)
_cache.pop_front();
}
void PassphraseCache::refresh(cache::iterator entry)
{
entry->tp = clock::now();
_cache.splice(_cache.end(), _cache, entry);
}
std::string PassphraseCache::latest_passphrase()
{
std::lock_guard<std::mutex> lock(_mtx);
cleanup();
_which = _cache.end();
return "";
}
std::string PassphraseCache::next_passphrase()
{
std::lock_guard<std::mutex> lock(_mtx);
if (_cache.empty() || _which == _cache.begin()) {
return "";
}
else {
--_which;
return _which->passphrase;
}
}
};