|
1 | 1 | import Foundation |
2 | 2 |
|
3 | | -/// Tracks generated but unused public keys along with their creation timestamps. |
| 3 | +/// Tracks generated but unused public keys along with their expiration timestamps. |
4 | 4 | /// This is used to clean up stale key material that was never used to establish a session. |
5 | 5 | enum PendingKeysStore { |
6 | | - private static let storeKey = Constants.Storage.pendingKeysStoreKey |
7 | | - private static let secureAccount = Constants.Storage.secureAccount |
8 | | - private static let q = DispatchQueue(label: "pendingKeys", attributes: .concurrent) |
9 | | - |
10 | | - static func add(_ pub: String) throws { |
11 | | - try q.sync(flags: .barrier) { |
12 | | - var dict = (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:] |
13 | | - dict[pub] = Date() |
14 | | - try LocalStore.set(dict, for: storeKey) |
| 6 | + private static let storeKey = Constants.Storage.pendingKeysStoreKey |
| 7 | + private static let secureAccount = Constants.Storage.secureAccount |
| 8 | + private static let q = DispatchQueue(label: "pendingKeys", attributes: .concurrent) |
| 9 | + |
| 10 | + static func add(_ pub: String, ttlHours: Double = 1) throws { |
| 11 | + try q.sync(flags: .barrier) { |
| 12 | + var dict = (try? LocalStore.get(storeKey) as [String: TimeInterval]?) ?? [:] |
| 13 | + let expiry = Date().addingTimeInterval(ttlHours * 3600).timeIntervalSince1970 |
| 14 | + dict[pub] = expiry |
| 15 | + try LocalStore.set(dict, for: storeKey) |
| 16 | + } |
15 | 17 | } |
16 | | - } |
17 | | - |
18 | | - static func remove(_ pub: String) throws { |
19 | | - try q.sync(flags: .barrier) { |
20 | | - var dict = (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:] |
21 | | - dict.removeValue(forKey: pub) |
22 | | - try LocalStore.set(dict, for: storeKey) |
| 18 | + |
| 19 | + static func remove(_ pub: String) throws { |
| 20 | + try q.sync(flags: .barrier) { |
| 21 | + var dict = (try? LocalStore.get(storeKey) as [String: TimeInterval]?) ?? [:] |
| 22 | + dict.removeValue(forKey: pub) |
| 23 | + try LocalStore.set(dict, for: storeKey) |
| 24 | + } |
23 | 25 | } |
24 | | - } |
25 | | - |
26 | | - static func all() -> [String: Date] { |
27 | | - q.sync { (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:] } |
28 | | - } |
29 | | - |
30 | | - static func purge(ttlHours: Double = 24) { |
31 | | - let cutoff = Date().addingTimeInterval(-ttlHours * 3600) |
32 | | - for (pub, createdAt) in all() where createdAt < cutoff { |
33 | | - do { |
34 | | - try SecureStore.delete(service: pub, account: secureAccount) |
35 | | - try remove(pub) |
36 | | - } catch { |
37 | | - print("PendingKeysStore purge error for \(pub): \(error)") |
38 | | - } |
| 26 | + |
| 27 | + static func all() -> [String: TimeInterval] { |
| 28 | + q.sync { (try? LocalStore.get(storeKey) as [String: TimeInterval]?) ?? [:] } |
| 29 | + } |
| 30 | + |
| 31 | + static func purge() { |
| 32 | + let now = Date().timeIntervalSince1970 |
| 33 | + for (pub, expiry) in all() where expiry < now { |
| 34 | + do { |
| 35 | + try SecureStore.delete(service: pub, account: secureAccount) |
| 36 | + try remove(pub) |
| 37 | + } catch { |
| 38 | + print("PendingKeysStore purge error for \(pub): \(error)") |
| 39 | + } |
| 40 | + } |
39 | 41 | } |
40 | | - } |
41 | 42 | } |
0 commit comments