2020-02-04 13:41:13 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import '../client.dart';
|
2020-05-15 18:40:17 +00:00
|
|
|
import '../database/database.dart' show DbUserDeviceKey, DbUserDeviceKeysKey;
|
|
|
|
import '../event.dart';
|
2020-05-17 13:25:42 +00:00
|
|
|
import 'key_verification.dart';
|
2020-02-04 13:41:13 +00:00
|
|
|
|
|
|
|
class DeviceKeysList {
|
|
|
|
String userId;
|
|
|
|
bool outdated = true;
|
|
|
|
Map<String, DeviceKeys> deviceKeys = {};
|
|
|
|
|
2020-05-15 18:40:17 +00:00
|
|
|
DeviceKeysList.fromDb(DbUserDeviceKey dbEntry, List<DbUserDeviceKeysKey> childEntries) {
|
|
|
|
userId = dbEntry.userId;
|
|
|
|
outdated = dbEntry.outdated;
|
|
|
|
deviceKeys = {};
|
|
|
|
for (final childEntry in childEntries) {
|
2020-05-20 07:37:32 +00:00
|
|
|
final entry = DeviceKeys.fromDb(childEntry);
|
|
|
|
if (entry.isValid) {
|
|
|
|
deviceKeys[childEntry.deviceId] = entry;
|
|
|
|
} else {
|
|
|
|
outdated = true;
|
|
|
|
}
|
2020-05-15 18:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 13:41:13 +00:00
|
|
|
DeviceKeysList.fromJson(Map<String, dynamic> json) {
|
|
|
|
userId = json['user_id'];
|
|
|
|
outdated = json['outdated'];
|
|
|
|
deviceKeys = {};
|
|
|
|
for (final rawDeviceKeyEntry in json['device_keys'].entries) {
|
|
|
|
deviceKeys[rawDeviceKeyEntry.key] =
|
|
|
|
DeviceKeys.fromJson(rawDeviceKeyEntry.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
2020-03-30 09:08:38 +00:00
|
|
|
var map = <String, dynamic>{};
|
|
|
|
final data = map;
|
|
|
|
data['user_id'] = userId;
|
|
|
|
data['outdated'] = outdated ?? true;
|
2020-02-04 13:41:13 +00:00
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
var rawDeviceKeys = <String, dynamic>{};
|
|
|
|
for (final deviceKeyEntry in deviceKeys.entries) {
|
2020-02-04 13:41:13 +00:00
|
|
|
rawDeviceKeys[deviceKeyEntry.key] = deviceKeyEntry.value.toJson();
|
|
|
|
}
|
|
|
|
data['device_keys'] = rawDeviceKeys;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-04 13:41:13 +00:00
|
|
|
String toString() => json.encode(toJson());
|
|
|
|
|
|
|
|
DeviceKeysList(this.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeviceKeys {
|
|
|
|
String userId;
|
|
|
|
String deviceId;
|
|
|
|
List<String> algorithms;
|
|
|
|
Map<String, String> keys;
|
|
|
|
Map<String, dynamic> signatures;
|
|
|
|
Map<String, dynamic> unsigned;
|
|
|
|
bool verified;
|
|
|
|
bool blocked;
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
String get curve25519Key => keys['curve25519:$deviceId'];
|
|
|
|
String get ed25519Key => keys['ed25519:$deviceId'];
|
2020-02-15 07:48:41 +00:00
|
|
|
|
2020-05-20 07:37:32 +00:00
|
|
|
bool get isValid => userId != null && deviceId != null && curve25519Key != null && ed25519Key != null;
|
|
|
|
|
2020-02-04 13:41:13 +00:00
|
|
|
Future<void> setVerified(bool newVerified, Client client) {
|
|
|
|
verified = newVerified;
|
2020-05-15 18:40:17 +00:00
|
|
|
return client.database?.setVerifiedUserDeviceKey(newVerified, client.id, userId, deviceId);
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setBlocked(bool newBlocked, Client client) {
|
|
|
|
blocked = newBlocked;
|
2020-03-30 09:08:38 +00:00
|
|
|
for (var room in client.rooms) {
|
2020-02-19 09:54:08 +00:00
|
|
|
if (!room.encrypted) continue;
|
|
|
|
if (room.getParticipants().indexWhere((u) => u.id == userId) != -1) {
|
|
|
|
room.clearOutboundGroupSession();
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 18:40:17 +00:00
|
|
|
return client.database?.setBlockedUserDeviceKey(newBlocked, client.id, userId, deviceId);
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceKeys({
|
|
|
|
this.userId,
|
|
|
|
this.deviceId,
|
|
|
|
this.algorithms,
|
|
|
|
this.keys,
|
|
|
|
this.signatures,
|
|
|
|
this.unsigned,
|
|
|
|
this.verified,
|
|
|
|
this.blocked,
|
|
|
|
});
|
2020-05-15 18:40:17 +00:00
|
|
|
|
|
|
|
DeviceKeys.fromDb(DbUserDeviceKeysKey dbEntry) {
|
|
|
|
final content = Event.getMapFromPayload(dbEntry.content);
|
|
|
|
userId = dbEntry.userId;
|
|
|
|
deviceId = dbEntry.deviceId;
|
|
|
|
algorithms = content['algorithms'].cast<String>();
|
|
|
|
keys = content['keys'] != null ? Map<String, String>.from(content['keys']) : null;
|
|
|
|
signatures = content['signatures'] != null
|
|
|
|
? Map<String, dynamic>.from(content['signatures'])
|
|
|
|
: null;
|
|
|
|
unsigned = content['unsigned'] != null
|
|
|
|
? Map<String, dynamic>.from(content['unsigned'])
|
|
|
|
: null;
|
|
|
|
verified = dbEntry.verified;
|
|
|
|
blocked = dbEntry.blocked;
|
|
|
|
}
|
2020-02-04 13:41:13 +00:00
|
|
|
|
|
|
|
DeviceKeys.fromJson(Map<String, dynamic> json) {
|
|
|
|
userId = json['user_id'];
|
|
|
|
deviceId = json['device_id'];
|
|
|
|
algorithms = json['algorithms'].cast<String>();
|
|
|
|
keys = json['keys'] != null ? Map<String, String>.from(json['keys']) : null;
|
|
|
|
signatures = json['signatures'] != null
|
|
|
|
? Map<String, dynamic>.from(json['signatures'])
|
|
|
|
: null;
|
|
|
|
unsigned = json['unsigned'] != null
|
|
|
|
? Map<String, dynamic>.from(json['unsigned'])
|
|
|
|
: null;
|
|
|
|
verified = json['verified'] ?? false;
|
|
|
|
blocked = json['blocked'] ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
2020-03-30 09:08:38 +00:00
|
|
|
final data = <String, dynamic>{};
|
|
|
|
data['user_id'] = userId;
|
|
|
|
data['device_id'] = deviceId;
|
|
|
|
data['algorithms'] = algorithms;
|
|
|
|
if (keys != null) {
|
|
|
|
data['keys'] = keys;
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|
2020-03-30 09:08:38 +00:00
|
|
|
if (signatures != null) {
|
|
|
|
data['signatures'] = signatures;
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|
2020-03-30 09:08:38 +00:00
|
|
|
if (unsigned != null) {
|
|
|
|
data['unsigned'] = unsigned;
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|
2020-03-30 09:08:38 +00:00
|
|
|
data['verified'] = verified;
|
|
|
|
data['blocked'] = blocked;
|
2020-02-04 13:41:13 +00:00
|
|
|
return data;
|
|
|
|
}
|
2020-05-17 13:25:42 +00:00
|
|
|
|
|
|
|
KeyVerification startVerification(Client client) {
|
|
|
|
final request = KeyVerification(client: client, userId: userId, deviceId: deviceId);
|
|
|
|
request.start();
|
|
|
|
client.addKeyVerificationRequest(request);
|
|
|
|
return request;
|
|
|
|
}
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|