Skip to content

Commit 065540d

Browse files
authored
Merge pull request #179 from mattrubin/hotfix
Ignore un-deserializable tokens in allPersistentTokens()
2 parents 2c091c8 + c14a295 commit 065540d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Sources/Keychain.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public final class Keychain {
4747
///
4848
/// - throws: A `Keychain.Error` if an error occurred.
4949
public func allPersistentTokens() throws -> Set<PersistentToken> {
50-
return Set(try allKeychainItems().map(PersistentToken.init(keychainDictionary:)))
50+
let allItems = try allKeychainItems()
51+
// This code intentionally ignores items which fail deserialization, instead opting to return as many readable
52+
// tokens as possible.
53+
// TODO: Restore deserialization error handling, in a way that provides info on the failure reason and allows
54+
// the caller to choose whether to fail completely or recover some data.
55+
return Set(allItems.flatMap({ try? PersistentToken.init(keychainDictionary:$0) }))
5156
}
5257

5358
// MARK: Write

Tests/KeychainTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ class KeychainTests: XCTestCase {
231231
let persistentRef = try addKeychainItem(withAttributes: keychainAttributes)
232232

233233
XCTAssertThrowsError(try keychain.persistentToken(withIdentifier: persistentRef))
234-
XCTAssertThrowsError(try keychain.allPersistentTokens())
234+
// TODO: Restore deserialization error handling in allPersistentTokens()
235+
// XCTAssertThrowsError(try keychain.allPersistentTokens())
235236

236237
XCTAssertNoThrow(try deleteKeychainItem(forPersistentRef: persistentRef),
237238
"Failed to delete the test token from the keychain. This may cause future test runs to fail.")
@@ -247,7 +248,8 @@ class KeychainTests: XCTestCase {
247248
let persistentRef = try addKeychainItem(withAttributes: keychainAttributes)
248249

249250
XCTAssertThrowsError(try keychain.persistentToken(withIdentifier: persistentRef))
250-
XCTAssertThrowsError(try keychain.allPersistentTokens())
251+
// TODO: Restore deserialization error handling in allPersistentTokens()
252+
// XCTAssertThrowsError(try keychain.allPersistentTokens())
251253

252254
XCTAssertNoThrow(try deleteKeychainItem(forPersistentRef: persistentRef),
253255
"Failed to delete the test token from the keychain. This may cause future test runs to fail.")
@@ -264,7 +266,8 @@ class KeychainTests: XCTestCase {
264266
let persistentRef = try addKeychainItem(withAttributes: keychainAttributes)
265267

266268
XCTAssertThrowsError(try keychain.persistentToken(withIdentifier: persistentRef))
267-
XCTAssertThrowsError(try keychain.allPersistentTokens())
269+
// TODO: Restore deserialization error handling in allPersistentTokens()
270+
// XCTAssertThrowsError(try keychain.allPersistentTokens())
268271

269272
XCTAssertNoThrow(try deleteKeychainItem(forPersistentRef: persistentRef),
270273
"Failed to delete the test token from the keychain. This may cause future test runs to fail.")
@@ -281,7 +284,8 @@ class KeychainTests: XCTestCase {
281284
let persistentRef = try addKeychainItem(withAttributes: keychainAttributes)
282285

283286
XCTAssertThrowsError(try keychain.persistentToken(withIdentifier: persistentRef))
284-
XCTAssertThrowsError(try keychain.allPersistentTokens())
287+
// TODO: Restore deserialization error handling in allPersistentTokens()
288+
// XCTAssertThrowsError(try keychain.allPersistentTokens())
285289

286290
XCTAssertNoThrow(try deleteKeychainItem(forPersistentRef: persistentRef),
287291
"Failed to delete the test token from the keychain. This may cause future test runs to fail.")

0 commit comments

Comments
 (0)