SPB Git

Repository Public

AppEnvironment.swift 4.6 KB 121 lines swift

1// SP Vault — where the app and its AutoFill extension find the same things: the server, the SQLite store in the2// app-group container and the device keys in the shared Keychain access group.3//4// Production (Release): https://vault.spboucher.ai, Secure Enclave keys behind user presence (biometrics or passcode).5// Development (Debug, SPV_DEV): a local test server and file-based software keys — see DevSupport.swift.6import Foundation7import SharedVaultKit89enum AppEnvironment {10    static let teamID = "3YM54G49SN"1112    #if os(macOS)13    static let platformName = "macOS"14    #else15    static let platformName = "iOS"16    #endif1718    // ── identifiers ──19    #if SPV_DEV20    static let isDev = true21    #else22    static let isDev = false23    #endif2425    static var appGroup: String {26        #if os(macOS)27        return "\(teamID).ai.spboucher.spvault"28        #else29        return isDev ? "group.ai.spboucher.spvault.ios.dev" : "group.ai.spboucher.spvault.ios"30        #endif31    }3233    static var keychainGroup: String {34        #if os(macOS)35        return "\(teamID).ai.spboucher.spvault.shared"36        #else37        return "\(teamID).ai.spboucher.spvault.ios\(isDev ? ".dev" : "").shared"38        #endif39    }4041    // ── server ──42    static var baseURL: URL {43        #if SPV_DEV44        return DevSupport.serverURL45        #else46        return URLSessionTransport.productionURL47        #endif48    }4950    static var relyingParty: String { baseURL.host ?? "vault.spboucher.ai" }51    /// Origin used in links the app builds (relay links, invitation links).52    static var origin: String {53        var s = baseURL.absoluteString54        while s.hasSuffix("/") { s.removeLast() }55        return s56    }5758    // ── storage ──59    /// Directory shared by the app and the extension. Production: the app-group container.60    static func sharedDirectory() throws -> URL {61        #if SPV_DEV62        return try DevSupport.sharedDirectory()63        #else64        guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {65            throw CocoaError(.fileNoSuchFile, userInfo: [NSLocalizedDescriptionKey: "The shared container of SP Vault is not available. Reinstall the app."])66        }67        let dir = url.appendingPathComponent("Vault", isDirectory: true)68        try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)69        return dir70        #endif71    }7273    static func storePath() throws -> String { try sharedDirectory().appendingPathComponent("vault.sqlite").path }74    static func pendingItemsURL() throws -> URL { try sharedDirectory().appendingPathComponent("pending-items.json") }7576    static func makeStore(readOnly: Bool = false) throws -> SQLiteStore { try SQLiteStore(path: try storePath(), readOnly: readOnly) }7778    /// The device key store. Production: Secure Enclave keys that need biometrics or the device passcode, in the79    /// Keychain access group shared with the AutoFill extension.80    static func makeKeyStore() throws -> any DeviceKeyStore {81        #if SPV_DEV82        return try DevSupport.makeKeyStore()83        #else84        return SecureEnclaveKeyStore(accessGroup: keychainGroup, policy: .userPresence)85        #endif86    }8788    /// Label of this device in the device list (`[A-Za-z0-9 ·.,()_-]{1,48}`, never starting with "Web").89    static var deviceLabel: String {90        #if os(macOS)91        var name = Host.current().localizedName ?? "Mac"92        #else93        var name = deviceModelName94        #endif95        name = String(name.unicodeScalars.filter { c in96            (c.value < 128 && (CharacterSet.alphanumerics.contains(c) || " .,()_-".unicodeScalars.contains(c))) || c == "·"97        })98        name = name.trimmingCharacters(in: .whitespaces)99        #if os(macOS)100        if name.isEmpty { name = "Mac" }101        let label = "\(name) · macOS"102        #else103        if name.isEmpty { name = "iPhone" }104        let label = "\(name) · iOS"105        #endif106        let cut = String(label.prefix(48))107        return cut.lowercased().hasPrefix("web") ? "Apple \(cut.prefix(42))" : cut108    }109110    #if !os(macOS)111    /// "iPhone" or "iPad", from the hardware identifier (no UIKit: also used by the extension, off the main actor).112    private static var deviceModelName: String {113        var info = utsname()114        uname(&info)115        var machine = withUnsafeBytes(of: &info.machine) { String(decoding: $0.prefix { $0 != 0 }, as: UTF8.self) }116        if let sim = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] { machine = sim }117        return machine.hasPrefix("iPad") ? "iPad" : "iPhone"118    }119    #endif120}121
Files main
Menu

Theme