diff --git a/lib/matrix_api_lite.dart b/lib/matrix_api_lite.dart index e7c0ed26..0583e2ef 100644 --- a/lib/matrix_api_lite.dart +++ b/lib/matrix_api_lite.dart @@ -79,6 +79,8 @@ export 'src/model/auth/authentication_three_pid_creds.dart'; export 'src/model/auth/authentication_token.dart'; export 'src/model/auth/authentication_types.dart'; export 'src/model/auth/authentication_user_identifier.dart'; +export 'src/model/events/room_encrypted_content.dart'; +export 'src/model/events/room_encryption_content.dart'; export 'src/model/events/secret_storage_default_key_content.dart'; export 'src/model/events/secret_storage_key_content.dart'; export 'src/model/events/tombstone_content.dart'; diff --git a/lib/src/model/events/room_encrypted_content.dart b/lib/src/model/events/room_encrypted_content.dart new file mode 100644 index 00000000..f5f98e4d --- /dev/null +++ b/lib/src/model/events/room_encrypted_content.dart @@ -0,0 +1,71 @@ +import 'package:matrix_api_lite/src/utils/logs.dart'; + +import '../basic_event.dart'; +import '../../utils/try_get_map_extension.dart'; + +extension RoomEncryptedContentBasicEventExtension on BasicEvent { + RoomEncryptedContent get parsedRoomEncryptedContent => + RoomEncryptedContent.fromJson(content); +} + +class RoomEncryptedContent { + String algorithm; + String senderKey; + String deviceId; + String sessionId; + String ciphertextMegolm; + Map ciphertextOlm; + + RoomEncryptedContent.fromJson(Map json) + : algorithm = json.tryGet('algorithm', ''), + senderKey = json.tryGet('sender_key', ''), + deviceId = json.tryGet('device_id'), + sessionId = json.tryGet('session_id'), + ciphertextMegolm = json.tryGet('ciphertext'), + ciphertextOlm = json + .tryGet>('ciphertext') + ?.map((k, v) => MapEntry(k, CiphertextInfo.fromJson(v))); + + Map toJson() { + final data = {}; + data['algorithm'] = algorithm; + data['sender_key'] = senderKey; + if (deviceId != null) { + data['device_id'] = deviceId; + } + if (sessionId != null) { + data['session_id'] = sessionId; + } + if (ciphertextMegolm != null) { + data['ciphertext'] = ciphertextMegolm; + } + if (ciphertextOlm != null) { + data['ciphertext'] = ciphertextOlm.map((k, v) => MapEntry(k, v.toJson())); + if (ciphertextMegolm != null) { + Logs().wtf( + 'ciphertextOlm and ciphertextMegolm are both set, which should never happen!'); + } + } + return data; + } +} + +class CiphertextInfo { + String body; + int type; + + CiphertextInfo.fromJson(Map json) + : body = json.tryGet('body'), + type = json.tryGet('type'); + + Map toJson() { + final data = {}; + if (body != null) { + data['body'] = body; + } + if (type != null) { + data['type'] = type; + } + return data; + } +} diff --git a/lib/src/model/events/room_encryption_content.dart b/lib/src/model/events/room_encryption_content.dart new file mode 100644 index 00000000..7431f99b --- /dev/null +++ b/lib/src/model/events/room_encryption_content.dart @@ -0,0 +1,30 @@ +import '../basic_event.dart'; +import '../../utils/try_get_map_extension.dart'; + +extension RoomEncryptionContentBasicEventExtension on BasicEvent { + RoomEncryptionContent get parsedRoomEncryptionContent => + RoomEncryptionContent.fromJson(content); +} + +class RoomEncryptionContent { + String algorithm; + int rotationPeriodMs; + int rotationPeriodMsgs; + + RoomEncryptionContent.fromJson(Map json) + : algorithm = json.tryGet('algorithm', ''), + rotationPeriodMs = json.tryGet('rotation_period_ms'), + rotationPeriodMsgs = json.tryGet('rotation_period_msgs'); + + Map toJson() { + final data = {}; + data['algorithm'] = algorithm; + if (rotationPeriodMs != null) { + data['rotation_period_ms'] = rotationPeriodMs; + } + if (rotationPeriodMsgs != null) { + data['rotation_period_msgs'] = rotationPeriodMsgs; + } + return data; + } +} diff --git a/test/event_content_test.dart b/test/event_content_test.dart new file mode 100644 index 00000000..e1fee0d0 --- /dev/null +++ b/test/event_content_test.dart @@ -0,0 +1,82 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix_api_lite/matrix_api_lite.dart'; +import 'package:test/test.dart'; + +void main() { + group('Event Content tests', () { + test('Room Encryption Content', () { + var json = { + 'content': { + 'algorithm': 'm.megolm.v1.aes-sha2', + 'rotation_period_ms': 604800000, + 'rotation_period_msgs': 100 + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.encryption', + 'unsigned': {'age': 1234} + }; + expect(MatrixEvent.fromJson(json).parsedRoomEncryptionContent.toJson(), + json['content']); + }); + test('Room Encrypted Content', () { + var json = { + 'content': { + 'algorithm': 'm.megolm.v1.aes-sha2', + 'ciphertext': 'AwgAEnACgAkLmt6qF84IK++J7UDH2Za1YVchHyprqTqsg...', + 'device_id': 'RJYKSTBOIE', + 'sender_key': 'IlRMeOPX2e0MurIyfWEucYBRVOEEUMrOHqn/8mLqMjA', + 'session_id': 'X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.encrypted', + 'unsigned': {'age': 1234} + }; + expect(MatrixEvent.fromJson(json).parsedRoomEncryptedContent.toJson(), + json['content']); + json = { + 'content': { + 'algorithm': 'm.olm.v1.curve25519-aes-sha2', + 'ciphertext': { + '7qZcfnBmbEGzxxaWfBjElJuvn7BZx+lSz/SvFrDF/z8': { + 'body': 'AwogGJJzMhf/S3GQFXAOrCZ3iKyGU5ZScVtjI0KypTYrW...', + 'type': 0 + } + }, + 'sender_key': 'Szl29ksW/L8yZGWAX+8dY1XyFi+i5wm+DRhTGkbMiwU' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.encrypted', + 'unsigned': {'age': 1234} + }; + expect(MatrixEvent.fromJson(json).parsedRoomEncryptedContent.toJson(), + json['content']); + }); + }); +}