Breaking: Change secret names and fix bug when storing secrets

This commit is contained in:
Nicolas Werner 2021-08-08 18:37:40 +02:00
parent 8784156da5
commit ad57a336dc
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
2 changed files with 38 additions and 20 deletions

View File

@ -720,20 +720,34 @@ Cache::storeSecret(const std::string name, const std::string secret)
{ {
auto settings = UserSettings::instance(); auto settings = UserSettings::instance();
auto job = new QKeychain::WritePasswordJob(QCoreApplication::applicationName()); auto job = new QKeychain::WritePasswordJob(QCoreApplication::applicationName());
job->setAutoDelete(true);
job->setInsecureFallback(true); job->setInsecureFallback(true);
job->setKey("matrix." +
QString(QCryptographicHash::hash(settings->profile().toUtf8(), // job->setSettings(new QSettings(job));
QCryptographicHash::Sha256)) + job->setKey(
"." + name.c_str()); "matrix." +
QString(QCryptographicHash::hash(settings->profile().toUtf8(), QCryptographicHash::Sha256)
.toBase64()) +
"." + QString::fromStdString(name));
job->setTextData(QString::fromStdString(secret)); job->setTextData(QString::fromStdString(secret));
QObject::connect(job, &QKeychain::Job::finished, job, [name, this](QKeychain::Job *job) { QObject::connect(
if (job->error()) { job,
nhlog::db()->warn( &QKeychain::WritePasswordJob::finished,
"Storing secret '{}' failed: {}", name, job->errorString().toStdString()); this,
} else { [name, this](QKeychain::Job *job) {
emit secretChanged(name); if (job->error()) {
} nhlog::db()->warn("Storing secret '{}' failed: {}",
}); name,
job->errorString().toStdString());
} else {
// if we emit the signal directly, qtkeychain breaks and won't execute new
// jobs. You can't start a job from the finish signal of a job.
QTimer::singleShot(100, [this, name] { emit secretChanged(name); });
nhlog::db()->info("Storing secret '{}' successful", name);
}
},
Qt::ConnectionType::DirectConnection);
job->start(); job->start();
} }
@ -744,10 +758,11 @@ Cache::deleteSecret(const std::string name)
QKeychain::DeletePasswordJob job(QCoreApplication::applicationName()); QKeychain::DeletePasswordJob job(QCoreApplication::applicationName());
job.setAutoDelete(false); job.setAutoDelete(false);
job.setInsecureFallback(true); job.setInsecureFallback(true);
job.setKey("matrix." + job.setKey(
QString(QCryptographicHash::hash(settings->profile().toUtf8(), "matrix." +
QCryptographicHash::Sha256)) + QString(QCryptographicHash::hash(settings->profile().toUtf8(), QCryptographicHash::Sha256)
"." + name.c_str()); .toBase64()) +
"." + QString::fromStdString(name));
// FIXME(Nico): Nested event loops are dangerous. Some other slots may resume in the mean // FIXME(Nico): Nested event loops are dangerous. Some other slots may resume in the mean
// time! // time!
QEventLoop loop; QEventLoop loop;
@ -765,10 +780,11 @@ Cache::secret(const std::string name)
QKeychain::ReadPasswordJob job(QCoreApplication::applicationName()); QKeychain::ReadPasswordJob job(QCoreApplication::applicationName());
job.setAutoDelete(false); job.setAutoDelete(false);
job.setInsecureFallback(true); job.setInsecureFallback(true);
job.setKey("matrix." + job.setKey(
QString(QCryptographicHash::hash(settings->profile().toUtf8(), "matrix." +
QCryptographicHash::Sha256)) + QString(QCryptographicHash::hash(settings->profile().toUtf8(), QCryptographicHash::Sha256)
"." + name.c_str()); .toBase64()) +
"." + QString::fromStdString(name));
// FIXME(Nico): Nested event loops are dangerous. Some other slots may resume in the mean // FIXME(Nico): Nested event loops are dangerous. Some other slots may resume in the mean
// time! // time!
QEventLoop loop; QEventLoop loop;

View File

@ -425,6 +425,8 @@ handle_olm_message(const OlmMessage &msg, const UserKeyCache &otherUserDeviceKey
} }
}); });
nhlog::crypto()->info("Storing secret {}",
secret_name->second);
cache::client()->storeSecret(secret_name->second, cache::client()->storeSecret(secret_name->second,
e->content.secret); e->content.secret);