From d5569f4b01ea82ebb9221ef9ed9df71ba8ba5dd9 Mon Sep 17 00:00:00 2001 From: Jason Wray Date: Wed, 17 Apr 2019 18:27:54 -0700 Subject: [ios] Add SKU tokens to all Mapbox requests (#14421) --- platform/darwin/src/MGLAccountManager.m | 40 +++++++++++++--- platform/darwin/src/MGLAccountManager_Private.h | 13 +++++- platform/darwin/src/http_file_source.mm | 24 ++++++++-- platform/ios/ios.xcodeproj/project.pbxproj | 52 ++++++++++++++++++++- platform/ios/sdk-files.json | 1 + platform/ios/src/MGLMapboxEvents.m | 3 ++ .../ios/vendor/mapbox-accounts-ios/MBXSKUToken.h | 13 ++++++ .../vendor/mapbox-accounts-ios/libmbxaccounts.a | Bin 0 -> 58936 bytes platform/ios/vendor/mapbox-events-ios | 2 +- 9 files changed, 132 insertions(+), 16 deletions(-) create mode 100644 platform/ios/vendor/mapbox-accounts-ios/MBXSKUToken.h create mode 100644 platform/ios/vendor/mapbox-accounts-ios/libmbxaccounts.a diff --git a/platform/darwin/src/MGLAccountManager.m b/platform/darwin/src/MGLAccountManager.m index 63fa634884..0566605323 100644 --- a/platform/darwin/src/MGLAccountManager.m +++ b/platform/darwin/src/MGLAccountManager.m @@ -1,26 +1,26 @@ #import "MGLAccountManager_Private.h" #import "NSBundle+MGLAdditions.h" + #if TARGET_OS_OSX #import "NSProcessInfo+MGLAdditions.h" #endif #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR #import "MGLMapboxEvents.h" +#import "MBXSKUToken.h" +#endif @interface MGLAccountManager () @property (atomic) NSString *accessToken; @property (nonatomic) NSURL *apiBaseURL; -@end -#else -@interface MGLAccountManager () - -@property (atomic) NSString *accessToken; -@property (nonatomic) NSURL *apiBaseURL; +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +@property (atomic) NSString *skuToken; +@property (atomic) NSDate *skuTokenExpiration; +#endif @end -#endif @implementation MGLAccountManager @@ -39,6 +39,10 @@ if (apiBaseURL.length && [NSURL URLWithString:apiBaseURL]) { [self setAPIBaseURL:[NSURL URLWithString:apiBaseURL]]; } + +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR + self.skuToken = [MBXSKUToken skuToken]; +#endif } + (instancetype)sharedManager { @@ -93,4 +97,26 @@ return [MGLAccountManager sharedManager].apiBaseURL; } +#pragma mark - SKU Tokens + +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR + ++ (void)setSkuToken:(NSString *)skuToken { + NSTimeInterval oneHour = 60 * 60; // TODO: make this const + MGLAccountManager.sharedManager.skuTokenExpiration = [NSDate dateWithTimeIntervalSinceNow:oneHour]; + + MGLAccountManager.sharedManager.skuToken = skuToken; +} + ++ (NSString *)skuToken { + return [MGLAccountManager.sharedManager isSKUTokenExpired] ? [MBXSKUToken skuToken] : MGLAccountManager.sharedManager.skuToken; +} + +- (BOOL)isSKUTokenExpired { + NSTimeInterval secondsUntilExpiration = [MGLAccountManager.sharedManager.skuTokenExpiration timeIntervalSinceDate:NSDate.date]; + return secondsUntilExpiration < 0; +} + +#endif + @end diff --git a/platform/darwin/src/MGLAccountManager_Private.h b/platform/darwin/src/MGLAccountManager_Private.h index 4644635940..3fd45f1ae8 100644 --- a/platform/darwin/src/MGLAccountManager_Private.h +++ b/platform/darwin/src/MGLAccountManager_Private.h @@ -1,14 +1,23 @@ #import "MGLAccountManager.h" +NS_ASSUME_NONNULL_BEGIN + @interface MGLAccountManager (Private) /// Returns the shared instance of the `MGLAccountManager` class. @property (class, nonatomic, readonly) MGLAccountManager *sharedManager; /// The current global access token. -@property (atomic) NSString *accessToken; +@property (atomic, nullable) NSString *accessToken; /// The API base URL that is used to access Mapbox resources. The default base URL is `https://api.mapbox.com`. If `nil`, the Mapbox default base API URL is in use. -@property (atomic, readwrite) NSURL *apiBaseURL; +@property (atomic, readwrite, nullable) NSURL *apiBaseURL; + +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +/// The current global SKU. +@property (class, atomic, readonly) NSString *skuToken; +#endif @end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index c26568d8bc..29f8f7db68 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -7,9 +7,14 @@ #include #import + #import "MGLLoggingConfiguration_Private.h" #import "MGLNetworkConfiguration_Private.h" +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +#import "MGLAccountManager_Private.h" +#endif + #include #include @@ -196,16 +201,25 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, auto shared = request->shared; // Explicit copy so that it also gets copied into the completion handler block below. @autoreleasepool { - NSURL* url = [NSURL URLWithString:@(resource.url.c_str())]; + NSURL *url = [NSURL URLWithString:@(resource.url.c_str())]; MGLLogDebug(@"Requesting URI: %@", url.relativePath); + +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR if (impl->accountType == 0 && ([url.host isEqualToString:@"mapbox.com"] || [url.host hasSuffix:@".mapbox.com"])) { - NSString* absoluteString = [url.absoluteString - stringByAppendingFormat:(url.query ? @"&%@" : @"?%@"), @"events=true"]; - url = [NSURL URLWithString:absoluteString]; + NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO]; + NSArray *newQueryItems = @[ + [NSURLQueryItem queryItemWithName:@"events" value:@"true"], + [NSURLQueryItem queryItemWithName:@"sku" value:MGLAccountManager.skuToken] + ]; + + components.queryItems = components.queryItems ? [components.queryItems arrayByAddingObjectsFromArray:newQueryItems] : newQueryItems; + + url = components.URL; } +#endif - NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url]; + NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; if (resource.priorEtag) { [req addValue:@(resource.priorEtag->c_str()) forHTTPHeaderField:@"If-None-Match"]; diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index 42e30b1417..9a7a0dd100 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -382,6 +382,10 @@ 967C864C210A9D3C004DF794 /* UIDevice+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 967C8649210A9D3C004DF794 /* UIDevice+MGLAdditions.h */; }; 967C864D210A9D3C004DF794 /* UIDevice+MGLAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 967C864A210A9D3C004DF794 /* UIDevice+MGLAdditions.m */; }; 967C864E210A9D3C004DF794 /* UIDevice+MGLAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 967C864A210A9D3C004DF794 /* UIDevice+MGLAdditions.m */; }; + 9680273F22653B84006BA4A1 /* MBXSKUToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 9680273E22653B84006BA4A1 /* MBXSKUToken.h */; }; + 9680274022653B84006BA4A1 /* MBXSKUToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 9680273E22653B84006BA4A1 /* MBXSKUToken.h */; }; + 9680276422655696006BA4A1 /* libmbxaccounts.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9680274122653C3E006BA4A1 /* libmbxaccounts.a */; }; + 96802766226556C5006BA4A1 /* libmbxaccounts.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9680274122653C3E006BA4A1 /* libmbxaccounts.a */; }; 968F36B51E4D128D003A5522 /* MGLDistanceFormatter.h in Headers */ = {isa = PBXBuildFile; fileRef = 3557F7AE1E1D27D300CCA5E6 /* MGLDistanceFormatter.h */; settings = {ATTRIBUTES = (Public, ); }; }; 96E027231E57C76E004B8E66 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 96E027251E57C76E004B8E66 /* Localizable.strings */; }; 96E516DC2000547000A02306 /* MGLPolyline_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 9654C1251FFC1AB900DB6A19 /* MGLPolyline_Private.h */; }; @@ -1067,6 +1071,8 @@ 966FCF511F3C321000F2B6DE /* MGLUserLocationHeadingArrowLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLUserLocationHeadingArrowLayer.m; sourceTree = ""; }; 967C8649210A9D3C004DF794 /* UIDevice+MGLAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIDevice+MGLAdditions.h"; sourceTree = ""; }; 967C864A210A9D3C004DF794 /* UIDevice+MGLAdditions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+MGLAdditions.m"; sourceTree = ""; }; + 9680273E22653B84006BA4A1 /* MBXSKUToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MBXSKUToken.h; path = "../vendor/mapbox-accounts-ios/MBXSKUToken.h"; sourceTree = ""; }; + 9680274122653C3E006BA4A1 /* libmbxaccounts.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmbxaccounts.a; path = "vendor/mapbox-accounts-ios/libmbxaccounts.a"; sourceTree = SOURCE_ROOT; }; 968F36B41E4D0FC6003A5522 /* ja */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = ""; }; 96E027241E57C76E004B8E66 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = ""; }; 96E027271E57C77A004B8E66 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = ""; }; @@ -1408,6 +1414,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 96802766226556C5006BA4A1 /* libmbxaccounts.a in Frameworks */, DAABF73D1CBC59BB005B1825 /* libmbgl-core.a in Frameworks */, 55D120A61F791007004B6D81 /* libmbgl-loop-darwin.a in Frameworks */, 55D120A81F79100C004B6D81 /* libmbgl-filesource.a in Frameworks */, @@ -1421,6 +1428,7 @@ buildActionMask = 2147483647; files = ( DAAE5F8720F046E60089D85B /* libmbgl-core.a in Frameworks */, + 9680276422655696006BA4A1 /* libmbxaccounts.a in Frameworks */, DAAE5F8920F047240089D85B /* libmbgl-filesource.a in Frameworks */, DAAE5F8A20F0472E0089D85B /* libmbgl-loop-darwin.a in Frameworks */, 55CF7531213ED92A00ED86C4 /* libicu.a in Frameworks */, @@ -1772,6 +1780,14 @@ name = Fixtures; sourceTree = ""; }; + 9680276322655623006BA4A1 /* Accounts */ = { + isa = PBXGroup; + children = ( + 9680273E22653B84006BA4A1 /* MBXSKUToken.h */, + ); + name = Accounts; + sourceTree = ""; + }; CA1B4A4F2099FA2800EDD491 /* Snapshotter Tests */ = { isa = PBXGroup; children = ( @@ -1871,6 +1887,7 @@ DA1DC9921CB6DF24006E619F /* Frameworks */ = { isa = PBXGroup; children = ( + 9680274122653C3E006BA4A1 /* libmbxaccounts.a */, 55CF7530213ED92A00ED86C4 /* libicu.a */, 55CF752E213ED92000ED86C4 /* libicu.a */, 55D120A91F79100C004B6D81 /* libmbgl-filesource.a */, @@ -2248,10 +2265,11 @@ DAD165851CF4D08B001FF4B9 /* Telemetry */ = { isa = PBXGroup; children = ( + 9680276322655623006BA4A1 /* Accounts */, + 40834BA11FE05CFD00C1BD0D /* Development */, AC518DFD201BB55A00EBC820 /* MGLTelemetryConfig.h */, AC518DFE201BB55A00EBC820 /* MGLTelemetryConfig.m */, 40834BA21FE05D3100C1BD0D /* Runtime */, - 40834BA11FE05CFD00C1BD0D /* Development */, ); name = Telemetry; sourceTree = ""; @@ -2368,6 +2386,7 @@ DA35A29E1CC9E94C00E826B2 /* MGLCoordinateFormatter.h in Headers */, DAF0D8181DFE6B2800B28378 /* MGLAttributionInfo_Private.h in Headers */, DAAF722B1DA903C700312FA4 /* MGLStyleValue.h in Headers */, + 9680273F22653B84006BA4A1 /* MBXSKUToken.h in Headers */, DA8847F71CBAFA5100AB86E3 /* MGLOverlay.h in Headers */, DA35A2B11CCA141D00E826B2 /* MGLCompassDirectionFormatter.h in Headers */, DA88488B1CBB037E00AB86E3 /* SMCalloutView.h in Headers */, @@ -2500,6 +2519,7 @@ 96E516E92000560B00A02306 /* MGLAnnotationImage_Private.h in Headers */, 96E516E52000560B00A02306 /* MGLOfflinePack_Private.h in Headers */, DD9BE4F91EB263D20079A3AF /* UIViewController+MGLAdditions.h in Headers */, + 9680274022653B84006BA4A1 /* MBXSKUToken.h in Headers */, DAF2571C201901E200367EF5 /* MGLHillshadeStyleLayer.h in Headers */, 74CB5EC4219B282500102936 /* MGLCircleStyleLayer_Private.h in Headers */, DABFB8621CBE99E500D62B32 /* MGLOfflinePack.h in Headers */, @@ -3714,6 +3734,11 @@ INFOPLIST_FILE = framework/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)", + "$(PROJECT_DIR)/vendor/mapbox-accounts-ios", + ); LLVM_LTO = YES; OTHER_CFLAGS = "-fvisibility=hidden"; OTHER_CPLUSPLUSFLAGS = ( @@ -3747,6 +3772,11 @@ "$(mbgl_core_INCLUDE_DIRECTORIES)", "$(mbgl_filesource_INCLUDE_DIRECTORIES)", ); + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)", + "$(PROJECT_DIR)/vendor/mapbox-accounts-ios", + ); LLVM_LTO = YES; OTHER_CFLAGS = "-fvisibility=hidden"; OTHER_CPLUSPLUSFLAGS = ( @@ -4102,6 +4132,11 @@ INFOPLIST_FILE = framework/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)", + "$(PROJECT_DIR)/vendor/mapbox-accounts-ios", + ); OTHER_CFLAGS = "-fvisibility=hidden"; OTHER_CPLUSPLUSFLAGS = ( "$(OTHER_CFLAGS)", @@ -4145,6 +4180,11 @@ INFOPLIST_FILE = framework/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)", + "$(PROJECT_DIR)/vendor/mapbox-accounts-ios", + ); LLVM_LTO = YES; OTHER_CFLAGS = "-fvisibility=hidden"; OTHER_CPLUSPLUSFLAGS = ( @@ -4201,6 +4241,11 @@ "$(mbgl_core_INCLUDE_DIRECTORIES)", "$(mbgl_filesource_INCLUDE_DIRECTORIES)", ); + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)", + "$(PROJECT_DIR)/vendor/mapbox-accounts-ios", + ); OTHER_CFLAGS = "-fvisibility=hidden"; OTHER_CPLUSPLUSFLAGS = ( "$(OTHER_CFLAGS)", @@ -4234,6 +4279,11 @@ "$(mbgl_core_INCLUDE_DIRECTORIES)", "$(mbgl_filesource_INCLUDE_DIRECTORIES)", ); + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)", + "$(PROJECT_DIR)/vendor/mapbox-accounts-ios", + ); LLVM_LTO = YES; OTHER_CFLAGS = "-fvisibility=hidden"; OTHER_CPLUSPLUSFLAGS = ( diff --git a/platform/ios/sdk-files.json b/platform/ios/sdk-files.json index d80a734c11..3031a87fd6 100644 --- a/platform/ios/sdk-files.json +++ b/platform/ios/sdk-files.json @@ -252,6 +252,7 @@ "MGLMultiPoint_Private.h": "platform/darwin/src/MGLMultiPoint_Private.h", "UIColor+MGLAdditions.h": "platform/ios/src/UIColor+MGLAdditions.h", "MGLAttributionInfo_Private.h": "platform/darwin/src/MGLAttributionInfo_Private.h", + "MBXSKUToken.h": "platform/ios/vendor/mapbox-accounts-ios/MBXSKUToken.h", "SMCalloutView.h": "platform/ios/vendor/SMCalloutView/SMCalloutView.h", "MGLOfflineRegion_Private.h": "platform/darwin/src/MGLOfflineRegion_Private.h", "MMEPinningConfigurationProvider.h": "platform/ios/vendor/mapbox-events-ios/MapboxMobileEvents/MMEPinningConfigurationProvider.h", diff --git a/platform/ios/src/MGLMapboxEvents.m b/platform/ios/src/MGLMapboxEvents.m index eabd970edb..5cbb178c2c 100644 --- a/platform/ios/src/MGLMapboxEvents.m +++ b/platform/ios/src/MGLMapboxEvents.m @@ -1,4 +1,5 @@ #import "MGLMapboxEvents.h" +#import "MBXSKUToken.h" #import "NSBundle+MGLAdditions.h" static NSString * const MGLAPIClientUserAgentBase = @"mapbox-maps-ios"; @@ -142,6 +143,8 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; if ([MGLMapboxEvents sharedInstance].baseURL) { [[MGLMapboxEvents sharedInstance] eventsManager].baseURL = [MGLMapboxEvents sharedInstance].baseURL; } + + [[MGLMapboxEvents sharedInstance] eventsManager].skuId = MBXAccountsSKUIDMaps; [self flush]; }); diff --git a/platform/ios/vendor/mapbox-accounts-ios/MBXSKUToken.h b/platform/ios/vendor/mapbox-accounts-ios/MBXSKUToken.h new file mode 100644 index 0000000000..3a812c89ad --- /dev/null +++ b/platform/ios/vendor/mapbox-accounts-ios/MBXSKUToken.h @@ -0,0 +1,13 @@ +#import + +NS_ASSUME_NONNULL_BEGIN + +extern NSString *const MBXAccountsSKUIDMaps; + +@interface MBXSKUToken : NSObject + ++ (NSString *)skuToken; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/ios/vendor/mapbox-accounts-ios/libmbxaccounts.a b/platform/ios/vendor/mapbox-accounts-ios/libmbxaccounts.a new file mode 100644 index 0000000000..1425ca733d Binary files /dev/null and b/platform/ios/vendor/mapbox-accounts-ios/libmbxaccounts.a differ diff --git a/platform/ios/vendor/mapbox-events-ios b/platform/ios/vendor/mapbox-events-ios index c407e33571..021588900c 160000 --- a/platform/ios/vendor/mapbox-events-ios +++ b/platform/ios/vendor/mapbox-events-ios @@ -1 +1 @@ -Subproject commit c407e33571fca955b3100d964df65a84a716ab09 +Subproject commit 021588900c1f76786a2f7f4c1c6b343aa2020d19 -- cgit v1.2.1