Repository Public
1import Foundation2import MCCCore3import UIKit45/// Launch configuration. Release builds always talk to production with system passkeys and the Keychain; the6/// overrides below exist in Debug builds only (local backend, software authenticator, UI tests).7struct AppEnvironment: Sendable {8 enum SecureStoreKind: String, Sendable { case keychain, file, memory }910 var origin: URL = CoreConfiguration.productionOrigin11 var softwarePasskeys = false12 var secureStore: SecureStoreKind = .keychain13 var language: AppLanguage?14 var uiTesting = false15 var resetState = false16 /// Setup link handed at launch (Debug, UI tests): goes through the same handler as a universal link.17 var launchURL: URL?18 var stateDirectory: URL?19 var appearance: String?2021 var isProduction: Bool { origin == CoreConfiguration.productionOrigin }2223 /// Scheme registered by the Debug build only; `mcc-ios-debug://setup#<token>` simulates the universal link.24 static let debugScheme = "mcc-ios-debug"2526 static func resolve(_ env: [String: String] = ProcessInfo.processInfo.environment) -> AppEnvironment {27 var out = AppEnvironment()28 #if DEBUG29 if let s = env["MCC_BASE_URL"], let url = URL(string: s), url.host != nil { out.origin = url }30 out.softwarePasskeys = env["MCC_PASSKEY"] == "software"31 if let k = env["MCC_SECURE_STORE"].flatMap(SecureStoreKind.init(rawValue:)) { out.secureStore = k }32 out.language = env["MCC_LANGUAGE"].flatMap(AppLanguage.init(rawValue:))33 out.uiTesting = env["MCC_UITEST"] == "1"34 out.resetState = env["MCC_RESET"] == "1"35 out.launchURL = env["MCC_OPEN_URL"].flatMap { $0.isEmpty ? nil : URL(string: $0) }36 out.appearance = env["MCC_APPEARANCE"]37 if let dir = env["MCC_STATE_DIR"], !dir.isEmpty { out.stateDirectory = URL(fileURLWithPath: dir, isDirectory: true) }38 #endif39 return out40 }4142 /// Where this build keeps its local, non-secret state (offline cache, Debug-only test material).43 var supportDirectory: URL {44 if let stateDirectory { return stateDirectory }45 let root = (try? FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true))46 ?? FileManager.default.temporaryDirectory47 return root.appendingPathComponent("MCC", isDirectory: true)48 }4950 /// DEBUG, UI tests: start from a clean local state (files and preferences of this build only).51 func resetLocalStateIfRequested() {52 #if DEBUG53 guard resetState else { return }54 try? FileManager.default.removeItem(at: supportDirectory)55 if let id = Bundle.main.bundleIdentifier { UserDefaults.standard.removePersistentDomain(forName: id) }56 #endif57 }5859 private var namespace: String { (origin.host() ?? "server") + (origin.port.map { "-\($0)" } ?? "") }6061 @MainActor62 func makeConfiguration() -> CoreConfiguration {63 let support = supportDirectory64 let passkeys: any PasskeyProvider65 let store: any SecureStore66 #if DEBUG67 if softwarePasskeys {68 var o = "\(origin.scheme ?? "http")://\(origin.host() ?? "localhost")"69 if let p = origin.port { o += ":\(p)" }70 passkeys = SoftwareAuthenticator(origin: o, storageURL: support.appendingPathComponent("debug-passkeys.json"))71 } else {72 passkeys = PlatformPasskeyProvider(relyingPartyID: isProduction ? CoreConfiguration.relyingPartyID : (origin.host() ?? "localhost")) { Self.keyWindow() }73 }74 switch secureStore {75 case .file: store = DebugFileSecureStore(directory: support.appendingPathComponent("debug-secrets", isDirectory: true))76 case .memory: store = InMemorySecureStore()77 case .keychain: store = Self.keychain()78 }79 #else80 passkeys = PlatformPasskeyProvider(relyingPartyID: CoreConfiguration.relyingPartyID) { Self.keyWindow() }81 store = Self.keychain()82 #endif83 return CoreConfiguration(origin: origin, secureStore: store, passkeys: passkeys,84 cacheDirectory: support.appendingPathComponent("OfflineCache", isDirectory: true).appendingPathComponent(namespace, isDirectory: true))85 }8687 /// Generic passwords, `AfterFirstUnlockThisDeviceOnly`, never synchronised (see `KeychainSecureStore`). No88 /// explicit access group: on iOS an item lands in the app's first keychain access group, which is the one89 /// declared in the entitlements (`<team>.<bundle id>`), private to this app.90 private static func keychain() -> KeychainSecureStore {91 KeychainSecureStore(service: "ai.spboucher.clustr.session", accessGroup: nil, dataProtection: true)92 }9394 /// The window that hosts the passkey sheet: the key window of the foreground scene.95 @MainActor96 static func keyWindow() -> UIWindow? {97 let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }98 let scene = scenes.first { $0.activationState == .foregroundActive } ?? scenes.first99 return scene?.keyWindow ?? scene?.windows.first100 }101102 /// APNs environment of this build: development for Debug (Xcode-installed), production for TestFlight / App Store.103 static var pushIsProduction: Bool {104 #if DEBUG105 false106 #else107 true108 #endif109 }110}111