@@ -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.
55enum 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}
0 commit comments