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 30 : MatrixSignableKey.fromJson(Map<String, Object?> json)
41 : : _json = json,
42 30 : userId = json['user_id'] as String,
43 60 : 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 30 : signatures = (() {
46 30 : final orig = json.tryGetMap<String, Object?>('signatures');
47 30 : final res = <String, Map<String, String>>{};
48 : for (final entry
49 93 : in (orig?.entries ?? <MapEntry<String, Object?>>[])) {
50 30 : final deviceSigs = entry.value;
51 30 : if (deviceSigs is Map<String, Object?>) {
52 60 : for (final nestedEntry in deviceSigs.entries) {
53 30 : final nestedValue = nestedEntry.value;
54 30 : if (nestedValue is String) {
55 150 : (res[entry.key] ??= <String, String>{})[nestedEntry.key] =
56 : nestedValue;
57 : }
58 : }
59 : }
60 : }
61 : return res;
62 30 : }()),
63 60 : unsigned = json.tryGetMap<String, Object?>('unsigned')?.copy();
64 :
65 30 : Map<String, Object?> toJson() {
66 30 : final data = _json ?? <String, Object?>{};
67 60 : data['user_id'] = userId;
68 60 : data['keys'] = keys;
69 :
70 30 : if (signatures != null) {
71 60 : data['signatures'] = signatures;
72 : }
73 30 : if (unsigned != null) {
74 58 : data['unsigned'] = unsigned;
75 : }
76 : return data;
77 : }
78 : }
79 :
80 : class MatrixCrossSigningKey extends MatrixSignableKey {
81 : List<String> usage;
82 :
83 58 : 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 29 : @override
94 87 : String? get identifier => keys.values.first;
95 :
96 30 : @override
97 : MatrixCrossSigningKey.fromJson(super.json)
98 30 : : usage = json.tryGetList<String>('usage') ?? [],
99 30 : super.fromJson();
100 :
101 29 : @override
102 : Map<String, Object?> toJson() {
103 29 : final data = super.toJson();
104 58 : 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 30 : @override
129 : MatrixDeviceKeys.fromJson(super.json)
130 30 : : algorithms = json.tryGetList<String>('algorithms') ?? [],
131 30 : deviceId = json['device_id'] as String,
132 30 : super.fromJson();
133 :
134 30 : @override
135 : Map<String, Object?> toJson() {
136 30 : final data = super.toJson();
137 60 : data['device_id'] = deviceId;
138 60 : data['algorithms'] = algorithms;
139 : return data;
140 : }
141 : }
|