Skip to content
Closed
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
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