Skip to content

Commit

Permalink
Update data if data already exists in Persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
mohannad-hassan committed Jan 17, 2025
1 parent 644cef1 commit 7edc942
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Data/AuthenticationClient/Sources/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,32 @@ final class KeychainPersistence: Persistence {
kSecValueData as String: state,
]
let status = SecItemAdd(addquery as CFDictionary, nil)
if status != errSecSuccess {
if status == errSecDuplicateItem {
logger.info("State already exists, updating")
try update(state: state)
} else if status != errSecSuccess {
logger.error("Failed to persist state -- \(status) status")
throw PersistenceError.persistenceFailed
}
logger.info("State persisted successfully")
}

private func update(state: Data) throws {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey,
]
let attributes: [String: Any] = [
kSecValueData as String: state,
]
let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
if status != errSecSuccess {
logger.error("Failed to update state -- \(status) status")
throw PersistenceError.persistenceFailed
}
logger.info("State updated")
}

func retrieve() throws -> Data? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
Expand Down

0 comments on commit 7edc942

Please sign in to comment.