Posts

Post not yet marked as solved
4 Replies
905 Views
To lay the scene, here's what the creation, copy and updating looks like:let attrs: [String: Any] = [kSecClass as String: kSecClassInternetPassword kSecAttrServer as String: self.hostname, kSecAttrPort as String: self.port, kSecAttrAccount as String: self.username, kSecValueData as String: self.password.data(using: .utf8)!, kSecAttrPath as String: self.database, kSecAttrLabel as String: "Some label" kSecReturnPersistentRef as String: true] var result: CFTypeRef? let status = SecItemAdd(attrs as CFDictionary, &result) guard status == errSecSuccess else { debugPrint("SecItemAdd returned \(status)") return } self.keychainRef = result as? Datavar result: CFTypeRef? let status = SecItemCopyMatching([ kSecValuePersistentRef as String: keychainRef, kSecMatchLimit as String: kSecMatchLimitOne, kSecReturnAttributes as String: true, kSecReturnData as String: true ] as CFDictionary, &result) guard status == errSecSuccess else { debugPrint("SecItemCopyMatching returned \(status)") // ... return } // ...let attrs: [String: Any] = [kSecAttrServer as String: self.hostname, kSecAttrPort as String: self.port, kSecAttrAccount as String: self.username, kSecValueData as String: self.password.data(using: .utf8)!, kSecAttrPath as String: self.database] let status = SecItemUpdate([ kSecValuePersistentRef as String: keychainRef ] as CFDictionary, attrs as CFDictionary) guard status == errSecSuccess else { debugPrint("SecItemUpdate returned \(status)") return }This is being run on macOS.Now the scenario:If the password field (kSecValueData) alone is updated, all is fine. I can continue to use SecItemCopyMatching without issue.However if I was to update another attribute, say the username (kSecAttrAccount) then it seems that the persistent reference is no longer valid as SecItemCopyMatching now returns errSecItemNotFound. But there seems to be no way to get a new reference (if indeed that is the issue) from SecItemUpdate.Is the recommendation to just delete and add every time?
Posted
by Bo98.
Last updated
.