-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathsession.dart
More file actions
169 lines (140 loc) · 6.29 KB
/
Copy pathsession.dart
File metadata and controls
169 lines (140 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'server_url.dart';
/// Persists the atomic session across restarts.
///
/// The **agent secret** is the one sensitive value — it is the account's private
/// key — so it lives in platform secure storage (EncryptedSharedPreferences on
/// Android, Keychain on iOS), not in plain SharedPreferences. Everything else
/// (server URL, active drive, known servers, peers) is non-sensitive routing
/// state and stays in SharedPreferences.
class AtomicSession {
static const _keyServerUrl = 'atomic_server_url';
static const _keySecret = 'atomic_agent_secret';
static const _keyDrive = 'atomic_drive';
static const _secure = FlutterSecureStorage(
aOptions: AndroidOptions(encryptedSharedPreferences: true),
);
static Future<void> save({
required String serverUrl,
required String secret,
String? drive,
}) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_keyServerUrl, serverUrl);
await _secure.write(key: _keySecret, value: secret);
// Clear any legacy plaintext copy left in SharedPreferences.
await prefs.remove(_keySecret);
if (drive != null) {
await prefs.setString(_keyDrive, drive);
}
await addKnownServer(serverUrl);
}
static Future<void> saveDrive(String drive) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_keyDrive, drive);
}
static Future<({String serverUrl, String secret, String? drive})?>
load() async {
final prefs = await SharedPreferences.getInstance();
final serverUrl = prefs.getString(_keyServerUrl);
final secret = await _loadSecret(prefs);
if (serverUrl == null || secret == null) return null;
return (
serverUrl: serverUrl,
secret: secret,
drive: prefs.getString(_keyDrive),
);
}
/// Read the secret from secure storage, migrating a pre-existing plaintext
/// SharedPreferences secret into it on first run so nobody has to re-sign-in.
static Future<String?> _loadSecret(SharedPreferences prefs) async {
final secure = await _secure.read(key: _keySecret);
if (secure != null) return secure;
final legacy = prefs.getString(_keySecret);
if (legacy != null) {
await _secure.write(key: _keySecret, value: legacy);
await prefs.remove(_keySecret);
return legacy;
}
return null;
}
static Future<void> clear() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_keyServerUrl);
await _secure.delete(key: _keySecret);
await prefs.remove(_keySecret); // legacy, if any
await prefs.remove(_keyDrive);
}
// ── Servers ───────────────────────────────────────────────────────
//
// The session has one *active* server, but remembers the others, so
// switching back is a tap rather than retyping a URL. Sync hubs are
// optional here: an empty list is a device-to-device-only setup.
static const _keyKnownServers = 'atomic_known_servers';
/// Every server this device knows, active one included. Stable order —
/// switching must not shuffle the list under the finger that tapped it.
static Future<List<String>> knownServers() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getStringList(_keyKnownServers) ?? [];
}
/// The server in use, or null when syncing device-to-device only.
static Future<String?> activeServer() async {
final prefs = await SharedPreferences.getInstance();
final url = prefs.getString(_keyServerUrl);
return url == null || url.isEmpty ? null : url;
}
/// Remembers `url` without switching to it. Normalizes first, so
/// `localhost:9883` and `http://localhost:9883/` are one entry.
static Future<void> addKnownServer(String url) async {
final normalized = normalizeServerUrl(url);
if (normalized.isEmpty) return;
final prefs = await SharedPreferences.getInstance();
final servers = prefs.getStringList(_keyKnownServers) ?? [];
if (servers.any((s) => sameOrigin(s, normalized))) return;
servers.add(normalized);
await prefs.setStringList(_keyKnownServers, servers);
}
/// Makes `url` the active server, remembering it.
static Future<void> setActiveServer(String url) async {
final normalized = normalizeServerUrl(url);
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_keyServerUrl, normalized);
await addKnownServer(normalized);
}
/// Forgets `url`. Clears the active server too when it was the one removed,
/// leaving the device syncing peer-to-peer rather than pointed at a server
/// it was just told to forget.
static Future<void> removeKnownServer(String url) async {
// Normalized first: `localhost:9883` parses as scheme `localhost`, which
// matches nothing stored. Removing must accept what adding accepted.
final normalized = normalizeServerUrl(url);
final prefs = await SharedPreferences.getInstance();
final servers = prefs.getStringList(_keyKnownServers) ?? [];
servers.removeWhere((s) => sameOrigin(s, normalized));
await prefs.setStringList(_keyKnownServers, servers);
if (sameOrigin(normalized, prefs.getString(_keyServerUrl))) {
await prefs.setString(_keyServerUrl, '');
}
}
// ── Paired peers ──────────────────────────────────────────────────
static const _keyPeers = 'atomic_peers';
static Future<List<String>> loadPeers() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getStringList(_keyPeers) ?? [];
}
static Future<void> addPeer(String nodeId) async {
final prefs = await SharedPreferences.getInstance();
final peers = prefs.getStringList(_keyPeers) ?? [];
if (!peers.contains(nodeId)) {
peers.add(nodeId);
await prefs.setStringList(_keyPeers, peers);
}
}
static Future<void> removePeer(String nodeId) async {
final prefs = await SharedPreferences.getInstance();
final peers = prefs.getStringList(_keyPeers) ?? [];
peers.remove(nodeId);
await prefs.setStringList(_keyPeers, peers);
}
}