SPB Git

Repository Public

SessionKeychain.swift 3.2 KB 62 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/SessionKeychain.swift3// Description: Where the session token lives on the Mac: the data-protection keychain (access group of the app) when the build is entitled to it, with a migration from the legacy login-keychain item of SP Notes 1.x that can never sign the owner out.4import Foundation5import Security6import SPNotesAPI78/// Reads the new store first, then the old one; writes to the new store and removes the old item only once the new9/// one has been read back. Every failure of the new store falls back on the old one, so the token of SP Notes 1.x10/// stays usable whatever happens (unsigned build, missing entitlement, keychain error).11nonisolated struct MigratingTokenStore: SessionTokenStore {12    let primary: any SessionTokenStore13    let legacy: any SessionTokenStore1415    func load() -> String? {16        if let token = primary.load() { return token }17        guard let old = legacy.load() else { return nil }18        // first launch of 2.0: copy, verify, and only then remove the 1.x item19        if (try? primary.save(old)) != nil, primary.load() == old { try? legacy.clear() }20        return old21    }2223    func save(_ token: String) throws {24        do {25            try primary.save(token)26            guard primary.load() == token else { throw KeychainError(status: errSecItemNotFound) }27            try? legacy.clear()28        } catch {29            // the new keychain refused: the session is kept where 1.x kept it, and the stale new item is removed30            try legacy.save(token)31            try? primary.clear()32        }33    }3435    func clear() throws {36        var failure: Error?37        do { try primary.clear() } catch { failure = error }38        do { try legacy.clear() } catch { failure = failure ?? error }39        // signing out succeeded when no store still returns a token40        if primary.load() != nil || legacy.load() != nil, let failure { throw failure }41    }42}4344enum SessionKeychain {45    static var service: String { (Bundle.main.bundleIdentifier ?? "ai.spboucher.spnotes") + ".session" }4647    /// The keychain access group this process is entitled to (signed Release build), nil otherwise (Debug, tests).48    nonisolated static func entitledAccessGroup() -> String? {49        guard let task = SecTaskCreateFromSelf(nil), let value = SecTaskCopyValueForEntitlement(task, "keychain-access-groups" as CFString, nil) else { return nil }50        return (value as? [String])?.first51    }5253    /// 1.x kept the token in the legacy file-based login keychain. 2.0 keeps it in the data-protection keychain (the54    /// accessibility class is honoured, and a future extension can share it through the access group) when the build55    /// is entitled; otherwise exactly as 1.x did.56    static func store(configuration: APIConfiguration) -> any SessionTokenStore {57        let legacy = KeychainTokenStore(configuration: configuration, service: service, useDataProtectionKeychain: false)58        guard let group = entitledAccessGroup() else { return legacy }59        return MigratingTokenStore(primary: KeychainTokenStore(configuration: configuration, service: service, accessGroup: group, useDataProtectionKeychain: true), legacy: legacy)60    }61}62
Files main
Menu

Theme