Skip to content

Commit

Permalink
Fix QR login support with cryptoV2
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr authored and stefanceriu committed Oct 2, 2023
1 parent 2b1e26c commit d798299
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
17 changes: 17 additions & 0 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,23 @@ extension MXCryptoMachine: MXCryptoCrossSigning {
log.error("Failed importing cross signing keys", context: error)
}
}

func queryMissingSecretsFromOtherSessions() async throws {
let has_missing = try machine.queryMissingSecretsFromOtherSessions()

if (has_missing) {
// Out-of-sync check if there are any secret request to sent out as a result of
// the missing secret request
for request in try machine.outgoingRequests() {
if case .toDevice(_, let eventType, _) = request {
if (eventType == kMXEventTypeStringSecretRequest) {
try await handleRequest(request)
}
}
}
}
}

}

extension MXCryptoMachine: MXCryptoVerifying {
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ protocol MXCryptoCrossSigning: MXCryptoUserIdentitySource, MXCryptoDevicesSource
func bootstrapCrossSigning(authParams: [AnyHashable: Any]) async throws
func exportCrossSigningKeys() -> CrossSigningKeyExport?
func importCrossSigningKeys(export: CrossSigningKeyExport)

func queryMissingSecretsFromOtherSessions() async throws
}

/// Verification functionality
Expand Down
42 changes: 32 additions & 10 deletions MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,39 @@ class MXCryptoV2: NSObject, MXCrypto {
case .verified:
// If we want to set verified status, we will manually verify the device,
// including uploading relevant signatures
try? machine.setLocalTrust(userId: machine.userId, deviceId: deviceId, trust: .verified)

Task {
do {
try await machine.verifyDevice(userId: userId, deviceId: deviceId)
log.debug("Successfully marked device as verified")
await MainActor.run {
success?()
if (userId == machine.userId) {
if (machine.crossSigningStatus().hasSelfSigning) {
// if we can cross sign, upload a new signature for that device
Task {
do {
// This method will always fail if the device belongs to someone else.
// XXX Should update API? and remove the userId?
try await machine.verifyDevice(userId: userId, deviceId: deviceId)
log.debug("Successfully marked device as verified")
await MainActor.run {
success?()
}
} catch {
log.error("Failed marking device as verified", context: error)
await MainActor.run {
failure?(error)
}
}
}
} catch {
log.error("Failed marking device as verified", context: error)
await MainActor.run {
failure?(error)
} else {
// It's a good time to request secrets
Task {
do {
try await machine.queryMissingSecretsFromOtherSessions()
await MainActor.run {
success?()
}
} catch {
log.error("Failed to query missing secrets", context: error)
failure?(error)
}
}
}
}
Expand All @@ -409,6 +430,7 @@ class MXCryptoV2: NSObject, MXCrypto {
do {
try machine.setLocalTrust(userId: userId, deviceId: deviceId, trust: localTrust)
log.debug("Successfully set local trust to \(localTrust)")
// XXX: Why no MainActor.run here?
success?()
} catch {
log.error("Failed setting local trust", context: error)
Expand Down

0 comments on commit d798299

Please sign in to comment.