Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>("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<Boolean>("forceInit") != true) {
result.success(false)
return
}

val options = call.argument<Map<String, Any>>("options")?.let { it ->
Expand Down
26 changes: 26 additions & 0 deletions macos/Classes/BiometricStorageImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<T>(_ name: String, _ cb: (T) -> Void) {
Expand Down Expand Up @@ -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
Expand All @@ -139,6 +158,9 @@ class BiometricStorageImpl {
result(storageError(code: "RetrieveError", message: "Unexpected data.", details: nil))
return
}
if (contextFirstAuthenticatedAt == nil) {
contextFirstAuthenticatedAt = Date()
}
result(dataString)
}

Expand Down Expand Up @@ -166,6 +188,7 @@ class BiometricStorageImpl {
}

var query = baseQuery(name: name)
let context = currentContext(name:name)

if (initOptions.authenticationRequired) {
if initOptions.authenticationValidityDurationSeconds > 0 {
Expand Down Expand Up @@ -211,6 +234,9 @@ class BiometricStorageImpl {
handleOSStatusError(status, result, "writing data")
return
}
if (contextFirstAuthenticatedAt == nil) {
contextFirstAuthenticatedAt = Date()
}
result(nil)
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down