Permalink
Browse files

Save MAS receipts into Library and use them to authenticate non-MAS v…

…ersions later
  • Loading branch information...
1 parent 37544f0 commit d4c55f9d381d2ecc9d1f5f428b4d68f762eceac3 @andreyvit andreyvit committed Mar 24, 2012
@@ -19,6 +19,7 @@
#import "ShitHappens.h"
#import "FixUnixPath.h"
+#import "MASReceipt.h"
void C_mainwnd__set_project_list(json_t *arg) {
@@ -90,35 +91,30 @@ - (void)awakeFromNib {
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
-#ifdef APPSTORE
- NSString *receiptPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/_MASReceipt/receipt"];
- if (![[NSFileManager defaultManager] fileExistsAtPath:receiptPath]) {
- // magic return value to make Finder ask for an App Store account and create a receipt
- exit(173);
- }
-#endif
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
#ifndef APPSTORE
- NSDate *now = [NSDate date];
- NSDateComponents *cutoff = [[[NSDateComponents alloc] init] autorelease];
- [cutoff setYear:2012];
- [cutoff setMonth:5];
- [cutoff setDay:1];
- if ([now compare:[[NSCalendar currentCalendar] dateFromComponents:cutoff]] == NSOrderedDescending) {
- // stop auto-login and show a message
- NSInteger ans = [[NSAlert alertWithMessageText:@"LiveReload 2 trial has expired"
- defaultButton:@"Visit our site"
- alternateButton:@"Quit LiveReload"
- otherButton:nil
- informativeTextWithFormat:@"Sorry, this trial version of LiveReload has expired and cannot be launched.\n\nPlease visit http://livereload.com/ to get an updated version."] runModal];
- if (ans == NSAlertDefaultReturn) {
- [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://livereload.com/"]];
- } else {
- [LoginItemController sharedController].loginItemEnabled = NO;
+ if (!MASReceiptIsAuthenticated()) {
+ NSDate *now = [NSDate date];
+ NSDateComponents *cutoff = [[[NSDateComponents alloc] init] autorelease];
+ [cutoff setYear:2012];
+ [cutoff setMonth:5];
+ [cutoff setDay:1];
+ if ([now compare:[[NSCalendar currentCalendar] dateFromComponents:cutoff]] == NSOrderedDescending) {
+ // stop auto-login and show a message
+ NSInteger ans = [[NSAlert alertWithMessageText:@"LiveReload 2 trial has expired"
+ defaultButton:@"Visit our site"
+ alternateButton:@"Quit LiveReload"
+ otherButton:nil
+ informativeTextWithFormat:@"Sorry, this trial version of LiveReload has expired and cannot be launched.\n\nPlease visit http://livereload.com/ to get an updated version."] runModal];
+ if (ans == NSAlertDefaultReturn) {
+ [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://livereload.com/"]];
+ } else {
+ [LoginItemController sharedController].loginItemEnabled = NO;
+ }
+ [NSApp terminate:self];
}
- [NSApp terminate:self];
}
#endif
@@ -0,0 +1,10 @@
+
+#import <Foundation/Foundation.h>
+
+// define APPSTORE for App Store builds
+
+#define MASReceiptApplicationSupportReceiptsFolder @"LiveReload/Receipts"
+#define MASReceiptFileExtension @"livereload-receipt"
+
+void MASReceiptStartup();
+BOOL MASReceiptIsAuthenticated();
@@ -0,0 +1,60 @@
+
+#import "MASReceipt.h"
+
+#import <CommonCrypto/CommonDigest.h>
+
+
+static NSString *SHA1OfNSDataAsNSString(NSData *data) {
+ uint8_t digest[CC_SHA1_DIGEST_LENGTH];
+ CC_SHA1(data.bytes, data.length, digest);
+
+ NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
+ for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
+ [output appendFormat:@"%02x", digest[i]];
+ return output;
+}
+
+
+static NSString *MASReceiptStashedReceiptsFolder(BOOL create) {
+ NSString *stashedReceiptsFolder = [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:MASReceiptApplicationSupportReceiptsFolder];
+ if (create) {
+ if (![[NSFileManager defaultManager] fileExistsAtPath:stashedReceiptsFolder]) {
+ [[NSFileManager defaultManager] createDirectoryAtPath:stashedReceiptsFolder withIntermediateDirectories:YES attributes:nil error:NULL];
+ }
+ }
+ return stashedReceiptsFolder;
+}
+
+
+void MASReceiptStartup() {
+#ifdef APPSTORE
+ NSString *receiptPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/_MASReceipt/receipt"];
+ if (![[NSFileManager defaultManager] fileExistsAtPath:receiptPath]) {
+ // magic return value to make Finder ask for an App Store account and create a receipt
+ exit(173);
+ }
+
+ NSData *receipt = [NSData dataWithContentsOfFile:receiptPath];
+ NSString *receiptHash = SHA1OfNSDataAsNSString(receipt);
+
+ NSString *stashedReceiptFile = [MASReceiptStashedReceiptsFolder(YES) stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", receiptHash, MASReceiptFileExtension]];
+ if (![[NSFileManager defaultManager] fileExistsAtPath:stashedReceiptFile]) {
+ [[NSFileManager defaultManager] copyItemAtPath:receiptPath toPath:stashedReceiptFile error:NULL];
+ }
+#endif
+}
+
+
+BOOL MASReceiptIsAuthenticated() {
+#ifdef APPSTORE
+ return YES;
+#else
+ NSString *stashedReceiptsFolder = MASReceiptStashedReceiptsFolder(NO);
+ for (NSString *fileName in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:stashedReceiptsFolder error:NULL]) {
+ if ([[fileName pathExtension] isEqualToString:MASReceiptFileExtension]) {
+ return YES;
+ }
+ }
+ return NO;
+#endif
+}
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
4304FC2D144B2BEB00D650DF /* NSDictionaryAndArray+SafeAccess.m in Sources */ = {isa = PBXBuildFile; fileRef = 4304FC2C144B2BEB00D650DF /* NSDictionaryAndArray+SafeAccess.m */; };
+ 43059209151DF2DE00CC3F88 /* MASReceipt.m in Sources */ = {isa = PBXBuildFile; fileRef = 43059208151DF2DE00CC3F88 /* MASReceipt.m */; };
4310DE3D146DB588003091D1 /* ToolOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4310DE3C146DB588003091D1 /* ToolOptions.m */; };
4310DE40146DB6A0003091D1 /* UIBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 4310DE3F146DB6A0003091D1 /* UIBuilder.m */; };
43146699146C5D2100DC075A /* ProjectPaneBackground.png in Resources */ = {isa = PBXBuildFile; fileRef = 43146698146C5D2100DC075A /* ProjectPaneBackground.png */; };
@@ -186,6 +187,8 @@
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
4304FC2B144B2BEB00D650DF /* NSDictionaryAndArray+SafeAccess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionaryAndArray+SafeAccess.h"; sourceTree = "<group>"; };
4304FC2C144B2BEB00D650DF /* NSDictionaryAndArray+SafeAccess.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionaryAndArray+SafeAccess.m"; sourceTree = "<group>"; };
+ 43059207151DF2DE00CC3F88 /* MASReceipt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASReceipt.h; sourceTree = "<group>"; };
+ 43059208151DF2DE00CC3F88 /* MASReceipt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASReceipt.m; sourceTree = "<group>"; };
4310DE3B146DB588003091D1 /* ToolOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ToolOptions.h; sourceTree = "<group>"; };
4310DE3C146DB588003091D1 /* ToolOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ToolOptions.m; sourceTree = "<group>"; };
4310DE3E146DB6A0003091D1 /* UIBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIBuilder.h; sourceTree = "<group>"; };
@@ -684,6 +687,8 @@
43FAB8561514FD5700400F61 /* osdep.h */,
43E34F9E14706D9B001641AA /* osdep.m */,
434DB81E1514F71400C62042 /* version.h */,
+ 43059207151DF2DE00CC3F88 /* MASReceipt.h */,
+ 43059208151DF2DE00CC3F88 /* MASReceipt.m */,
);
path = Application;
sourceTree = "<group>";
@@ -1205,6 +1210,7 @@
4378C8D21515205C003187D5 /* OldFSTree.m in Sources */,
4378C8D31515205C003187D5 /* OldFSTreeDiffer.m in Sources */,
4378C8D41515205C003187D5 /* OldFSTreeFilter.m in Sources */,
+ 43059209151DF2DE00CC3F88 /* MASReceipt.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -1,7 +1,12 @@
#import <Cocoa/Cocoa.h>
+#import "MASReceipt.h"
+
int main(int argc, char *argv[])
{
+ @autoreleasepool {
+ MASReceiptStartup();
+ }
return NSApplicationMain(argc, (const char **) argv);
}

0 comments on commit d4c55f9

Please sign in to comment.