Skip to content

Commit

Permalink
More things
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Fella committed Dec 30, 2024
1 parent 619bc93 commit d49e709
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
13 changes: 10 additions & 3 deletions Quotient/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ void Connection::sync(int timeout)
} else {
auto syncChanges = syncChangesResult->value();

if (syncChanges->secrets_received()) {
emit backupFinished(BackupResult::Success);
}

for (const auto& key : syncChanges->keys()) {
if (const auto& room = this->room(stringFromRust(key.room_id()))) {
room->newSession(stringFromRust(key.session_id()));
Expand Down Expand Up @@ -2082,7 +2086,7 @@ void Connection::Private::confirmKeyVerification(KeyVerificationSession* session
{
auto requestsResult = (*cryptoMachine)->confirm_verification(stringToRust(session->remoteUser()), stringToRust(session->verificationId()));
if (requestsResult->has_error()) {
qWarning() << stringFromRust(requestsResult->error_string());
qWarning() << "Failed to confirm verification" << stringFromRust(requestsResult->error_string());
return;
}
auto requests = requestsResult->value();
Expand Down Expand Up @@ -2287,14 +2291,17 @@ void Connection::loadFromBackup(const QString& passphrase)
stringToRust(version)
);
if (request->is_invalid_passphrase()) {
//TODO
emit backupFinished(BackupResult::InvalidPassphrase);
return;
}
if (request->has_error()) {
qWarning() << stringFromRust(request->error_string());
emit backupFinished(BackupResult::Error);
return;
}
callApi<UploadCrossSigningSignaturesJob>(fromJson<QHash<UserId, QHash<QString, QJsonObject>>>(jsonFromRust(request->value())));
(*d->cryptoMachine)->import_from_backup(bytesToRust(keysJob->rawData()));
emit backupFinished(BackupResult::Success);
});
});
}
Expand Down Expand Up @@ -2343,7 +2350,7 @@ QString Connection::exportKeys(const QString& passphrase)

bool Connection::isUserVerified(const QString& userId) const
{
if (d->cryptoMachine) {
if (!d->cryptoMachine) {
return false;
}
auto result = (*d->cryptoMachine)->is_user_verified(stringToRust(userId));
Expand Down
11 changes: 10 additions & 1 deletion Quotient/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ class QUOTIENT_API Connection : public QObject {
UnpublishRoom
}; // FIXME: Should go inside CreateRoomJob

enum BackupResult {
Success,
InvalidPassphrase,
Error,
};
Q_ENUM(BackupResult)

explicit Connection(QObject* parent = nullptr);
explicit Connection(const QUrl& server, QObject* parent = nullptr);
~Connection() override;
Expand Down Expand Up @@ -341,7 +348,7 @@ class QUOTIENT_API Connection : public QObject {
bool isVerifiedEvent(const QString& eventId, Room* room);

// //! Returns whether the device is verified
// bool isVerifiedDevice(const QString& userId, const QString& deviceId) const;
bool isVerifiedDevice(const QString& userId, const QString& deviceId) const;

// //! \brief Returns whether the device is known and supports end-to-end encryption.
// //!
Expand Down Expand Up @@ -936,6 +943,8 @@ public Q_SLOTS:
//! Usually not relevant to clients.
void verificationEventProcessed();

void backupFinished(Connection::BackupResult status);

friend class ::TestCrossSigning;
friend class KeyVerificationSession;
protected:
Expand Down
12 changes: 11 additions & 1 deletion Quotient/crypto-sdk/src/cryptomachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Key {
pub(crate) struct SyncChanges {
sessions: Vec<KeyVerificationRequest>,
keys: Vec<Key>,
secrets_received: bool,
}

pub(crate) enum SyncChangesResult {
Expand Down Expand Up @@ -123,6 +124,10 @@ impl SyncChanges {
pub(crate) fn keys(&self) -> Vec<Key> {
self.keys.clone()
}

pub(crate) fn secrets_received(&self) -> bool {
self.secrets_received
}
}

pub(crate) enum VoidResult {
Expand Down Expand Up @@ -827,6 +832,7 @@ impl CryptoMachine {
.await?;

let mut events = vec![];
let mut secrets_received = false;
for to_device_event in changes.0 {
// NOTE: Do not use question mark operator in for loop.
match to_device_event.deserialize() {
Expand All @@ -842,8 +848,11 @@ impl CryptoMachine {
request.sender, request.content.transaction_id
);
}
},
Ok(AnyToDeviceEvent::SecretSend(_)) => {
secrets_received = true;
}
Ok(_) => {}
Ok(_) => {},
Err(error) => {
eprintln!("Failed to deserialize to_device event: {}", error);
}
Expand All @@ -859,6 +868,7 @@ impl CryptoMachine {
room_id: it.room_id.to_string(),
})
.collect(),
secrets_received,
}))
});
Box::new(match result {
Expand Down
2 changes: 2 additions & 0 deletions Quotient/crypto-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ mod ffi {

fn sessions(self: &SyncChanges) -> Vec<KeyVerificationRequest>;
fn keys(self: &SyncChanges) -> Vec<Key>;
fn secrets_received(self: &SyncChanges) -> bool;


fn session_id(self: &Key) -> String;
fn room_id(self: &Key) -> String;
Expand Down

0 comments on commit d49e709

Please sign in to comment.