Merge branch 'client-fix-logs' into 'master'

[Client] Dont create new inbound session when session matches

See merge request famedly/famedlysdk!214
This commit is contained in:
Christian Pauly 2020-02-20 15:29:16 +00:00
commit 5a4c484db6
2 changed files with 17 additions and 4 deletions

View file

@ -1025,7 +1025,10 @@ class Client {
try { try {
toDeviceEvent = decryptToDeviceEvent(toDeviceEvent); toDeviceEvent = decryptToDeviceEvent(toDeviceEvent);
} catch (e) { } catch (e) {
print("[LibOlm] Could not decrypt to device event: " + e.toString()); print(
"[LibOlm] Could not decrypt to device event from ${toDeviceEvent.sender}: " +
e.toString());
print(toDeviceEvent.sender);
toDeviceEvent = ToDeviceEvent.fromJson(events[i]); toDeviceEvent = ToDeviceEvent.fromJson(events[i]);
} }
} }
@ -1320,8 +1323,10 @@ class Client {
Room room = getRoomById(toDeviceEvent.content["room_id"]); Room room = getRoomById(toDeviceEvent.content["room_id"]);
if (room != null && toDeviceEvent.content["session_id"] is String) { if (room != null && toDeviceEvent.content["session_id"] is String) {
final String sessionId = toDeviceEvent.content["session_id"]; final String sessionId = toDeviceEvent.content["session_id"];
if (room != null) {
room.setSessionKey(sessionId, toDeviceEvent.content); room.setSessionKey(sessionId, toDeviceEvent.content);
} }
}
break; break;
} }
} catch (e) { } catch (e) {
@ -1570,7 +1575,11 @@ class Client {
List<olm.Session> existingSessions = olmSessions[senderKey]; List<olm.Session> existingSessions = olmSessions[senderKey];
if (existingSessions != null) { if (existingSessions != null) {
for (olm.Session session in existingSessions) { for (olm.Session session in existingSessions) {
if ((type == 0 && session.matches_inbound(body) == 1) || type == 1) { if (type == 0 && session.matches_inbound(body) != 0) {
plaintext = session.decrypt(type, body);
storeOlmSession(senderKey, session);
break;
} else if (type == 1) {
try { try {
plaintext = session.decrypt(type, body); plaintext = session.decrypt(type, body);
storeOlmSession(senderKey, session); storeOlmSession(senderKey, session);
@ -1679,6 +1688,7 @@ class Client {
}; };
final olm.EncryptResult encryptResult = final olm.EncryptResult encryptResult =
existingSessions.first.encrypt(json.encode(payload)); existingSessions.first.encrypt(json.encode(payload));
storeOlmSession(device.curve25519Key, existingSessions.first);
encryptedMessage["ciphertext"][device.curve25519Key] = { encryptedMessage["ciphertext"][device.curve25519Key] = {
"type": encryptResult.type, "type": encryptResult.type,
"body": encryptResult.body, "body": encryptResult.body,

View file

@ -836,7 +836,7 @@ class Room {
.getItem("/clients/${client.deviceID}/rooms/${this.id}/session_keys"); .getItem("/clients/${client.deviceID}/rooms/${this.id}/session_keys");
if (sessionKeysPickle?.isNotEmpty ?? false) { if (sessionKeysPickle?.isNotEmpty ?? false) {
final Map<String, dynamic> map = json.decode(sessionKeysPickle); final Map<String, dynamic> map = json.decode(sessionKeysPickle);
this._sessionKeys = {}; if (this._sessionKeys == null) this._sessionKeys = {};
for (var entry in map.entries) { for (var entry in map.entries) {
try { try {
this._sessionKeys[entry.key] = this._sessionKeys[entry.key] =
@ -848,6 +848,9 @@ class Room {
} }
} }
} }
await client.storeAPI?.setItem(
"/clients/${client.deviceID}/rooms/${this.id}/session_keys",
json.encode(sessionKeys));
_fullyRestored = true; _fullyRestored = true;
} }