// // VerifyAppOperation.swift // AltStore // // Created by Riley Testut on 5/2/20. // Copyright © 2020 Riley Testut. All rights reserved. // import Foundation import AltStoreCore import AltSign import Roxas extension VerificationError { enum Code: Int, ALTErrorCode, CaseIterable { typealias Error = VerificationError case mismatchedBundleIdentifiers case iOSVersionNotSupported } static func mismatchedBundleIdentifiers(sourceBundleID: String, app: ALTApplication) -> VerificationError { VerificationError(code: .mismatchedBundleIdentifiers, app: app, sourceBundleID: sourceBundleID) } static func iOSVersionNotSupported(app: AppProtocol, osVersion: OperatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion, requiredOSVersion: OperatingSystemVersion?) -> VerificationError { VerificationError(code: .iOSVersionNotSupported, app: app, deviceOSVersion: osVersion, requiredOSVersion: requiredOSVersion) } } struct VerificationError: ALTLocalizedError { let code: Code var errorTitle: String? var errorFailure: String? @Managed var app: AppProtocol? var sourceBundleID: String? var deviceOSVersion: OperatingSystemVersion? var requiredOSVersion: OperatingSystemVersion? var errorDescription: String? { //TODO: Make this automatic somehow with ALTLocalizedError guard self.errorFailure == nil else { return nil } switch self.code { case .iOSVersionNotSupported: guard let deviceOSVersion else { break } var failureReason = self.errorFailureReason if self.app == nil { // failureReason does not start with app name, so make first letter lowercase. let firstLetter = failureReason.prefix(1).lowercased() failureReason = firstLetter + failureReason.dropFirst() } let localizedDescription = String(format: NSLocalizedString("This device is running iOS %@, but %@", comment: ""), deviceOSVersion.stringValue, failureReason) return localizedDescription default: break } return self.errorFailureReason } var errorFailureReason: String { switch self.code { case .mismatchedBundleIdentifiers: if let appBundleID = self.$app.bundleIdentifier, let bundleID = self.sourceBundleID { return String(format: NSLocalizedString("The bundle ID “%@” does not match the one specified by the source (“%@”).", comment: ""), appBundleID, bundleID) } else { return NSLocalizedString("The bundle ID does not match the one specified by the source.", comment: "") } case .iOSVersionNotSupported: let appName = self.$app.name ?? NSLocalizedString("The app", comment: "") let deviceOSVersion = self.deviceOSVersion ?? ProcessInfo.processInfo.operatingSystemVersion guard let requiredOSVersion else { return String(format: NSLocalizedString("%@ does not support iOS %@.", comment: ""), appName, deviceOSVersion.stringValue) } if deviceOSVersion > requiredOSVersion { // Device OS version is higher than maximum supported OS version. let failureReason = String(format: NSLocalizedString("%@ requires iOS %@ or earlier.", comment: ""), appName, requiredOSVersion.stringValue) return failureReason } else { // Device OS version is lower than minimum supported OS version. let failureReason = String(format: NSLocalizedString("%@ requires iOS %@ or later.", comment: ""), appName, requiredOSVersion.stringValue) return failureReason } } } } @objc(VerifyAppOperation) class VerifyAppOperation: ResultOperation { let context: AppOperationContext var verificationHandler: ((VerificationError) -> Bool)? init(context: AppOperationContext) { self.context = context super.init() } override func main() { super.main() do { if let error = self.context.error { throw error } let appName = self.context.app?.name ?? NSLocalizedString("The app", comment: "") self.localizedFailure = String(format: NSLocalizedString("%@ could not be installed.", comment: ""), appName) guard let app = self.context.app else { throw OperationError.invalidParameters } guard app.bundleIdentifier == self.context.bundleIdentifier else { throw VerificationError.mismatchedBundleIdentifiers(sourceBundleID: self.context.bundleIdentifier, app: app) } guard ProcessInfo.processInfo.isOperatingSystemAtLeast(app.minimumiOSVersion) else { throw VerificationError.iOSVersionNotSupported(app: app, requiredOSVersion: app.minimumiOSVersion) } self.finish(.success(())) } catch { self.finish(.failure(error)) } } }