mirror of
https://github.com/yattee/yattee.git
synced 2026-10-27 15:07:18 +00:00
Yattee 2 keys credentials by instance UUID with no notion of which Apple account they belong to, and reads them back with SynchronizableAny. Apple documents that Keychain data may stay on device when a user signs out and chooses to keep it, so whoever signs in next would be served the previous user's credential - guaranteed for migrated accounts, since local instances survive the switch and the migrated account UUID is deterministically the instance UUID. Credentials are now keyed "<identityScope>|<accountID>", where the scope is the CloudKit user record (identical across a user's devices, so iCloud Keychain sync still works). Usernames get the same scope and live beside the credential, keeping login identifiers out of synced metadata. The legacy unscoped item is never adopted automatically. A strict kSecAttrSynchronizable == true query was the candidate discriminator for "this secret belongs to the current Apple account", but measured on iPadOS 27.0 a retained item keeps the attribute and still matches that query with no Apple account signed in at all - the flag records what was once requested, not live iCloud Keychain membership. Reads therefore never fall back to the legacy key; adoption is an explicit user action that copies the item, verifies the scoped read-back, then records a marker bound to the identity and a salted fingerprint of the secret. The legacy item is retained, because deleting a synchronizable item propagates and would log out devices still on the previous release mid-rollout. Deleting it later needs all three of: a marker on this device, matching identity, and the item still holding the adopted secret. The instance-keyed path is untouched, so no shipping call site changes behavior. BasicAuthCredentialsManager stays instance-keyed - proxy auth belongs to the instance, not to a user account. While the scope is unresolved (first launch under a new Apple account, CloudKit unreachable) reads report "can't tell yet" via isScopeResolved rather than "signed out", and writes are refused rather than guessing a scope. Login state is cached per scope so one identity's cache can never answer for another. Also adds the debug-only harness the measurement was taken with, under Advanced > Developer > Experiments.
113 lines
4.1 KiB
Swift
113 lines
4.1 KiB
Swift
//
|
|
// KeychainCredentialStore.swift
|
|
// Yattee
|
|
//
|
|
// Keychain read/write shared by the Invidious and Piped credential managers.
|
|
//
|
|
|
|
import Foundation
|
|
import Security
|
|
|
|
/// Generic-password Keychain access for one service name.
|
|
///
|
|
/// Both credential managers had a byte-for-byte copy of this logic keyed by
|
|
/// instance UUID; the account revamp adds identity scoping and username
|
|
/// entries on top, so it lives in one place now.
|
|
///
|
|
/// **tvOS:** `kSecAttrSynchronizable` is accepted but app Keychain items are
|
|
/// never synchronized through iCloud there. Writes still set the attribute (so
|
|
/// an item created on Apple TV syncs if Apple ever enables it), but an account
|
|
/// synced from another device stays credential-less on tvOS until the user
|
|
/// signs in locally — that is permanent, not a pending sync.
|
|
struct KeychainCredentialStore: Sendable {
|
|
let service: String
|
|
|
|
// MARK: - Key construction
|
|
|
|
/// Identity-scoped credential key: `"<scope>|<accountID>"`.
|
|
static func credentialKey(scope: String, accountID: UUID) -> String {
|
|
"\(scope)|\(accountID.uuidString)"
|
|
}
|
|
|
|
/// Identity-scoped username key: `"<scope>|<accountID>#username"`.
|
|
///
|
|
/// Scoped exactly like the credential, so a username retained from one
|
|
/// Apple account never surfaces under another.
|
|
static func usernameKey(scope: String, accountID: UUID) -> String {
|
|
"\(credentialKey(scope: scope, accountID: accountID))#username"
|
|
}
|
|
|
|
/// The pre-revamp key: a bare instance UUID, with no identity scope.
|
|
///
|
|
/// Only the adoption path may read this, and only when the user explicitly
|
|
/// asks for it.
|
|
static func legacyKey(instanceID: UUID) -> String {
|
|
instanceID.uuidString
|
|
}
|
|
|
|
// MARK: - Access
|
|
|
|
/// Reads a value, matching both synchronizable forms.
|
|
///
|
|
/// `SynchronizableAny` is safe for scoped keys — the scope, not the sync
|
|
/// flag, is what provides identity isolation.
|
|
func value(forKey key: String) -> String? {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: key,
|
|
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny,
|
|
kSecReturnData as String: true,
|
|
kSecMatchLimit as String: kSecMatchLimitOne
|
|
]
|
|
|
|
var result: AnyObject?
|
|
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
|
|
|
guard status == errSecSuccess,
|
|
let data = result as? Data,
|
|
let value = String(data: data, encoding: .utf8)
|
|
else { return nil }
|
|
|
|
return value
|
|
}
|
|
|
|
/// Writes a value, replacing any existing item in either synchronizable form.
|
|
@discardableResult
|
|
func set(_ value: String, forKey key: String, synchronizable: Bool) -> OSStatus {
|
|
delete(key: key)
|
|
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: key,
|
|
kSecAttrSynchronizable as String: synchronizable,
|
|
kSecValueData as String: Data(value.utf8)
|
|
]
|
|
|
|
return SecItemAdd(query as CFDictionary, nil)
|
|
}
|
|
|
|
/// Deletes a value in both synchronizable forms.
|
|
///
|
|
/// Deleting a synchronizable item propagates to every device signed in to
|
|
/// the same Apple account — intended for a real sign-out, which is why the
|
|
/// legacy item is never deleted as a side effect of adoption.
|
|
@discardableResult
|
|
func delete(key: String) -> OSStatus {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: key,
|
|
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny
|
|
]
|
|
|
|
return SecItemDelete(query as CFDictionary)
|
|
}
|
|
|
|
func describe(_ status: OSStatus) -> String {
|
|
if status == errSecSuccess { return "success" }
|
|
return (SecCopyErrorMessageString(status, nil) as String?) ?? "status \(status)"
|
|
}
|
|
}
|