Line data Source code
1 : /* MIT License
2 : *
3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
4 : *
5 : * Permission is hereby granted, free of charge, to any person obtaining a copy
6 : * of this software and associated documentation files (the "Software"), to deal
7 : * in the Software without restriction, including without limitation the rights
8 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : * copies of the Software, and to permit persons to whom the Software is
10 : * furnished to do so, subject to the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included in all
13 : * copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 : * SOFTWARE.
22 : */
23 :
24 : import 'package:matrix/matrix_api_lite.dart';
25 :
26 : abstract class MatrixSignableKey {
27 : String userId;
28 :
29 : String? get identifier;
30 :
31 : Map<String, String> keys;
32 : Map<String, Map<String, String>>? signatures;
33 : Map<String, Object?>? unsigned;
34 :
35 0 : MatrixSignableKey(this.userId, this.keys, this.signatures, {this.unsigned});
36 :
37 : // This object is used for signing so we need the raw json too
38 : Map<String, Object?>? _json;
39 :
40 31 : MatrixSignableKey.fromJson(Map<String, Object?> json)
41 : : _json = json,
42 31 : userId = json['user_id'] as String,
43 62 : keys = Map<String, String>.from(json['keys'] as Map<String, Object?>),
44 : // we need to manually copy to ensure that our map is Map<String, Map<String, String>>
45 31 : signatures = (() {
46 31 : final orig = json.tryGetMap<String, Object?>('signatures');
47 31 : final res = <String, Map<String, String>>{};
48 : for (final entry
49 96 : in (orig?.entries ?? <MapEntry<String, Object?>>[])) {
50 31 : final deviceSigs = entry.value;
51 31 : if (deviceSigs is Map<String, Object?>) {
52 62 : for (final nestedEntry in deviceSigs.entries) {
53 31 : final nestedValue = nestedEntry.value;
54 31 : if (nestedValue is String) {
55 155 : (res[entry.key] ??= <String, String>{})[nestedEntry.key] =
56 : nestedValue;
57 : }
58 : }
59 : }
60 : }
61 : return res;
62 31 : }()),
63 62 : unsigned = json.tryGetMap<String, Object?>('unsigned')?.copy();
64 :
65 31 : Map<String, Object?> toJson() {
66 31 : final data = _json ?? <String, Object?>{};
67 62 : data['user_id'] = userId;
68 62 : data['keys'] = keys;
69 :
70 31 : if (signatures != null) {
71 62 : data['signatures'] = signatures;
72 : }
73 31 : if (unsigned != null) {
74 60 : data['unsigned'] = unsigned;
75 : }
76 : return data;
77 : }
78 : }
79 :
80 : class MatrixCrossSigningKey extends MatrixSignableKey {
81 : List<String> usage;
82 :
83 60 : String? get publicKey => identifier;
84 :
85 0 : MatrixCrossSigningKey(
86 : String userId,
87 : this.usage,
88 : Map<String, String> keys,
89 : Map<String, Map<String, String>> signatures, {
90 : Map<String, Object?>? unsigned,
91 0 : }) : super(userId, keys, signatures, unsigned: unsigned);
92 :
93 30 : @override
94 90 : String? get identifier => keys.values.first;
95 :
96 31 : @override
97 : MatrixCrossSigningKey.fromJson(super.json)
98 31 : : usage = json.tryGetList<String>('usage') ?? [],
99 31 : super.fromJson();
100 :
101 30 : @override
102 : Map<String, Object?> toJson() {
103 30 : final data = super.toJson();
104 60 : data['usage'] = usage;
105 : return data;
106 : }
107 : }
108 :
109 : class MatrixDeviceKeys extends MatrixSignableKey {
110 : String deviceId;
111 : List<String> algorithms;
112 :
113 0 : String? get deviceDisplayName =>
114 0 : unsigned?.tryGet<String>('device_display_name');
115 :
116 0 : MatrixDeviceKeys(
117 : String userId,
118 : this.deviceId,
119 : this.algorithms,
120 : Map<String, String> keys,
121 : Map<String, Map<String, String>> signatures, {
122 : Map<String, Object?>? unsigned,
123 0 : }) : super(userId, keys, signatures, unsigned: unsigned);
124 :
125 0 : @override
126 0 : String? get identifier => deviceId;
127 :
128 31 : @override
129 : MatrixDeviceKeys.fromJson(super.json)
130 31 : : algorithms = json.tryGetList<String>('algorithms') ?? [],
131 31 : deviceId = json['device_id'] as String,
132 31 : super.fromJson();
133 :
134 31 : @override
135 : Map<String, Object?> toJson() {
136 31 : final data = super.toJson();
137 62 : data['device_id'] = deviceId;
138 62 : data['algorithms'] = algorithms;
139 : return data;
140 : }
141 : }
|