askrobots / network-computer-flutter
Public
One Flutter client for network-computer: drive a remote Linux/Mac desktop from Android, iPhone, iPad, or desktop over WebRTC.
Dan Quellhorst
No colour (compare) in ⋯: the desk's picture in grey on this device
c74471a
1 month, 3 weeks ago
History
secrets.dart · 2.4 KB · 60 lines
| 1 | import 'package:flutter/foundation.dart'; |
| 2 | import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 3 | import 'package:shared_preferences/shared_preferences.dart'; |
| 4 | |
| 5 | /// Credentials (the rendezvous password, pairing tokens) live in the |
| 6 | /// platform keychain, not in plain app preferences. Values saved by older |
| 7 | /// versions in preferences are moved there on first read. If the keychain |
| 8 | /// can't be used, preferences stay in use: a credential is never lost. |
| 9 | class Secrets { |
| 10 | /// Developer test runs (--dart-define=NC_AUTO_URL=...) keep credentials in |
| 11 | /// memory only: each test build is signed differently, and macOS would ask |
| 12 | /// for keychain access every time. |
| 13 | static final bool memoryOnly = const String.fromEnvironment('NC_AUTO_URL').isNotEmpty; |
| 14 | static final Map<String, String> _memory = {}; |
| 15 | |
| 16 | static const _store = FlutterSecureStorage( |
| 17 | iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock_this_device), |
| 18 | // the login keychain: the data-protection one needs a signed entitlement |
| 19 | mOptions: MacOsOptions( |
| 20 | accessibility: KeychainAccessibility.first_unlock_this_device, |
| 21 | usesDataProtectionKeychain: false), |
| 22 | ); |
| 23 | |
| 24 | static Future<String?> read(String key) async { |
| 25 | if (memoryOnly) return _memory[key]; |
| 26 | final p = await SharedPreferences.getInstance(); |
| 27 | try { |
| 28 | final v = await _store.read(key: key); |
| 29 | if (v != null) return v; |
| 30 | final old = p.getString(key); |
| 31 | if (old != null) { // migrate from plain preferences |
| 32 | await _store.write(key: key, value: old); |
| 33 | if (await _store.read(key: key) == old) await p.remove(key); |
| 34 | } |
| 35 | return old; |
| 36 | } catch (e) { |
| 37 | debugPrint('nc secrets: keychain unavailable ($e); using preferences'); |
| 38 | return p.getString(key); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static Future<void> write(String key, String value) async { |
| 43 | if (memoryOnly) { _memory[key] = value; return; } |
| 44 | try { |
| 45 | await _store.write(key: key, value: value); |
| 46 | (await SharedPreferences.getInstance()).remove(key); |
| 47 | } catch (e) { |
| 48 | debugPrint('nc secrets: keychain unavailable ($e); using preferences'); |
| 49 | await (await SharedPreferences.getInstance()).setString(key, value); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static Future<void> delete(String key) async { |
| 54 | if (memoryOnly) { _memory.remove(key); return; } |
| 55 | try { |
| 56 | await _store.delete(key: key); |
| 57 | } catch (_) {} |
| 58 | (await SharedPreferences.getInstance()).remove(key); |
| 59 | } |
| 60 | } |