make auth_data just a json object

This commit is contained in:
Sorunome 2020-06-23 08:30:50 +02:00
parent dbcdb6883d
commit 48c03865a2
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
4 changed files with 15 additions and 60 deletions

View file

@ -44,11 +44,11 @@ class KeyManager {
final keyObj = olm.PkDecryption(); final keyObj = olm.PkDecryption();
try { try {
final info = await client.api.getRoomKeysBackup(); final info = await client.api.getRoomKeysBackup();
if (!(info.authData is RoomKeysAuthDataV1Curve25519AesSha2)) { if (info.algorithm != RoomKeysAlgorithmType.v1Curve25519AesSha2) {
return false; return false;
} }
if (keyObj.init_with_private_key(base64.decode(secret)) == if (keyObj.init_with_private_key(base64.decode(secret)) ==
(info.authData as RoomKeysAuthDataV1Curve25519AesSha2).publicKey) { info.authData['public_key']) {
_requestedSessionIds.clear(); _requestedSessionIds.clear();
return true; return true;
} }
@ -340,9 +340,8 @@ class KeyManager {
backupPubKey = decryption.init_with_private_key(privateKey); backupPubKey = decryption.init_with_private_key(privateKey);
if (backupPubKey == null || if (backupPubKey == null ||
!(info.authData is RoomKeysAuthDataV1Curve25519AesSha2) || info.algorithm != RoomKeysAlgorithmType.v1Curve25519AesSha2 ||
(info.authData as RoomKeysAuthDataV1Curve25519AesSha2).publicKey != info.authData['public_key'] != backupPubKey) {
backupPubKey) {
return; return;
} }
for (final roomEntry in keys.rooms.entries) { for (final roomEntry in keys.rooms.entries) {

View file

@ -2042,13 +2042,13 @@ class MatrixApi {
/// Create room keys backup /// Create room keys backup
/// https://matrix.org/docs/spec/client_server/unstable#post-matrix-client-r0-room-keys-version /// https://matrix.org/docs/spec/client_server/unstable#post-matrix-client-r0-room-keys-version
Future<String> createRoomKeysBackup( Future<String> createRoomKeysBackup(
RoomKeysAlgorithmType algorithm, RoomKeysAuthData authData) async { RoomKeysAlgorithmType algorithm, Map<String, dynamic> authData) async {
final ret = await request( final ret = await request(
RequestType.POST, RequestType.POST,
'/client/unstable/room_keys/version', '/client/unstable/room_keys/version',
data: { data: {
'algorithm': algorithm.algorithmString, 'algorithm': algorithm.algorithmString,
'auth_data': authData.toJson(), 'auth_data': authData,
}, },
); );
return ret['version']; return ret['version'];
@ -2071,13 +2071,13 @@ class MatrixApi {
/// Updates a room key backup /// Updates a room key backup
/// https://matrix.org/docs/spec/client_server/unstable#put-matrix-client-r0-room-keys-version-version /// https://matrix.org/docs/spec/client_server/unstable#put-matrix-client-r0-room-keys-version-version
Future<void> updateRoomKeysBackup(String version, Future<void> updateRoomKeysBackup(String version,
RoomKeysAlgorithmType algorithm, RoomKeysAuthData authData) async { RoomKeysAlgorithmType algorithm, Map<String, dynamic> authData) async {
await request( await request(
RequestType.PUT, RequestType.PUT,
'/client/unstable/room_keys/version/${Uri.encodeComponent(version)}', '/client/unstable/room_keys/version/${Uri.encodeComponent(version)}',
data: { data: {
'algorithm': algorithm.algorithmString, 'algorithm': algorithm.algorithmString,
'auth_data': authData.toJson(), 'auth_data': authData,
'version': version, 'version': version,
}, },
); );

View file

@ -38,46 +38,9 @@ extension RoomKeysAlgorithmTypeExtension on RoomKeysAlgorithmType {
} }
} }
abstract class RoomKeysAuthData {
// This object is used for signing so we need the raw json too
Map<String, dynamic> _json;
RoomKeysAuthData.fromJson(Map<String, dynamic> json) {
_json = json;
}
Map<String, dynamic> toJson() {
return _json;
}
}
class RoomKeysAuthDataV1Curve25519AesSha2 extends RoomKeysAuthData {
String publicKey;
Map<String, Map<String, String>> signatures;
RoomKeysAuthDataV1Curve25519AesSha2.fromJson(Map<String, dynamic> json)
: super.fromJson(json) {
publicKey = json['public_key'];
signatures = json['signatures'] is Map
? Map<String, Map<String, String>>.from((json['signatures'] as Map)
.map((k, v) => MapEntry(k, Map<String, String>.from(v))))
: null;
}
@override
Map<String, dynamic> toJson() {
final data = super.toJson();
data['public_key'] = publicKey;
if (signatures != null) {
data['signatures'] = signatures;
}
return data;
}
}
class RoomKeysVersionResponse { class RoomKeysVersionResponse {
RoomKeysAlgorithmType algorithm; RoomKeysAlgorithmType algorithm;
RoomKeysAuthData authData; Map<String, dynamic> authData;
int count; int count;
String etag; String etag;
String version; String version;
@ -85,14 +48,7 @@ class RoomKeysVersionResponse {
RoomKeysVersionResponse.fromJson(Map<String, dynamic> json) { RoomKeysVersionResponse.fromJson(Map<String, dynamic> json) {
algorithm = algorithm =
RoomKeysAlgorithmTypeExtension.fromAlgorithmString(json['algorithm']); RoomKeysAlgorithmTypeExtension.fromAlgorithmString(json['algorithm']);
switch (algorithm) { authData = json['auth_data'];
case RoomKeysAlgorithmType.v1Curve25519AesSha2:
authData =
RoomKeysAuthDataV1Curve25519AesSha2.fromJson(json['auth_data']);
break;
default:
authData = null;
}
count = json['count']; count = json['count'];
etag = etag =
json['etag'].toString(); // synapse replies an int but docs say string? json['etag'].toString(); // synapse replies an int but docs say string?
@ -102,7 +58,7 @@ class RoomKeysVersionResponse {
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final data = <String, dynamic>{}; final data = <String, dynamic>{};
data['algorithm'] = algorithm?.algorithmString; data['algorithm'] = algorithm?.algorithmString;
data['auth_data'] = authData?.toJson(); data['auth_data'] = authData;
data['count'] = count; data['count'] = count;
data['etag'] = etag; data['etag'] = etag;
data['version'] = version; data['version'] = version;

View file

@ -1596,10 +1596,10 @@ void main() {
matrixApi.accessToken = '1234'; matrixApi.accessToken = '1234';
final algorithm = RoomKeysAlgorithmType.v1Curve25519AesSha2; final algorithm = RoomKeysAlgorithmType.v1Curve25519AesSha2;
final authData = RoomKeysAuthDataV1Curve25519AesSha2.fromJson({ final authData = <String, dynamic>{
'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM', 'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM',
'signatures': {}, 'signatures': {},
}); };
final ret = await matrixApi.createRoomKeysBackup(algorithm, authData); final ret = await matrixApi.createRoomKeysBackup(algorithm, authData);
expect( expect(
FakeMatrixApi.api['POST'] FakeMatrixApi.api['POST']
@ -1619,10 +1619,10 @@ void main() {
matrixApi.accessToken = '1234'; matrixApi.accessToken = '1234';
final algorithm = RoomKeysAlgorithmType.v1Curve25519AesSha2; final algorithm = RoomKeysAlgorithmType.v1Curve25519AesSha2;
final authData = RoomKeysAuthDataV1Curve25519AesSha2.fromJson({ final authData = <String, dynamic>{
'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM', 'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM',
'signatures': {}, 'signatures': {},
}); };
await matrixApi.updateRoomKeysBackup('5', algorithm, authData); await matrixApi.updateRoomKeysBackup('5', algorithm, authData);
}); });
test('deleteRoomKeysBackup', () async { test('deleteRoomKeysBackup', () async {