Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose macOS Keychain options #405

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions flutter_secure_storage/lib/flutter_secure_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage_platform_interface/flutter_secure_storage_platform_interface.dart';

part './options/web_options.dart';
part './options/ios_options.dart';
part './options/android_options.dart';
part './options/apple_options.dart';
part './options/ios_options.dart';
part './options/linux_options.dart';
part './options/windows_options.dart';
part './options/macos_options.dart';
part './options/web_options.dart';
part './options/windows_options.dart';

class FlutterSecureStorage {
final IOSOptions iOptions;
Expand Down
52 changes: 52 additions & 0 deletions flutter_secure_storage/lib/options/apple_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
part of flutter_secure_storage;

// KeyChain accessibility attributes as defined here:
// https://developer.apple.com/documentation/security/ksecattraccessible?language=objc

enum KeychainAccessibility {
// The data in the keychain can only be accessed when the device is unlocked.
// Only available if a passcode is set on the device.
// Items with this attribute do not migrate to a new device.
passcode,

// The data in the keychain item can be accessed only while the device is unlocked by the user.
unlocked,

// The data in the keychain item can be accessed only while the device is unlocked by the user.
// Items with this attribute do not migrate to a new device.
unlocked_this_device,

// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
first_unlock,

// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
// Items with this attribute do not migrate to a new device.
first_unlock_this_device,
}

abstract class AppleOptions extends Options {
const AppleOptions({
String? groupId,
String? accountName = AppleOptions.defaultAccountName,
KeychainAccessibility accessibility = KeychainAccessibility.unlocked,
bool synchronizable = false,
}) : _groupId = groupId,
_accessibility = accessibility,
_accountName = accountName,
_synchronizable = synchronizable;

static const defaultAccountName = 'flutter_secure_storage_service';

final String? _groupId;
final String? _accountName;
final KeychainAccessibility _accessibility;
final bool _synchronizable;

@override
Map<String, String> toMap() => <String, String>{
'accessibility': describeEnum(_accessibility),
if (_accountName != null) 'accountName': _accountName!,
if (_groupId != null) 'groupId': _groupId!,
'synchronizable': '$_synchronizable',
};
}
56 changes: 10 additions & 46 deletions flutter_secure_storage/lib/options/ios_options.dart
Original file line number Diff line number Diff line change
@@ -1,60 +1,24 @@
part of flutter_secure_storage;

// KeyChain accessibility attributes as defined here:
// https://developer.apple.com/documentation/security/ksecattraccessible?language=objc
enum IOSAccessibility {
// The data in the keychain can only be accessed when the device is unlocked.
// Only available if a passcode is set on the device.
// Items with this attribute do not migrate to a new device.
passcode,

// The data in the keychain item can be accessed only while the device is unlocked by the user.
unlocked,

// The data in the keychain item can be accessed only while the device is unlocked by the user.
// Items with this attribute do not migrate to a new device.
unlocked_this_device,

// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
first_unlock,

// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
// Items with this attribute do not migrate to a new device.
first_unlock_this_device,
}

class IOSOptions extends Options {
class IOSOptions extends AppleOptions {
const IOSOptions({
String? groupId,
String? accountName = IOSOptions.defaultAccountName,
IOSAccessibility accessibility = IOSAccessibility.unlocked,
String? accountName = AppleOptions.defaultAccountName,
KeychainAccessibility accessibility = KeychainAccessibility.unlocked,
bool synchronizable = false,
}) : _groupId = groupId,
_accessibility = accessibility,
_accountName = accountName,
_synchronizable = synchronizable;

static const defaultAccountName = 'flutter_secure_storage_service';
}) : super(
groupId: groupId,
accountName: accountName,
accessibility: accessibility,
synchronizable: synchronizable,
);

static const IOSOptions defaultOptions = IOSOptions();

final String? _groupId;
final String? _accountName;
final IOSAccessibility _accessibility;
final bool _synchronizable;

@override
Map<String, String> toMap() => <String, String>{
'accessibility': describeEnum(_accessibility),
if (_accountName != null) 'accountName': _accountName!,
if (_groupId != null) 'groupId': _groupId!,
'synchronizable': '$_synchronizable',
};

IOSOptions copyWith({
String? groupId,
String? accountName,
IOSAccessibility? accessibility,
KeychainAccessibility? accessibility,
bool? synchronizable,
}) =>
IOSOptions(
Expand Down
28 changes: 24 additions & 4 deletions flutter_secure_storage/lib/options/macos_options.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
part of flutter_secure_storage;

class MacOsOptions extends Options {
const MacOsOptions();
class MacOsOptions extends AppleOptions {
const MacOsOptions({
String? groupId,
String? accountName = AppleOptions.defaultAccountName,
KeychainAccessibility accessibility = KeychainAccessibility.unlocked,
bool synchronizable = false,
}) : super(
groupId: groupId,
accountName: accountName,
accessibility: accessibility,
synchronizable: synchronizable,
);

static const MacOsOptions defaultOptions = MacOsOptions();

@override
Map<String, String> toMap() => <String, String>{};
MacOsOptions copyWith({
String? groupId,
String? accountName,
KeychainAccessibility? accessibility,
bool? synchronizable,
}) =>
MacOsOptions(
groupId: groupId ?? _groupId,
accountName: accountName ?? _accountName,
accessibility: accessibility ?? _accessibility,
synchronizable: synchronizable ?? _synchronizable,
);
}