-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): Split storage interfaces (#62)
Create two variations of a new `Storage` interface: `LocalStorage` and `SecureStorage`. This is necessary in Auth to properly store information, since platform keychains have different persistence characteristics (e.g. iOS/macOS persist after uninstall), and some platforms cannot support secure storage (e.g. Web).
- Loading branch information
Showing
22 changed files
with
151 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
packages/celest_core/example/integration_test/secure_storage_test.dart
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...tegration_test/secure_storage_shared.dart → ...mple/integration_test/storage_shared.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/celest_core/example/integration_test/storage_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:celest_core/src/storage/local/local_storage.dart'; | ||
import 'package:celest_core/src/storage/secure/secure_storage.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
|
||
import 'storage_shared.dart'; | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
sharedTests('SecureStorage', SecureStorage.new); | ||
sharedTests('LocalStorage', LocalStorage.new); | ||
} |
33 changes: 0 additions & 33 deletions
33
packages/celest_core/lib/src/secure_storage/secure_storage.dart
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
packages/celest_core/lib/src/storage/local/local_storage.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:celest_core/src/storage/local/local_storage_platform.vm.dart' | ||
if (dart.library.js_interop) 'package:celest_core/src/storage/local/local_storage_platform.web.dart'; | ||
import 'package:celest_core/src/storage/storage.dart'; | ||
|
||
abstract interface class LocalStorage implements Storage { | ||
factory LocalStorage({String? scope}) = LocalStoragePlatform; | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/celest_core/lib/src/storage/local/local_storage_platform.vm.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:celest_core/src/storage/local/local_storage.dart'; | ||
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart'; | ||
|
||
extension type LocalStoragePlatform._(LocalStorage _impl) | ||
implements LocalStorage { | ||
LocalStoragePlatform({String? scope}) | ||
: _impl = SecureStoragePlatform(scope: scope ?? _defaultScope); | ||
|
||
static const _defaultScope = 'dev.celest.celest'; | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/celest_core/lib/src/storage/local/local_storage_platform.web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:celest_core/src/storage/local/local_storage.dart'; | ||
import 'package:web/web.dart' as web; | ||
|
||
final class LocalStoragePlatform implements LocalStorage { | ||
LocalStoragePlatform({String? scope}) : scope = scope ?? _defaultScope; | ||
|
||
static const _defaultScope = 'dev.celest.celest'; | ||
|
||
final String scope; | ||
final web.Storage _storage = web.window.localStorage; | ||
|
||
@override | ||
void clear() { | ||
for (final key in _storage.keys) { | ||
if (key.startsWith('$scope/')) { | ||
_storage.removeItem(key); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
String? delete(String key) { | ||
final value = read(key); | ||
if (value != null) { | ||
_storage.removeItem('$scope/$key'); | ||
} | ||
return null; | ||
} | ||
|
||
@override | ||
String? read(String key) => _storage['$scope/$key']; | ||
|
||
@override | ||
String write(String key, String value) { | ||
_storage.setItem('$scope/$key', value); | ||
return value; | ||
} | ||
} | ||
|
||
extension on web.Storage { | ||
List<String> get keys => [for (var i = 0; i < length; i++) key(i)!]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:celest_core/src/storage/secure/secure_storage.dart'; | ||
import 'package:celest_core/src/storage/storage.dart'; | ||
|
||
/// An in-memory implementation of [Storage] and [SecureStorage]. | ||
final class MemoryStorage implements Storage, SecureStorage { | ||
MemoryStorage({ | ||
required this.scope, | ||
}); | ||
|
||
final _storage = <String, String>{}; | ||
final String scope; | ||
|
||
@override | ||
void clear() => _storage.removeWhere((key, _) => key.startsWith('$scope/')); | ||
|
||
@override | ||
String? delete(String key) => _storage.remove('$scope/$key'); | ||
|
||
@override | ||
String? read(String key) => _storage['$scope/$key']; | ||
|
||
@override | ||
String write(String key, String value) => _storage['$scope/$key'] = value; | ||
} |
2 changes: 1 addition & 1 deletion
2
...ecure_storage/secure_storage.android.dart → ...torage/secure/secure_storage.android.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/celest_core/lib/src/storage/secure/secure_storage.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart' | ||
if (dart.library.js_interop) 'package:celest_core/src/storage/secure/secure_storage_platform.web.dart'; | ||
import 'package:celest_core/src/storage/storage.dart'; | ||
|
||
abstract interface class SecureStorage implements Storage { | ||
factory SecureStorage({String? scope}) = SecureStoragePlatform; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
14 changes: 8 additions & 6 deletions
14
...e_storage/secure_storage_platform.vm.dart → ...ge/secure/secure_storage_platform.vm.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
..._storage/secure_storage_platform.web.dart → ...e/secure/secure_storage_platform.web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import 'package:celest_core/src/secure_storage/secure_storage.dart'; | ||
import 'package:celest_core/src/storage/memory_storage.dart'; | ||
import 'package:celest_core/src/storage/secure/secure_storage.dart'; | ||
|
||
extension type SecureStoragePlatform._(SecureStorage _impl) | ||
implements SecureStorage { | ||
SecureStoragePlatform({String? scope}) | ||
: _impl = MemorySecureStorage(scope: scope ?? _defaultScope); | ||
: _impl = MemoryStorage(scope: scope ?? _defaultScope); | ||
|
||
static const _defaultScope = 'dev.celest.celest'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:celest_core/src/storage/local/local_storage.dart'; | ||
import 'package:celest_core/src/storage/secure/secure_storage.dart'; | ||
|
||
abstract interface class Storage { | ||
factory Storage.local({String? scope}) = LocalStorage; | ||
factory Storage.secure({String? scope}) = SecureStorage; | ||
|
||
String? read(String key); | ||
String write(String key, String value); | ||
String? delete(String key); | ||
void clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
packages/celest_core/test/secure_storage/secure_storage_test.dart
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...e_storage/secure_storage_darwin_test.dart → ...ge/secure/secure_storage_darwin_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:celest_core/src/storage/local/local_storage.dart'; | ||
import 'package:celest_core/src/storage/secure/secure_storage.dart'; | ||
|
||
import '../../example/integration_test/storage_shared.dart'; | ||
|
||
void main() { | ||
sharedTests('SecureStorage', SecureStorage.new); | ||
sharedTests('LocalStorage', LocalStorage.new); | ||
} |