diff options
Diffstat (limited to 'DomainDig')
| -rw-r--r-- | DomainDig/AppVersion.swift | 2 | ||||
| -rw-r--r-- | DomainDig/OwnerAccess.swift | 34 | ||||
| -rw-r--r-- | DomainDig/PurchaseService.swift | 53 |
3 files changed, 85 insertions, 4 deletions
diff --git a/DomainDig/AppVersion.swift b/DomainDig/AppVersion.swift index 479695a..5fb8925 100644 --- a/DomainDig/AppVersion.swift +++ b/DomainDig/AppVersion.swift @@ -2,6 +2,6 @@ import Foundation enum AppVersion { nonisolated static var current: String { - "5.0.0" + "5.0.1" } } diff --git a/DomainDig/OwnerAccess.swift b/DomainDig/OwnerAccess.swift new file mode 100644 index 0000000..eca2bab --- /dev/null +++ b/DomainDig/OwnerAccess.swift @@ -0,0 +1,34 @@ +import CloudKit + +/// Owner-only entitlement support. The app owner is identified by their CloudKit +/// user-record ID — a stable, opaque per-Apple-ID value for this app's container. +/// `PurchaseService` grants the owner Pro+ when the signed-in iCloud user matches, +/// so the owner does not need a purchase. +/// +/// Publishing the record ID here is safe: it is not an Apple ID or any personal +/// identifier, it is scoped to the `iCloud.net.cleberg.DomainDig` container, and +/// CloudKit identity is verified server-side — another user cannot present it as +/// their own. An empty value makes the allowlist inert. +enum OwnerAccess { + static let ownerUserRecordID = "_1c35d6a25540b3ef00023cc0425ec373" + + static var isConfigured: Bool { !ownerUserRecordID.isEmpty } + + /// The current iCloud user's record name for this app's container, or nil if + /// it is unavailable (not signed into iCloud, restricted, or offline before + /// the first fetch). + static func currentUserRecordName() async -> String? { + do { + return try await CKContainer.default().userRecordID().recordName + } catch { + return nil + } + } + + /// True only when the allowlist is configured and the current iCloud user is + /// the owner. + static func isOwner() async -> Bool { + guard isConfigured else { return false } + return await currentUserRecordName() == ownerUserRecordID + } +} diff --git a/DomainDig/PurchaseService.swift b/DomainDig/PurchaseService.swift index 4ab8ef4..91ce211 100644 --- a/DomainDig/PurchaseService.swift +++ b/DomainDig/PurchaseService.swift @@ -34,6 +34,20 @@ final class PurchaseService { private static let debugForceProPlusArgument = "DOMAIN_DIG_FORCE_PRO_PLUS" #endif + private static let ownerEntitlementKey = "purchase.ownerEntitlement" + + /// Whether the owner allowlist has confirmed this device's iCloud user as the + /// owner. Persisted so the grant is instant on later launches and survives + /// offline, when CloudKit cannot be reached. + static var ownerEntitlementGranted: Bool { + UserDefaults.standard.bool(forKey: ownerEntitlementKey) + } + + private static var storedEntitlement: CachedEntitlement? { + guard let data = UserDefaults.standard.data(forKey: entitlementCacheKey) else { return nil } + return try? JSONDecoder().decode(CachedEntitlement.self, from: data) + } + static var cachedEntitlement: CachedEntitlement? { #if DEBUG if let forcedEntitlement = debugForcedEntitlement { @@ -41,12 +55,17 @@ final class PurchaseService { } #endif - guard let data = UserDefaults.standard.data(forKey: entitlementCacheKey) else { return nil } - return try? JSONDecoder().decode(CachedEntitlement.self, from: data) + return storedEntitlement } static var cachedTier: FeatureTier { - cachedEntitlement?.tier ?? .free + #if DEBUG + // A debug override wins outright so free/pro tiers remain testable on the + // owner's own device. + if let forcedEntitlement = debugForcedEntitlement { return forcedEntitlement.tier } + #endif + if ownerEntitlementGranted { return .proPlus } + return storedEntitlement?.tier ?? .free } var products: [Product] = [] @@ -64,8 +83,10 @@ final class PurchaseService { currentTier = Self.cachedTier activeProductID = Self.cachedEntitlement?.activeProductID applyDebugOverrideIfNeeded() + applyOwnerOverrideIfNeeded() updatesTask = observeTransactionUpdates() Task { + await resolveOwnerEntitlementIfNeeded() await refreshProducts() await refreshEntitlements() } @@ -118,6 +139,7 @@ final class PurchaseService { currentTier = tier(for: activeProductID) persistCurrentEntitlement() applyDebugOverrideIfNeeded() + applyOwnerOverrideIfNeeded() } func purchase(_ product: Product) async { @@ -251,6 +273,31 @@ final class PurchaseService { #endif } + /// Elevates the current tier to Pro+ when the device's iCloud user has been + /// confirmed as the owner. Only ever elevates, and defers to a debug override + /// so free/pro tiers stay testable on the owner's own device. + private func applyOwnerOverrideIfNeeded() { + #if DEBUG + if Self.debugForcedEntitlement != nil { return } + #endif + guard Self.ownerEntitlementGranted else { return } + currentTier = .proPlus + } + + /// Resolves the owner allowlist against CloudKit once per launch. On a match + /// it records the grant so future launches apply it synchronously and offline. + private func resolveOwnerEntitlementIfNeeded() async { + guard OwnerAccess.isConfigured else { return } + if Self.ownerEntitlementGranted { + applyOwnerOverrideIfNeeded() + return + } + if await OwnerAccess.isOwner() { + UserDefaults.standard.set(true, forKey: Self.ownerEntitlementKey) + applyOwnerOverrideIfNeeded() + } + } + private func verifiedTransaction(from result: VerificationResult<Transaction>) throws -> Transaction { switch result { case .verified(let transaction): |
