From 033342f1d7639cb7f97349717d3f9f55208f8234 Mon Sep 17 00:00:00 2001 From: luckyrat Date: Mon, 4 Jul 2022 15:30:32 +0100 Subject: [PATCH 1/3] Fix ios authenticationValidityDurationSeconds --- macos/Classes/BiometricStorageImpl.swift | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/macos/Classes/BiometricStorageImpl.swift b/macos/Classes/BiometricStorageImpl.swift index 30176e20..7fffee3e 100644 --- a/macos/Classes/BiometricStorageImpl.swift +++ b/macos/Classes/BiometricStorageImpl.swift @@ -42,6 +42,7 @@ class BiometricStorageImpl { } private lazy var context: LAContext = LAContext() + private var contextFirstAuthenticatedAt: Date? private var stores: [String: InitOptions] = [:] private let storageError: StorageError private let storageMethodNotImplemented: Any @@ -56,6 +57,23 @@ class BiometricStorageImpl { kSecAttrAccount as String: name] } + private func currentContext(name: String) -> LAContext { + guard let initOptions = stores[name] else { + hpdebug("Store not initialised; creating new context.") + context = LAContext() + contextFirstAuthenticatedAt = nil + return context + } + + if (initOptions.authenticationValidityDurationSeconds <= 0 || + contextFirstAuthenticatedAt == nil || + contextFirstAuthenticatedAt! < Date(timeIntervalSinceNow: Double(-initOptions.authenticationValidityDurationSeconds))) { + context = LAContext() + contextFirstAuthenticatedAt = nil + } + return context; + } + public func handle(_ call: StorageMethodCall, result: @escaping StorageCallback) { func requiredArg(_ name: String, _ cb: (T) -> Void) { @@ -114,6 +132,7 @@ class BiometricStorageImpl { private func read(_ name: String, _ result: @escaping StorageCallback, _ promptInfo: IOSPromptInfo) { + let context = currentContext(name:name) var query = baseQuery(name: name) query[kSecMatchLimit as String] = kSecMatchLimitOne query[kSecUseOperationPrompt as String] = promptInfo.accessTitle @@ -139,6 +158,9 @@ class BiometricStorageImpl { result(storageError(code: "RetrieveError", message: "Unexpected data.", details: nil)) return } + if (contextFirstAuthenticatedAt == nil) { + contextFirstAuthenticatedAt = Date() + } result(dataString) } @@ -166,6 +188,7 @@ class BiometricStorageImpl { } var query = baseQuery(name: name) + let context = currentContext(name:name) if (initOptions.authenticationRequired) { if initOptions.authenticationValidityDurationSeconds > 0 { @@ -211,6 +234,9 @@ class BiometricStorageImpl { handleOSStatusError(status, result, "writing data") return } + if (contextFirstAuthenticatedAt == nil) { + contextFirstAuthenticatedAt = Date() + } result(nil) } From 59f28197995d215dacd5d584f10e13f800d0004a Mon Sep 17 00:00:00 2001 From: luckyrat Date: Mon, 6 Dec 2021 22:06:38 +0000 Subject: [PATCH 2/3] Enable forceInit feature --- .../biometric_storage/BiometricStoragePlugin.kt | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/android/src/main/kotlin/design/codeux/biometric_storage/BiometricStoragePlugin.kt b/android/src/main/kotlin/design/codeux/biometric_storage/BiometricStoragePlugin.kt index 45488970..98640e92 100644 --- a/android/src/main/kotlin/design/codeux/biometric_storage/BiometricStoragePlugin.kt +++ b/android/src/main/kotlin/design/codeux/biometric_storage/BiometricStoragePlugin.kt @@ -208,16 +208,9 @@ class BiometricStoragePlugin : FlutterPlugin, ActivityAware, MethodCallHandler { "canAuthenticate" -> result.success(canAuthenticate().name) "init" -> { val name = getName() - if (storageFiles.containsKey(name)) { - if (call.argument("forceInit") == true) { - throw MethodCallException( - "AlreadyInitialized", - "A storage file with the name '$name' was already initialized." - ) - } else { - result.success(false) - return - } + if (storageFiles.containsKey(name) && call.argument("forceInit") != true) { + result.success(false) + return } val options = call.argument>("options")?.let { it -> From a4ab555b46c92bcf57ceb028363e0e6246a0cd3d Mon Sep 17 00:00:00 2001 From: luckyrat Date: Mon, 4 Jul 2022 17:19:50 +0100 Subject: [PATCH 3/3] Increment version number --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index c4be32ae..f230a093 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: biometric_storage description: | Secure Storage: Encrypted data store optionally secured by biometric lock with support for iOS, Android, MacOS. Partial support for Linux, Windows and web (localStorage). -version: 4.1.3 +version: 4.1.4 homepage: https://github.com/authpass/biometric_storage/ environment: