Handle keychain query errors in keychain helper lookup (#211)

This allows us to handle errors from lookup with only the keychain
helper error types which simplifies the logic

related https://github.com/apple/container/pull/331

Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
This commit is contained in:
Kathryn Baldauf
2025-07-14 11:40:30 -07:00
committed by GitHub
parent 197e9b63a9
commit ab559752b8
@@ -29,17 +29,25 @@ public struct KeychainHelper: Sendable {
public func lookup(domain: String) throws -> Authentication {
let kq = KeychainQuery()
guard try kq.exists(id: self.id, host: domain) else {
throw Self.Error.keyNotFound
do {
guard try kq.exists(id: self.id, host: domain) else {
throw Self.Error.keyNotFound
}
guard let fetched = try kq.get(id: self.id, host: domain) else {
throw Self.Error.keyNotFound
}
return BasicAuthentication(
username: fetched.account,
password: fetched.data
)
} catch let err as KeychainQuery.Error {
switch err {
case .keyNotPresent(_):
throw Self.Error.keyNotFound
default:
throw Self.Error.queryError("query failure: \(String(describing: err))")
}
}
guard let fetched = try kq.get(id: self.id, host: domain) else {
throw Self.Error.keyNotFound
}
return BasicAuthentication(
username: fetched.account,
password: fetched.data
)
}
/// Delete authorization data for a given domain from the keychain.
@@ -93,6 +101,7 @@ extension KeychainHelper {
public enum Error: Swift.Error {
case keyNotFound
case invalidInput
case queryError(String)
}
}
#endif