|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public class SessionCookieStore { |
| 4 | + |
| 5 | + // MARK: - Private properties |
| 6 | + private static let bundleIdentifier = Bundle.main.bundleIdentifier ?? "com.mendix.app" |
| 7 | + private static let storageKey = bundleIdentifier + "sessionCookies" |
| 8 | + private static let queue = DispatchQueue(label: bundleIdentifier + ".session-cookie-store", qos: .utility) |
| 9 | + |
| 10 | + // MARK: - Public API |
| 11 | + public static func restore() { |
| 12 | + |
| 13 | + guard let cookies = get(key: storageKey) else { |
| 14 | + NSLog("SessionCookieStore: No cookies to restore") |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + let storage = HTTPCookieStorage.shared |
| 19 | + let existing = Set(storage.cookies ?? []) |
| 20 | + cookies.filter { !existing.contains($0) }.forEach { storage.setCookie($0) } |
| 21 | + |
| 22 | + clear() // Clear stored cookies after restoration to avoid any side effects |
| 23 | + } |
| 24 | + |
| 25 | + public static func persist() { |
| 26 | + queue.async { |
| 27 | + let sessionCookies = HTTPCookieStorage.shared.cookies?.filter { isSessionCookie($0) } ?? [] |
| 28 | + guard !sessionCookies.isEmpty else { |
| 29 | + clear() |
| 30 | + NSLog("SessionCookieStore: Clear existing session cookies from storage") |
| 31 | + return |
| 32 | + } |
| 33 | + set(key: storageKey, cookies: sessionCookies) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static func clear() { |
| 38 | + clear(key: storageKey) |
| 39 | + } |
| 40 | + |
| 41 | + // MARK: - Private API |
| 42 | + private static func isSessionCookie(_ cookie: HTTPCookie) -> Bool { |
| 43 | + return cookie.expiresDate == nil |
| 44 | + } |
| 45 | + |
| 46 | + private static func set(key: String, cookies: [HTTPCookie]) { |
| 47 | + do { |
| 48 | + let data = try NSKeyedArchiver.archivedData(withRootObject: cookies, requiringSecureCoding: false) |
| 49 | + let storeQuery = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: key, kSecValueData: data] as CFDictionary |
| 50 | + SecItemDelete(storeQuery) |
| 51 | + let status = SecItemAdd(storeQuery, nil) |
| 52 | + if status != noErr { |
| 53 | + NSLog("SessionCookieStore: Failed to persist session cookies with status: \(status)") |
| 54 | + } |
| 55 | + } catch { |
| 56 | + NSLog("SessionCookieStore: Failed to persist session cookies: \(error.localizedDescription)") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static func get(key: String) -> [HTTPCookie]? { |
| 61 | + do { |
| 62 | + let query: [CFString: Any] = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: key, kSecReturnData: true] |
| 63 | + var item: CFTypeRef? |
| 64 | + let status = SecItemCopyMatching(query as CFDictionary, &item) |
| 65 | + if status == errSecSuccess, let data = item as? Data { |
| 66 | + let cookies = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, HTTPCookie.self], from: data) as? [HTTPCookie] |
| 67 | + return cookies |
| 68 | + } else { |
| 69 | + NSLog("SessionCookieStore: No session cookies found with status: \(status)") |
| 70 | + return nil |
| 71 | + } |
| 72 | + } catch { |
| 73 | + NSLog("SessionCookieStore: Failed to retrieve session cookies: \(error.localizedDescription)") |
| 74 | + return nil |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static func clear(key: String) { |
| 79 | + let query = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: key, kSecReturnData: true] as CFDictionary |
| 80 | + let status = SecItemDelete(query) |
| 81 | + if status != errSecSuccess { |
| 82 | + NSLog("SessionCookieStore: Failed to clear cookies with status: \(status)") |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments