LCOV - code coverage report
Current view: top level - lib/matrix_api_lite/model - matrix_keys.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 40 49 81.6 %
Date: 2024-09-28 12:47:43 Functions: 0 0 -

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

Generated by: LCOV version 1.14