Skip to content

Commit

Permalink
testUpdateItemNotFoundFails test fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Nov 1, 2023
1 parent 30e2c01 commit 2ded540
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/WalletConnectKMS/Keychain/KeychainStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class KeychainStorage: KeychainStorageProtocol {
case errSecSuccess:
return
case errSecItemNotFound:
tryMigrateAttrAccessibleOnUpdate(data: data, key: key) // TODO: Remove once migration period ends
try tryMigrateAttrAccessibleOnUpdate(data: data, key: key) // TODO: Remove once migration period ends
default:
throw KeychainError(status)
}
Expand Down Expand Up @@ -133,20 +133,24 @@ public final class KeychainStorage: KeychainStorageProtocol {
return item as? Data
}

private func tryMigrateAttrAccessibleOnUpdate(data: Data, key: String) {
private func tryMigrateAttrAccessibleOnUpdate(data: Data, key: String) throws {
var updateAccessQuery = buildBaseServiceQuery(for: key)
updateAccessQuery[kSecAttrAccessible] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly

let accessAttributes = [kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly]
let accessStatus = secItem.update(updateAccessQuery as CFDictionary, accessAttributes as CFDictionary)

guard accessStatus == errSecSuccess else {
return
throw KeychainError.itemNotFound
}

let updateQuery = buildBaseServiceQuery(for: key)
let updateAttributes = [kSecValueData: data]

_ = secItem.update(updateQuery as CFDictionary, updateAttributes as CFDictionary)
let updateStatus = secItem.update(updateQuery as CFDictionary, updateAttributes as CFDictionary)

guard updateStatus == errSecSuccess else {
throw KeychainError.itemNotFound
}
}
}

0 comments on commit 2ded540

Please sign in to comment.