Two failures here are silent rather than loud, which is why they have gone unnoticed on iOS and would have been much harder to diagnose on a Mac. The old `enum Keychain` discarded all four OSStatus results. On Mac Catalyst a missing `keychain-access-groups` entitlement makes SecItem calls fail with errSecMissingEntitlement, and because nothing checked, that was indistinguishable from "no token stored" — the user would be signed out on every launch, with no error anywhere. KeychainStore now checks every status, distinguishes errSecItemNotFound from real failures, and sets kSecUseDataProtectionKeychain so macOS selects the modern entitlement-gated keychain instead of the legacy login keychain. Adding the entitlement moves the default access group to the first array entry, so that entry is deliberately the app's own group: existing tokens keep resolving with an unqualified query and nobody is signed out. loadToken() then migrates forward in three steps — shared group, own default group, and the pre-Keychain UserDefaults copy. If the entitlement is not provisioned yet, KeychainStore falls back to the default group rather than throwing. A hard failure would make the app unusable for everyone whose provisioning lags; the fallback asserts in DEBUG instead, so a misconfiguration is loud in development and survivable in production. Separately, logout() left widget-cache.json in the App Group container, so widgets kept rendering the signed-out user's events indefinitely. That is an existing iOS bug, and it would have leaked the same data to any other app reading the container. WidgetStore.clear() now removes both cache files and reloads the timelines. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
101 lines
3.6 KiB
Swift
101 lines
3.6 KiB
Swift
import SwiftUI
|
|
|
|
@main
|
|
struct CalendarrApp: App {
|
|
@State private var appState = AppState()
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
RootView()
|
|
.environment(appState)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Observable
|
|
class AppState {
|
|
var serverURL: String = ""
|
|
var authToken: String = ""
|
|
var username: String = ""
|
|
var isAdmin: Bool = false
|
|
|
|
var isConfigured: Bool { !serverURL.isEmpty }
|
|
var isLoggedIn: Bool { !authToken.isEmpty }
|
|
|
|
init() {
|
|
serverURL = UserDefaults.standard.string(forKey: "serverURL") ?? ""
|
|
authToken = Self.loadToken()
|
|
username = UserDefaults.standard.string(forKey: "username") ?? ""
|
|
isAdmin = UserDefaults.standard.bool(forKey: "isAdmin")
|
|
}
|
|
|
|
/// Find the stored token, migrating it forward from wherever an older build
|
|
/// left it. Runs on every launch but does real work only once.
|
|
///
|
|
/// Step 2 is the one that keeps existing users signed in. Before the
|
|
/// `keychain-access-groups` entitlement existed, items landed in the app's
|
|
/// own default group. Adding the entitlement makes the *first* array entry
|
|
/// the new default — and that entry is deliberately the app's own group, so
|
|
/// an unqualified query still resolves those items and we can copy them
|
|
/// across instead of stranding them.
|
|
private static func loadToken() -> String {
|
|
let key = "authToken"
|
|
|
|
// 1. Already in the shared group — the steady state.
|
|
if let token = try? KeychainStore.get(key), !token.isEmpty {
|
|
return token
|
|
}
|
|
|
|
// 2. In the app's own default group, written before the entitlement.
|
|
if let token = try? KeychainStore.get(key, accessGroup: nil), !token.isEmpty {
|
|
try? KeychainStore.set(token, for: key)
|
|
try? KeychainStore.set(nil, for: key, accessGroup: nil)
|
|
return token
|
|
}
|
|
|
|
// 3. In UserDefaults, written before secrets moved to the Keychain.
|
|
if let legacy = UserDefaults.standard.string(forKey: key), !legacy.isEmpty {
|
|
try? KeychainStore.set(legacy, for: key)
|
|
UserDefaults.standard.removeObject(forKey: key)
|
|
return legacy
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
func saveServer(url: String) {
|
|
serverURL = url.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
if serverURL.hasSuffix("/") { serverURL = String(serverURL.dropLast()) }
|
|
UserDefaults.standard.set(serverURL, forKey: "serverURL")
|
|
}
|
|
|
|
func saveLogin(token: String, user: String, admin: Bool) {
|
|
authToken = token
|
|
username = user
|
|
isAdmin = admin
|
|
try? KeychainStore.set(token, for: "authToken") // secret → Keychain, not UserDefaults
|
|
UserDefaults.standard.set(user, forKey: "username")
|
|
UserDefaults.standard.set(admin, forKey: "isAdmin")
|
|
}
|
|
|
|
func logout() {
|
|
authToken = ""
|
|
username = ""
|
|
isAdmin = false
|
|
try? KeychainStore.set(nil, for: "authToken")
|
|
try? KeychainStore.set(nil, for: "authToken", accessGroup: nil) // pre-entitlement copy
|
|
UserDefaults.standard.removeObject(forKey: "authToken") // pre-Keychain copy
|
|
UserDefaults.standard.removeObject(forKey: "username")
|
|
UserDefaults.standard.removeObject(forKey: "isAdmin")
|
|
// The shared container outlives the session, so it has to be cleared
|
|
// explicitly — otherwise widgets keep showing the signed-out user's data.
|
|
WidgetStore.clear()
|
|
}
|
|
|
|
func resetServer() {
|
|
logout()
|
|
serverURL = ""
|
|
UserDefaults.standard.removeObject(forKey: "serverURL")
|
|
}
|
|
}
|