From 4fdd01052b0e08f0dd20f05650468000875c0039 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 23 Jan 2021 01:43:41 -0800 Subject: [PATCH 1/3] Add option to store a specified private+public key pair directly This is useful for migrating keys from one machine to another. --- generate_keys/main.swift | 118 +++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 31 deletions(-) diff --git a/generate_keys/main.swift b/generate_keys/main.swift index cca1dca9..783656b5 100644 --- a/generate_keys/main.swift +++ b/generate_keys/main.swift @@ -9,6 +9,8 @@ import Foundation import Security +let PRIVATE_KEY_LABEL = "Private key for signing Sparkle updates" + private func commonKeychainItemAttributes() -> [String: Any] { /// Attributes used for both adding a new item and matching an existing one. return [ @@ -86,7 +88,7 @@ func findPublicKey() -> Data? { exit(1) } -func generateKeyPair() -> Data { +func generateKeyPair() -> (publicEdKey: Data, privateEdKey: Data) { var seed = Array(repeating: 0, count: 32) var publicEdKey = Array(repeating: 0, count: 32) var privateEdKey = Array(repeating: 0, count: 64) @@ -95,7 +97,11 @@ func generateKeyPair() -> Data { failure("Unable to initialize random seed. Try restarting your computer.") } ed25519_create_keypair(&publicEdKey, &privateEdKey, seed) + + return (Data(publicEdKey), Data(privateEdKey)) +} +func storeKeyPair(publicEdKey: Data, privateEdKey: Data) { let query = commonKeychainItemAttributes().merging([ /// Mark the new item as sensitive (requires keychain password to export - e.g. a private key). kSecAttrIsSensitive as String: kCFBooleanTrue!, @@ -105,7 +111,7 @@ func generateKeyPair() -> Data { kSecAttrIsPermanent as String: kCFBooleanTrue!, /// The label of the new item (shown as its name/title in Keychain Access). - kSecAttrLabel as String: "Private key for signing Sparkle updates", + kSecAttrLabel as String: PRIVATE_KEY_LABEL, /// A comment regarding the item's content (can be viewed in Keychain Access; we give the public key here). kSecAttrComment as String: "Public key (SUPublicEDKey value) for this key is:\n\n\(Data(publicEdKey).base64EncodedString())", @@ -120,7 +126,7 @@ func generateKeyPair() -> Data { switch SecItemAdd(query, nil) { case errSecSuccess: - return Data(publicEdKey) + break case errSecDuplicateItem: failure("You already have a conflicting key in your Keychain which was not found during lookup.") case errSecAuthFailed: @@ -135,36 +141,98 @@ func generateKeyPair() -> Data { You can look up this error at """) } - exit(1) +} + +func printNewPublicKeyUsage(_ publicKey: Data) { + print(""" + A key has been generated and saved in your keychain. Add the `SUPublicEDKey` key to + the Info.plist of each app for which you intend to use Sparkle for distributing + updates. It should appear like this: + + SUPublicEDKey + \(publicKey.base64EncodedString()) + + """) } /// Once it's safe to require Swift 5.3 and Xcode 12 for this code, rename this file to `generate_keys.swift` and /// replace this function with a class tagged with `@main`. func entryPoint() { - let isLookupMode = (CommandLine.arguments.dropFirst().first.map({ $0 == "-p" }) ?? false) + let arguments = CommandLine.arguments + let programName = arguments.first ?? "generate_keys" + + let mode = arguments.count > 1 ? arguments[1] : nil - /// If not in lookup-only mode, give an intro blurb. - if !isLookupMode { + /// If not in any mode, give an intro blurb. + if mode == nil { print(""" - This tool uses the macOS Keychain to store a private key for signing app updates which - will be distributed via Sparkle. The key will be associated with your user account. + Usage: \(programName) [-p] [-s key-pair] + + This tool uses the macOS Keychain to store a public/private key pair for signing app updates which + will be distributed via Sparkle. This key will be associated with your user account. Note: You only need one signing key, no matter how many apps you embed Sparkle in. The keychain may ask permission for this tool to access an existing key, if one exists, or for permission to save the new key. You must allow access in order to successfully proceed. + + Additional Options: + -p + Looks up and just prints the existing public key stored in the Keychain. + -s key-pair + Stores the specified key-pair to your keychain instead of generating a new key. + If you want to migrate keys from another machine, find \"\(PRIVATE_KEY_LABEL)\" + in Keychain Access from your previous machine and retrieve its password, + which is the key-pair to specify here. Any existing keys in Keychain Access + may need to be removed first before proceeding. + + ---------------------------------------------------------------------------------------------------- + """) } - switch (findPublicKey(), isLookupMode) { - /// Existing key found, lookup mode - print just the pubkey and exit - case (.some(let pubKey), true): + switch mode { + case .some("-p"): + /// Lookup mode - print just the pubkey and exit + if let pubKey = findPublicKey() { print(pubKey.base64EncodedString()) + } else { + failure("No existing signing key found!") + } + case .some("-s"): + /// Store mode - store the specifed key-pair + guard arguments.count > 2 else { + failure("key-pair was not specified") + } - /// Existing key found, normal mode - print instructions blurb and pubkey - case (.some(let pubKey), false): + let privateAndPublicBase64Key = arguments[2] + print(privateAndPublicBase64Key) + + guard let privateAndPublicKey = Data(base64Encoded: privateAndPublicBase64Key, options: .init()) else { + failure("Failed to decode private+public key data from: \(privateAndPublicBase64Key)") + } + + guard privateAndPublicKey.count == 64 + 32 else { + failure("Specified private+public key must be 96 bytes decoded. Instead it is \(privateAndPublicKey.count) bytes decoded.") + } + + print("Storing signing key..") + + let publicKey = privateAndPublicKey[64...] + let privateKey = privateAndPublicKey[0..<64] + + storeKeyPair(publicEdKey: publicKey, privateEdKey: privateKey) + + printNewPublicKeyUsage(publicKey) + + case .some(let unknownOption): + failure("Unknown option: \(unknownOption)") + + case nil: + /// Default mode - find an existing public key and print its usage, or generate new keys + if let pubKey = findPublicKey() { print(""" A pre-existing signing key was found. This is how it should appear in your Info.plist: @@ -172,26 +240,14 @@ func entryPoint() { \(pubKey.base64EncodedString()) """) - - /// No existing key, lookup mode - error out - case (.none, true): - failure("No existing signing key found!") - - /// No existing key, normal mode - generate a new one - case (.none, false): + } else { print("Generating a new signing key. This may take a moment, depending on your machine.") - let pubKey = generateKeyPair() + let (pubKey, privKey) = generateKeyPair() + storeKeyPair(publicEdKey: pubKey, privateEdKey: privKey) - print(""" - A key has been generated and saved in your keychain. Add the `SUPublicEDKey` key to - the Info.plist of each app for which you intend to use Sparkle for distributing - updates. It should appear like this: - - SUPublicEDKey - \(pubKey.base64EncodedString()) - - """) + printNewPublicKeyUsage(pubKey) + } } } From d85c1df752201c52c6e87d3dd79744c44bd243b7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 23 Jan 2021 07:51:32 -0800 Subject: [PATCH 2/3] Add options to export and import private/public key pair --- generate_keys/main.swift | 78 ++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/generate_keys/main.swift b/generate_keys/main.swift index 783656b5..feb0350c 100644 --- a/generate_keys/main.swift +++ b/generate_keys/main.swift @@ -39,7 +39,7 @@ private func failure(_ message: String) -> Never { exit(1) } -func findPublicKey() -> Data? { +func findKeyPair() -> Data? { var item: CFTypeRef? let res = SecItemCopyMatching(commonKeychainItemAttributes().merging([ /// Return a matched item's value as a CFData object. @@ -49,7 +49,7 @@ func findPublicKey() -> Data? { switch res { case errSecSuccess: if let keys = (item as? Data).flatMap({ Data(base64Encoded: $0) }) { - return keys[64...] + return keys } else { failure(""" Item found, but is corrupt or has been overwritten! @@ -120,7 +120,7 @@ func storeKeyPair(publicEdKey: Data, privateEdKey: Data) { kSecAttrDescription as String: "private key", /// The actual data content of the new item. - kSecValueData as String: Data(privateEdKey + publicEdKey).base64EncodedData() as CFData + kSecValueData as String: (privateEdKey + publicEdKey).base64EncodedData() as CFData ], uniquingKeysWith: { $1 }) as CFDictionary @@ -166,10 +166,11 @@ func entryPoint() { /// If not in any mode, give an intro blurb. if mode == nil { print(""" - Usage: \(programName) [-p] [-s key-pair] + Usage: \(programName) [-p] [-x private-key-file] [-f private-key-file] - This tool uses the macOS Keychain to store a public/private key pair for signing app updates which - will be distributed via Sparkle. This key will be associated with your user account. + This tool generates a public & private keys and uses the macOS Keychain to store + the private key for signing app updates which will be distributed via Sparkle. + This key will be associated with your user account. Note: You only need one signing key, no matter how many apps you embed Sparkle in. @@ -180,13 +181,16 @@ func entryPoint() { Additional Options: -p Looks up and just prints the existing public key stored in the Keychain. + + -x private-key-file + Exports your private key from your login keychain and writes it to private-key-file. Note the contents of + this sensitive exported file are the same as the password to the \"\(PRIVATE_KEY_LABEL)\" item in + your keychain. - -s key-pair - Stores the specified key-pair to your keychain instead of generating a new key. - If you want to migrate keys from another machine, find \"\(PRIVATE_KEY_LABEL)\" - in Keychain Access from your previous machine and retrieve its password, - which is the key-pair to specify here. Any existing keys in Keychain Access - may need to be removed first before proceeding. + -f private-key-file + Imports the private key from private-key-file into your keychain instead of generating a new key. + This file has likely been exported via -x option from another machine. + Any existing \"\(PRIVATE_KEY_LABEL)\" items listed in Keychain Access may need to be removed manually first before proceeding. ---------------------------------------------------------------------------------------------------- @@ -196,29 +200,35 @@ func entryPoint() { switch mode { case .some("-p"): /// Lookup mode - print just the pubkey and exit - if let pubKey = findPublicKey() { + if let keyPair = findKeyPair() { + let pubKey = keyPair[64...] print(pubKey.base64EncodedString()) } else { failure("No existing signing key found!") } - case .some("-s"): - /// Store mode - store the specifed key-pair + case .some("-f"): + /// Import mode - import the specifed key-pair file guard arguments.count > 2 else { - failure("key-pair was not specified") + failure("private-key-file was not specified") } - let privateAndPublicBase64Key = arguments[2] - print(privateAndPublicBase64Key) + let privateAndPublicBase64KeyFile = arguments[2] + let privateAndPublicBase64Key: String + do { + privateAndPublicBase64Key = try String(contentsOfFile: privateAndPublicBase64KeyFile) + } catch { + failure("Failed to read private-key-file: \(error)") + } - guard let privateAndPublicKey = Data(base64Encoded: privateAndPublicBase64Key, options: .init()) else { - failure("Failed to decode private+public key data from: \(privateAndPublicBase64Key)") + guard let privateAndPublicKey = Data(base64Encoded: privateAndPublicBase64Key.trimmingCharacters(in: .whitespacesAndNewlines), options: .init()) else { + failure("Failed to decode base64 encoded key data from: \(privateAndPublicBase64Key)") } guard privateAndPublicKey.count == 64 + 32 else { - failure("Specified private+public key must be 96 bytes decoded. Instead it is \(privateAndPublicKey.count) bytes decoded.") + failure("Imported key must be 96 bytes decoded. Instead it is \(privateAndPublicKey.count) bytes decoded.") } - print("Storing signing key..") + print("Importing signing key..") let publicKey = privateAndPublicKey[64...] let privateKey = privateAndPublicKey[0..<64] @@ -227,12 +237,34 @@ func entryPoint() { printNewPublicKeyUsage(publicKey) + case .some("-x"): + /// Export mode - export the key-pair file from the user's keychain + guard arguments.count > 2 else { + failure("private-key-file was not specified") + } + + let exportURL = URL(fileURLWithPath: arguments[2]) + if let reachable = try? exportURL.checkResourceIsReachable(), reachable { + failure("private-key-file already exists: \(exportURL.path)") + } + + guard let keyPair = findKeyPair() else { + failure("No existing signing key found!") + } + + do { + try keyPair.base64EncodedString().write(to: exportURL, atomically: true, encoding: .utf8) + } catch { + failure("Failed to write exported file: \(error)") + } + case .some(let unknownOption): failure("Unknown option: \(unknownOption)") case nil: /// Default mode - find an existing public key and print its usage, or generate new keys - if let pubKey = findPublicKey() { + if let keyPair = findKeyPair() { + let pubKey = keyPair[64...] print(""" A pre-existing signing key was found. This is how it should appear in your Info.plist: From 5ca8984c100859e2ae6cb8935eb2ede94df49167 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 00:21:47 -0800 Subject: [PATCH 3/3] Port preventing new version from being auto installed to 2.x Original PR for master: https://github.com/sparkle-project/Sparkle/pull/1688 --- Sparkle/SPUAutomaticUpdateDriver.m | 6 +++--- Sparkle/SPUBasicUpdateDriver.h | 2 +- Sparkle/SPUBasicUpdateDriver.m | 6 +++--- Sparkle/SPUCoreBasedUpdateDriver.h | 4 ++-- Sparkle/SPUCoreBasedUpdateDriver.m | 12 ++++++------ Sparkle/SPUInformationalUpdate.h | 2 +- Sparkle/SPUInformationalUpdate.m | 4 +++- Sparkle/SPUProbingUpdateDriver.m | 2 +- Sparkle/SPUResumableUpdate.h | 1 + Sparkle/SPUUIBasedUpdateDriver.m | 5 +++-- Sparkle/SUAppcastDriver.h | 2 +- Sparkle/SUAppcastDriver.m | 7 ++++++- Sparkle/SUAppcastItem.h | 1 + Sparkle/SUAppcastItem.m | 7 +++++++ Sparkle/SUConstants.h | 1 + Sparkle/SUConstants.m | 1 + 16 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Sparkle/SPUAutomaticUpdateDriver.m b/Sparkle/SPUAutomaticUpdateDriver.m index 401b7cdc..0515a9d7 100644 --- a/Sparkle/SPUAutomaticUpdateDriver.m +++ b/Sparkle/SPUAutomaticUpdateDriver.m @@ -77,12 +77,12 @@ SULog(SULogLevelError, @"Error: resumeDownloadedUpdate:completion: called on SPUAutomaticUpdateDriver"); } -- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem +- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate { self.updateItem = updateItem; - if (updateItem.isInformationOnlyUpdate) { - [self.coreDriver deferInformationalUpdate:updateItem]; + if (updateItem.isInformationOnlyUpdate || preventsAutoupdate) { + [self.coreDriver deferInformationalUpdate:updateItem preventsAutoupdate:preventsAutoupdate]; [self abortUpdate]; } else { [self.coreDriver downloadUpdateFromAppcastItem:updateItem inBackground:YES]; diff --git a/Sparkle/SPUBasicUpdateDriver.h b/Sparkle/SPUBasicUpdateDriver.h index b9003ee1..a8de86e3 100644 --- a/Sparkle/SPUBasicUpdateDriver.h +++ b/Sparkle/SPUBasicUpdateDriver.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @protocol SPUBasicUpdateDriverDelegate -- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)appcastItem; +- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)appcastItem preventsAutoupdate:(BOOL)preventsAutoupdate; - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error; diff --git a/Sparkle/SPUBasicUpdateDriver.m b/Sparkle/SPUBasicUpdateDriver.m index 7b1c618a..9d8c8ffe 100644 --- a/Sparkle/SPUBasicUpdateDriver.m +++ b/Sparkle/SPUBasicUpdateDriver.m @@ -87,7 +87,7 @@ [self notifyFinishLoadingAppcast]; SUAppcastItem *nonNullUpdateItem = updateItem; - [self didFindValidUpdateWithAppcastItem:nonNullUpdateItem]; + [self didFindValidUpdateWithAppcastItem:nonNullUpdateItem preventsAutoupdate:NO]; } } @@ -141,7 +141,7 @@ } } -- (void)didFindValidUpdateWithAppcastItem:(SUAppcastItem *)updateItem +- (void)didFindValidUpdateWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate { if (!self.aborted) { [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterDidFindValidUpdateNotification @@ -152,7 +152,7 @@ [self.updaterDelegate updater:self.updater didFindValidUpdate:updateItem]; } - [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem]; + [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem preventsAutoupdate:preventsAutoupdate]; } } diff --git a/Sparkle/SPUCoreBasedUpdateDriver.h b/Sparkle/SPUCoreBasedUpdateDriver.h index 012e7d43..9014289d 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.h +++ b/Sparkle/SPUCoreBasedUpdateDriver.h @@ -17,7 +17,7 @@ NS_ASSUME_NONNULL_BEGIN @protocol SPUCoreBasedUpdateDriverDelegate -- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem; +- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate; - (void)installerDidFinishPreparationAndWillInstallImmediately:(BOOL)willInstallImmediately silently:(BOOL)willInstallSilently; @@ -63,7 +63,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem inBackground:(BOOL)background; -- (void)deferInformationalUpdate:(SUAppcastItem *)updateItem; +- (void)deferInformationalUpdate:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate; - (void)extractDownloadedUpdate; diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index f9bb98ac..d1647ce4 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -141,24 +141,24 @@ } } -- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem +- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate { self.updateItem = updateItem; if (self.resumingInstallingUpdate) { [self.installerDriver resumeInstallingUpdateWithUpdateItem:updateItem]; - [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem]; + [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem preventsAutoupdate:preventsAutoupdate]; } else { if (!self.preventsInstallerInteraction) { // Simple case - delegate allows interaction, so we should continue along - [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem]; + [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem preventsAutoupdate:preventsAutoupdate]; } else { // Package type installations will always require installer interaction as long as we don't support running as root // If it's not a package type installation, we should be okay since we did an auth check before checking for updates above if (![updateItem.installationType isEqualToString:SPUInstallationTypeApplication]) { [self.delegate coreDriverIsRequestingAbortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUNotAllowedInteractionError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:SULocalizedString(@"A new update is available but cannot be installed because interaction has been prevented.", nil)] }]]; } else { - [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem]; + [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem preventsAutoupdate:preventsAutoupdate]; } } } @@ -204,9 +204,9 @@ [self extractUpdate:downloadedUpdate]; } -- (void)deferInformationalUpdate:(SUAppcastItem *)updateItem +- (void)deferInformationalUpdate:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate { - self.resumableUpdate = [[SPUInformationalUpdate alloc] initWithAppcastItem:updateItem]; + self.resumableUpdate = [[SPUInformationalUpdate alloc] initWithAppcastItem:updateItem preventsAutoupdate:preventsAutoupdate]; } - (void)extractDownloadedUpdate diff --git a/Sparkle/SPUInformationalUpdate.h b/Sparkle/SPUInformationalUpdate.h index 2302d5a4..c26b03f4 100644 --- a/Sparkle/SPUInformationalUpdate.h +++ b/Sparkle/SPUInformationalUpdate.h @@ -13,7 +13,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SPUInformationalUpdate : NSObject -- (instancetype)initWithAppcastItem:(SUAppcastItem *)updateItem; +- (instancetype)initWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate; @end diff --git a/Sparkle/SPUInformationalUpdate.m b/Sparkle/SPUInformationalUpdate.m index d5cdfca0..6dd3b15c 100644 --- a/Sparkle/SPUInformationalUpdate.m +++ b/Sparkle/SPUInformationalUpdate.m @@ -16,12 +16,14 @@ // If we ever enable auto-synthesize in the future, we'll still need this synthesize // because the property is declared in a protocol @synthesize updateItem = _updateItem; +@synthesize preventsAutoupdate = _preventsAutoupdate; -- (instancetype)initWithAppcastItem:(SUAppcastItem *)updateItem +- (instancetype)initWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate { self = [super init]; if (self != nil) { _updateItem = updateItem; + _preventsAutoupdate = preventsAutoupdate; } return self; } diff --git a/Sparkle/SPUProbingUpdateDriver.m b/Sparkle/SPUProbingUpdateDriver.m index a16f045a..61a1b7c6 100644 --- a/Sparkle/SPUProbingUpdateDriver.m +++ b/Sparkle/SPUProbingUpdateDriver.m @@ -54,7 +54,7 @@ [self.basicDriver resumeUpdate:resumableUpdate completion:completionBlock]; } -- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)__unused appcastItem +- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)__unused appcastItem preventsAutoupdate:(BOOL)__unused preventsAutoupdate { // Stop as soon as we have an answer [self abortUpdate]; diff --git a/Sparkle/SPUResumableUpdate.h b/Sparkle/SPUResumableUpdate.h index 31956ca7..65ad7db6 100644 --- a/Sparkle/SPUResumableUpdate.h +++ b/Sparkle/SPUResumableUpdate.h @@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN @protocol SPUResumableUpdate @property (nonatomic, readonly) SUAppcastItem *updateItem; +@property (nonatomic, readonly) BOOL preventsAutoupdate; @end diff --git a/Sparkle/SPUUIBasedUpdateDriver.m b/Sparkle/SPUUIBasedUpdateDriver.m index 57d3b3c1..d7dd3fc8 100644 --- a/Sparkle/SPUUIBasedUpdateDriver.m +++ b/Sparkle/SPUUIBasedUpdateDriver.m @@ -91,7 +91,8 @@ - (void)resumeUpdate:(id)resumableUpdate completion:(SPUUpdateDriverCompletion)completionBlock { // Informational downloads shouldn't be presented as updates to be downloaded - if (!resumableUpdate.updateItem.isInformationOnlyUpdate) { + // Neither should items that prevent auto updating + if (!resumableUpdate.updateItem.isInformationOnlyUpdate && !resumableUpdate.preventsAutoupdate) { self.resumingDownloadedUpdate = YES; } [self.coreDriver resumeUpdate:resumableUpdate completion:completionBlock]; @@ -104,7 +105,7 @@ } } -- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem +- (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem preventsAutoupdate:(BOOL)preventsAutoupdate { if (updateItem.isInformationOnlyUpdate) { assert(!self.resumingDownloadedUpdate); diff --git a/Sparkle/SUAppcastDriver.h b/Sparkle/SUAppcastDriver.h index 080ca733..a032b508 100644 --- a/Sparkle/SUAppcastDriver.h +++ b/Sparkle/SUAppcastDriver.h @@ -17,7 +17,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)didFailToFetchAppcastWithError:(NSError *)error; - (void)didFinishLoadingAppcast:(SUAppcast *)appcast; -- (void)didFindValidUpdateWithAppcastItem:(SUAppcastItem *)appcastItem; +- (void)didFindValidUpdateWithAppcastItem:(SUAppcastItem *)appcastItem preventsAutoupdate:(BOOL)preventsAutoupdate; - (void)didNotFindUpdate; @end diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 87574255..38949b1d 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -104,7 +104,7 @@ if ([self itemContainsValidUpdate:item includesSkippedUpdates:includesSkippedUpdates]) { self.nonDeltaUpdateItem = nonDeltaUpdateItem; - [self.delegate didFindValidUpdateWithAppcastItem:item]; + [self.delegate didFindValidUpdateWithAppcastItem:item preventsAutoupdate:[self itemPreventsAutoupdate:item]]; } else { [self.delegate didNotFindUpdate]; } @@ -177,6 +177,11 @@ return [[self versionComparator] compareVersion:[self.host version] toVersion:[ui versionString]] == NSOrderedAscending; } +- (BOOL)itemPreventsAutoupdate:(SUAppcastItem *)ui + { + return ([ui minimumAutoupdateVersion] && ! [[ui minimumAutoupdateVersion] isEqualToString:@""] && ([[self versionComparator] compareVersion:[self.host version] toVersion:[ui minimumAutoupdateVersion]] == NSOrderedAscending)); + } + - (BOOL)itemContainsSkippedVersion:(SUAppcastItem *)ui { NSString *skippedVersion = [self.host objectForUserDefaultsKey:SUSkippedVersionKey]; diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index b92b3ef5..c722e53d 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -36,6 +36,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly) NSDictionary *deltaUpdates; @property (strong, readonly) NSURL *infoURL; @property (nonatomic, copy, readonly) NSString *installationType; +@property (copy, readonly) NSString *minimumAutoupdateVersion; // Initializes with data from a dictionary provided by the RSS class. - (instancetype)initWithDictionary:(NSDictionary *)dict; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 33de60f8..71c3f07f 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -49,6 +49,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT @synthesize osString = _osString; @synthesize propertiesDictionary = _propertiesDictionary; @synthesize installationType = _installationType; +@synthesize minimumAutoupdateVersion = _minimumAutoupdateVersion; + (BOOL)supportsSecureCoding { @@ -76,6 +77,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _itemDescription = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemDescriptionKey] copy]; _maximumSystemVersion = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemMaximumSystemVersionKey] copy]; _minimumSystemVersion = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemMinimumSystemVersionKey] copy]; + _minimumAutoupdateVersion = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastElementMinimumAutoupdateVersion] copy]; _releaseNotesURL = [decoder decodeObjectOfClass:[NSURL class] forKey:SUAppcastItemReleaseNotesURLKey]; _title = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemTitleKey] copy]; _versionString = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemVersionStringKey] copy]; @@ -122,6 +124,10 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT [encoder encodeObject:self.minimumSystemVersion forKey:SUAppcastItemMinimumSystemVersionKey]; } + if (self.minimumAutoupdateVersion != nil) { + [encoder encodeObject:self.minimumAutoupdateVersion forKey:SUAppcastElementMinimumAutoupdateVersion]; + } + if (self.releaseNotesURL != nil) { [encoder encodeObject:self.releaseNotesURL forKey:SUAppcastItemReleaseNotesURLKey]; } @@ -268,6 +274,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _versionString = [(NSString *)newVersion copy]; _minimumSystemVersion = [(NSString *)[dict objectForKey:SUAppcastElementMinimumSystemVersion] copy]; _maximumSystemVersion = [(NSString *)[dict objectForKey:SUAppcastElementMaximumSystemVersion] copy]; + _minimumAutoupdateVersion = [(NSString *)[dict objectForKey:SUAppcastElementMinimumAutoupdateVersion] copy]; NSString *shortVersionString = [enclosure objectForKey:SUAppcastAttributeShortVersionString]; if (nil == shortVersionString) { diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index 61973bc5..e765fc3b 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -76,6 +76,7 @@ extern NSString *const SUAppcastAttributeInstallationType; extern NSString *const SUAppcastElementCriticalUpdate; extern NSString *const SUAppcastElementDeltas; +extern NSString *const SUAppcastElementMinimumAutoupdateVersion; extern NSString *const SUAppcastElementMinimumSystemVersion; extern NSString *const SUAppcastElementMaximumSystemVersion; extern NSString *const SUAppcastElementReleaseNotesLink; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 28fd37e7..1a055784 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -69,6 +69,7 @@ NSString *const SUAppcastAttributeInstallationType = @"sparkle:installationType" NSString *const SUAppcastElementCriticalUpdate = @"sparkle:criticalUpdate"; NSString *const SUAppcastElementDeltas = @"sparkle:deltas"; +NSString *const SUAppcastElementMinimumAutoupdateVersion = @"sparkle:minimumAutoupdateVersion"; NSString *const SUAppcastElementMinimumSystemVersion = @"sparkle:minimumSystemVersion"; NSString *const SUAppcastElementMaximumSystemVersion = @"sparkle:maximumSystemVersion"; NSString *const SUAppcastElementReleaseNotesLink = @"sparkle:releaseNotesLink";