Skip to content

Commit

Permalink
chore(core): Secure storage clean up (#61)
Browse files Browse the repository at this point in the history
- Separate memory implementation into its own class
- Clean up Linux implementation
  • Loading branch information
dnys1 authored Mar 7, 2024
1 parent cad92d1 commit eeae84b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 51 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/celest_core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,23 @@ jobs:
# - name: Test (Windows)
# working-directory: packages/celest_core/example
# run: flutter test -d windows integration_test/secure_storage_test.dart
test_web:
needs: analyze_and_format
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Git Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # 4.1.1
- name: Setup Flutter
uses: subosito/flutter-action@62f096cacda5168a3bd7b95793373be14fa4fbaf # 2.13.0
with:
cache: true
- name: Get Packages
working-directory: packages/celest_core
run: dart pub get
- name: Test (Chrome, dart2js)
working-directory: packages/celest_core
run: dart test -p chrome
- name: Test (Chrome, dart2wasm)
working-directory: packages/celest_core
run: dart test -p chrome -c dart2wasm
7 changes: 7 additions & 0 deletions packages/celest_core/ffigen.glib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedefs:
- gboolean
- gint
- gpointer
- gchar
functions:
include:
- g_hash_table_new
Expand All @@ -43,3 +44,9 @@ structs:
"_GHashTable": GHashTable
"_GCancellable": GCancellable
"_GObject": GObject
type-map:
typedefs:
gchar:
lib: pkg_ffi
c-type: Utf8
dart-type: Char
9 changes: 5 additions & 4 deletions packages/celest_core/lib/src/native/linux/glib.ffi.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions packages/celest_core/lib/src/secure_storage/secure_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@ abstract interface class SecureStorage {
String? delete(String key);
void clear();
}

/// An in-memory implementation of [SecureStorage].
final class MemorySecureStorage implements SecureStorage {
MemorySecureStorage({
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ final class SecureStoragePlatformLinux extends SecureStoragePlatform {
if (application == nullptr) {
return File('/proc/self/exe').resolveSymbolicLinksSync();
}
return _gio
.g_application_get_application_id(application)
.cast<Utf8>()
.toDartString();
return _gio.g_application_get_application_id(application).toDartString();
}();

String _labelFor(String key) => '$scope/$key';
Pointer<SecretSchema> _schemaFor(Arena arena) => arena<SecretSchema>()
..ref.name = _appName.toNativeUtf8(allocator: arena)
Pointer<SecretSchema> _schema(Arena arena) => arena<SecretSchema>()
..ref.name = '$_appName/$scope'.toNativeUtf8(allocator: arena)
..ref.flags = SecretSchemaFlags.SECRET_SCHEMA_NONE
..ref.attributes[0].name = 'key'.toNativeUtf8(allocator: arena)
..ref.attributes[0].type =
Expand All @@ -64,7 +60,7 @@ final class SecureStoragePlatformLinux extends SecureStoragePlatform {

@override
void clear() => using((arena) {
final schema = _schemaFor(arena);
final schema = _schema(arena);
final attributes = _attributes(arena: arena);
_check(
(err) => _libSecret.secret_password_clearv_sync(
Expand All @@ -80,7 +76,7 @@ final class SecureStoragePlatformLinux extends SecureStoragePlatform {
@override
String? delete(String key) => using((arena) {
final secret = read(key);
final schema = _schemaFor(arena);
final schema = _schema(arena);
final attributes = _attributes(key: key, arena: arena);
_check(
(err) => _libSecret.secret_password_clearv_sync(
Expand All @@ -97,7 +93,7 @@ final class SecureStoragePlatformLinux extends SecureStoragePlatform {
@override
String? read(String key) => using((arena) {
final attributes = _attributes(key: key, arena: arena);
final schema = _schemaFor(arena);
final schema = _schema(arena);
final result = _check(
(err) => _libSecret.secret_password_lookupv_sync(
schema,
Expand All @@ -117,12 +113,13 @@ final class SecureStoragePlatformLinux extends SecureStoragePlatform {
@override
String write(String key, String value) {
using((arena) {
final label = _labelFor(key).toNativeUtf8(allocator: arena);
final schema = _schema(arena);
final label = key.toNativeUtf8(allocator: arena);
final secret = value.toNativeUtf8(allocator: arena);
final attributes = _attributes(key: key, arena: arena);
_check(
(err) => _libSecret.secret_password_storev_sync(
_schemaFor(arena),
schema,
attributes,
nullptr,
label,
Expand All @@ -145,7 +142,7 @@ final class SecureStoragePlatformLinux extends SecureStoragePlatform {
final error = err.value;
if (error != nullptr) {
arena.onReleaseAll(() => _glib.g_error_free(error));
final message = error.ref.message.cast<Utf8>().toDartString();
final message = error.ref.message.toDartString();
throw SecureStorageUnknownException(message);
}
return result;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import 'package:celest_core/src/secure_storage/secure_storage.dart';
import 'package:celest_core/src/secure_storage/secure_storage.stub.dart';
import 'package:meta/meta.dart';

abstract base class SecureStoragePlatform implements SecureStorage {
factory SecureStoragePlatform({
String? scope,
}) =>
SecureStoragePlatformStub(scope: scope ?? _defaultScope);
extension type SecureStoragePlatform._(SecureStorage _impl)
implements SecureStorage {
SecureStoragePlatform({String? scope})
: _impl = MemorySecureStorage(scope: scope ?? _defaultScope);

static const _defaultScope = 'dev.celest.celest';

@protected
const SecureStoragePlatform.base();
}

0 comments on commit eeae84b

Please sign in to comment.