// The breach check's one network act (SHAPES `vault.breach`, step 2): GET // each range address the core named, with the headers it named, and hand the // bodies back by prefix. The addresses are the range service's // (api.pwnedpasswords.com, `cotton_core::keys::health::RANGE_URL`), never a // Cotton server's, and carry no credential: only the five SHA-1 characters in // each address leave the phone, and only when the person taps Check. A range // that does not arrive is left out; the core's toast says so. #if os(iOS) import CottonKit import Foundation enum VaultBreachFetch { static func ranges(_ asked: [(prefix: String, address: String)], headers: [String: String]) async -> [String: String] { await withTaskGroup(of: (String, String?).self) { group in for (prefix, address) in asked { group.addTask { guard let url = URL(string: address), url.scheme == "https" else { return (prefix, nil) } var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 20) for (name, value) in headers { request.setValue(value, forHTTPHeaderField: name) } do { let (data, response) = try await URLSession.shared.data(for: request) guard (response as? HTTPURLResponse)?.statusCode == 200 else { return (prefix, nil) } return (prefix, String(data: data, encoding: .utf8)) } catch { Log.write("vault: a breach range did not arrive — \(error.localizedDescription)") return (prefix, nil) } } } var bodies: [String: String] = [:] for await (prefix, body) in group { if let body { bodies[prefix] = body } } return bodies } } } #endif