import Foundation import WebKit /// The web view signs in; the player rides on what that left behind. /// /// ipx knows who is asking by cookie: `ipx_session` from its own form, `CF_Authorization` from /// Cloudflare Access in front of the tunnel. That is why the player needs no API of its own. But /// AVPlayer and URLSession read `HTTPCookieStorage.shared`, and WKWebView keeps a store of its /// own, so this copies one into the other whenever the web view's changes. final class CookieBridge: NSObject, WKHTTPCookieStoreObserver { private let store: WKHTTPCookieStore init(store: WKHTTPCookieStore) { self.store = store super.init() store.add(self) sync() } func cookiesDidChange(in cookieStore: WKHTTPCookieStore) { sync() } /// All of them, replaced: a cookie the web view dropped (signing out) has to go here too, or /// the player would go on playing as someone who signed out. private func sync() { store.getAllCookies { all in let shared = HTTPCookieStorage.shared shared.cookies?.forEach(shared.deleteCookie) all.forEach(shared.setCookie) } } }