Line data Source code
1 : /*
2 : * Famedly Matrix SDK
3 : * Copyright (C) 2020, 2021 Famedly GmbH
4 : *
5 : * This program is free software: you can redistribute it and/or modify
6 : * it under the terms of the GNU Affero General Public License as
7 : * published by the Free Software Foundation, either version 3 of the
8 : * License, or (at your option) any later version.
9 : *
10 : * This program is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : * GNU Affero General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Affero General Public License
16 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 : */
18 :
19 : import 'package:olm/olm.dart' as olm;
20 :
21 : import 'package:matrix/matrix.dart';
22 :
23 : class OlmSession {
24 : String identityKey;
25 : String? sessionId;
26 : olm.Session? session;
27 : DateTime? lastReceived;
28 : final String key;
29 92 : String? get pickledSession => session?.pickle(key);
30 :
31 8 : bool get isValid => session != null;
32 :
33 23 : OlmSession({
34 : required this.key,
35 : required this.identityKey,
36 : required this.sessionId,
37 : required this.session,
38 : required this.lastReceived,
39 : });
40 :
41 5 : OlmSession.fromJson(Map<String, dynamic> dbEntry, this.key)
42 5 : : identityKey = dbEntry['identity_key'] ?? '' {
43 10 : session = olm.Session();
44 : try {
45 20 : session!.unpickle(key, dbEntry['pickle']);
46 8 : sessionId = dbEntry['session_id'];
47 4 : lastReceived =
48 8 : DateTime.fromMillisecondsSinceEpoch(dbEntry['last_received'] ?? 0);
49 16 : assert(sessionId == session!.session_id());
50 : } catch (e, s) {
51 2 : Logs().e('[LibOlm] Could not unpickle olm session', e, s);
52 1 : dispose();
53 : }
54 : }
55 :
56 21 : void dispose() {
57 42 : session?.free();
58 21 : session = null;
59 : }
60 : }
|