Skip to content

Commit 4619d54

Browse files
committed
added print to sessionRegistiry purge function
1 parent cb199bd commit 4619d54

File tree

3 files changed

+50
-58
lines changed

3 files changed

+50
-58
lines changed

Sources/TurnkeySwift/Internal/Storage/Keys/PendingKeysStore.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,15 @@ enum PendingKeysStore {
2727
q.sync { (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:] }
2828
}
2929

30-
static func purgeExpiredSessions() {
31-
do {
32-
for sessionKey in try all() {
30+
static func purge(ttlHours: Double = 24) {
31+
let cutoff = Date().addingTimeInterval(-ttlHours * 3600)
32+
for (pub, createdAt) in all() where createdAt < cutoff {
3333
do {
34-
guard let sess = try JwtSessionStore.load(key: sessionKey) else {
35-
try remove(sessionKey) // dangling index
36-
continue
37-
}
38-
39-
if Date(timeIntervalSince1970: sess.exp) <= Date() {
40-
JwtSessionStore.delete(key: sessionKey)
41-
try KeyPairStore.delete(for: sess.publicKey)
42-
try remove(sessionKey)
43-
}
34+
try SecureStore.delete(service: pub, account: secureAccount)
35+
try remove(pub)
4436
} catch {
45-
print("SessionRegistry purge error for \(sessionKey): \(error)")
37+
print("PendingKeysStore purge error for \(pub): \(error)")
4638
}
4739
}
48-
} catch {
49-
print("SessionRegistry purge error (enumerating keys): \(error)")
5040
}
5141
}
52-
53-
}

Sources/TurnkeySwift/Internal/Storage/Sessions/SessionRegistryStore.swift

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,50 @@ import Foundation
33
/// Stores a list of all active session keys.
44
/// Used to track and manage multiple JWT-backed sessions, and to purge expired ones.
55
enum SessionRegistryStore: CollectionStore {
6-
7-
private static let storeKey = Constants.Storage.sessionRegistryKey
8-
private static let secureAccount = Constants.Storage.secureAccount
9-
private static let q = DispatchQueue(label: "sessionKeys", attributes: .concurrent)
10-
11-
static func add(_ sessionKey: String) throws {
12-
try q.sync(flags: .barrier) {
13-
var list: [String] = try LocalStore.get(storeKey) ?? []
14-
guard !list.contains(sessionKey) else { return }
15-
list.append(sessionKey)
16-
try LocalStore.set(list, for: storeKey)
6+
7+
private static let storeKey = Constants.Storage.sessionRegistryKey
8+
private static let secureAccount = Constants.Storage.secureAccount
9+
private static let q = DispatchQueue(label: "sessionKeys", attributes: .concurrent)
10+
11+
static func add(_ sessionKey: String) throws {
12+
try q.sync(flags: .barrier) {
13+
var list: [String] = try LocalStore.get(storeKey) ?? []
14+
guard !list.contains(sessionKey) else { return }
15+
list.append(sessionKey)
16+
try LocalStore.set(list, for: storeKey)
17+
}
1718
}
18-
}
19-
20-
static func remove(_ sessionKey: String) throws {
21-
try q.sync(flags: .barrier) {
22-
var list: [String] = try LocalStore.get(storeKey) ?? []
23-
list.removeAll { $0 == sessionKey }
24-
try LocalStore.set(list, for: storeKey)
19+
20+
static func remove(_ sessionKey: String) throws {
21+
try q.sync(flags: .barrier) {
22+
var list: [String] = try LocalStore.get(storeKey) ?? []
23+
list.removeAll { $0 == sessionKey }
24+
try LocalStore.set(list, for: storeKey)
25+
}
2526
}
26-
}
27-
28-
static func all() throws -> [String] {
29-
try q.sync {
30-
try LocalStore.get(storeKey) ?? []
27+
28+
static func all() throws -> [String] {
29+
try q.sync {
30+
try LocalStore.get(storeKey) ?? []
31+
}
3132
}
32-
}
33-
34-
static func purgeExpiredSessions() throws {
35-
for sessionKey in try all() {
36-
guard let sess = try JwtSessionStore.load(key: sessionKey) else {
37-
try remove(sessionKey) // dangling index
38-
continue
39-
}
40-
41-
if Date(timeIntervalSince1970: sess.exp) <= Date() {
42-
JwtSessionStore.delete(key: sessionKey)
43-
try KeyPairStore.delete(for: sess.publicKey)
44-
try remove(sessionKey)
45-
}
33+
34+
static func purgeExpiredSessions() {
35+
do {
36+
let sessionKeys = try all()
37+
for sessionKey in sessionKeys {
38+
if let sess = try JwtSessionStore.load(key: sessionKey) {
39+
if Date(timeIntervalSince1970: sess.exp) <= Date() {
40+
JwtSessionStore.delete(key: sessionKey)
41+
try KeyPairStore.delete(for: sess.publicKey)
42+
try remove(sessionKey)
43+
}
44+
} else {
45+
try remove(sessionKey)
46+
}
47+
}
48+
} catch {
49+
print("purgeExpiredSessions error: \(error)")
50+
}
4651
}
47-
}
4852
}

Sources/TurnkeySwift/Public/TurnkeyContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public final class TurnkeyContext: ObservableObject {
2828

2929
// clean up expired sessions and pending keys
3030
PendingKeysStore.purge(ttlHours: 2)
31-
try? SessionRegistryStore.purgeExpiredSessions()
31+
SessionRegistryStore.purgeExpiredSessions()
3232

3333
// restore session and timers after launch
3434
Task { [weak self] in
@@ -41,7 +41,7 @@ public final class TurnkeyContext: ObservableObject {
4141
Task.detached {
4242
for await _ in NotificationCenter.default.notifications(named: note) {
4343
PendingKeysStore.purge(ttlHours: 1)
44-
try? SessionRegistryStore.purgeExpiredSessions()
44+
SessionRegistryStore.purgeExpiredSessions()
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)