feat: Add encryption content

This commit is contained in:
Christian Pauly 2021-01-07 13:55:57 +01:00
parent 52c771b793
commit 128df8d8eb
4 changed files with 185 additions and 0 deletions

View File

@ -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';

View File

@ -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<String, CiphertextInfo> ciphertextOlm;
RoomEncryptedContent.fromJson(Map<String, dynamic> json)
: algorithm = json.tryGet<String>('algorithm', ''),
senderKey = json.tryGet<String>('sender_key', ''),
deviceId = json.tryGet<String>('device_id'),
sessionId = json.tryGet<String>('session_id'),
ciphertextMegolm = json.tryGet<String>('ciphertext'),
ciphertextOlm = json
.tryGet<Map<String, dynamic>>('ciphertext')
?.map((k, v) => MapEntry(k, CiphertextInfo.fromJson(v)));
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
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<String, dynamic> json)
: body = json.tryGet<String>('body'),
type = json.tryGet<int>('type');
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
if (body != null) {
data['body'] = body;
}
if (type != null) {
data['type'] = type;
}
return data;
}
}

View File

@ -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<String, dynamic> json)
: algorithm = json.tryGet<String>('algorithm', ''),
rotationPeriodMs = json.tryGet<int>('rotation_period_ms'),
rotationPeriodMsgs = json.tryGet<int>('rotation_period_msgs');
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['algorithm'] = algorithm;
if (rotationPeriodMs != null) {
data['rotation_period_ms'] = rotationPeriodMs;
}
if (rotationPeriodMsgs != null) {
data['rotation_period_msgs'] = rotationPeriodMsgs;
}
return data;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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 = <String, dynamic>{
'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 = <String, dynamic>{
'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 = <String, dynamic>{
'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']);
});
});
}