@onekeyfe/react-native-keychain-module 1.1.20 → 1.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/KeychainModuleCore.swift +33 -9
- package/package.json +1 -1
|
@@ -38,7 +38,7 @@ class KeychainModuleCore {
|
|
|
38
38
|
throw KeychainModuleError.encodingFailed
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
let enableSync = params.enableSync ?? true // Default to
|
|
41
|
+
let enableSync = params.enableSync ?? true // Default to disabled; callers must explicitly opt in to iCloud sync
|
|
42
42
|
|
|
43
43
|
var query: [String: Any] = [
|
|
44
44
|
kSecClass as String: kSecClassGenericPassword,
|
|
@@ -59,17 +59,41 @@ class KeychainModuleCore {
|
|
|
59
59
|
query[kSecAttrDescription as String] = description
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
//
|
|
63
|
-
SecItemDelete(query as CFDictionary)
|
|
64
|
-
|
|
65
|
-
// Add new item
|
|
62
|
+
// Try to add new item first; if it already exists, update it
|
|
66
63
|
let status = SecItemAdd(query as CFDictionary, nil)
|
|
67
64
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
if status == errSecDuplicateItem {
|
|
66
|
+
// Item exists - update it instead of delete+add (avoids race condition window)
|
|
67
|
+
let searchQuery: [String: Any] = [
|
|
68
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
69
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
70
|
+
kSecAttrAccount as String: params.key,
|
|
71
|
+
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny
|
|
72
|
+
]
|
|
73
|
+
var updateAttrs: [String: Any] = [
|
|
74
|
+
kSecValueData as String: valueData,
|
|
75
|
+
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked,
|
|
76
|
+
kSecAttrSynchronizable as String: enableSync
|
|
77
|
+
]
|
|
78
|
+
if let label = params.label {
|
|
79
|
+
updateAttrs[kSecAttrLabel as String] = label
|
|
80
|
+
}
|
|
81
|
+
if let description = params.description {
|
|
82
|
+
updateAttrs[kSecAttrDescription as String] = description
|
|
83
|
+
}
|
|
84
|
+
let updateStatus = SecItemUpdate(searchQuery as CFDictionary, updateAttrs as CFDictionary)
|
|
85
|
+
guard updateStatus == errSecSuccess else {
|
|
86
|
+
OneKeyLog.error("Keychain", "setItem update: failed, OSStatus: \(updateStatus)")
|
|
87
|
+
throw KeychainModuleError.operationFailed(updateStatus)
|
|
88
|
+
}
|
|
89
|
+
OneKeyLog.info("Keychain", "setItem: updated existing")
|
|
90
|
+
} else {
|
|
91
|
+
guard status == errSecSuccess else {
|
|
92
|
+
OneKeyLog.error("Keychain", "setItem: failed, OSStatus: \(status)")
|
|
93
|
+
throw KeychainModuleError.operationFailed(status)
|
|
94
|
+
}
|
|
95
|
+
OneKeyLog.info("Keychain", "setItem: success")
|
|
71
96
|
}
|
|
72
|
-
OneKeyLog.info("Keychain", "setItem: success")
|
|
73
97
|
}
|
|
74
98
|
|
|
75
99
|
// MARK: - Get Item
|