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

[Core] Update method migration #1206

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Changes from 1 commit
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
30 changes: 26 additions & 4 deletions Sources/WalletConnectKMS/Keychain/KeychainStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public final class KeychainStorage: KeychainStorageProtocol {
case errSecSuccess:
return item as? Data
case errSecItemNotFound:
return tryMigrateAttrAccessible(key: key) // TODO: Replace with nil once migration period ends
return tryMigrateAttrAccessibleOnRead(key: key) // TODO: Replace with nil once migration period ends
default:
throw KeychainError(status)
}
Expand All @@ -70,8 +70,13 @@ public final class KeychainStorage: KeychainStorageProtocol {
let attributes = [kSecValueData: data]

let status = secItem.update(query as CFDictionary, attributes as CFDictionary)

guard status == errSecSuccess else {

switch status {
case errSecSuccess:
return
case errSecItemNotFound:
tryMigrateAttrAccessibleOnUpdate(data: data, key: key) // TODO: Remove once migration period ends
default:
throw KeychainError(status)
}
}
Expand Down Expand Up @@ -108,7 +113,7 @@ public final class KeychainStorage: KeychainStorageProtocol {
]
}

private func tryMigrateAttrAccessible(key: String) -> Data? {
private func tryMigrateAttrAccessibleOnRead(key: String) -> Data? {
var updateQuery = buildBaseServiceQuery(for: key)
updateQuery[kSecAttrAccessible] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly

Expand All @@ -127,4 +132,21 @@ public final class KeychainStorage: KeychainStorageProtocol {

return item as? Data
}

private func tryMigrateAttrAccessibleOnUpdate(data: Data, key: String) {
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
}

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

_ = secItem.update(updateQuery as CFDictionary, updateAttributes as CFDictionary)
}
}
Loading