diff options
author | Jason Wray <friedbunny@users.noreply.github.com> | 2019-04-17 18:27:54 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-17 18:27:54 -0700 |
commit | d5569f4b01ea82ebb9221ef9ed9df71ba8ba5dd9 (patch) | |
tree | a0d3b6b471b0309ce149ba14d998ae035233e45c /platform/darwin/src | |
parent | b3b62f4d5abd5bc779e564cc71e5adf0ccc924cc (diff) | |
download | qtlocation-mapboxgl-d5569f4b01ea82ebb9221ef9ed9df71ba8ba5dd9.tar.gz |
[ios] Add SKU tokens to all Mapbox requests (#14421)
Diffstat (limited to 'platform/darwin/src')
-rw-r--r-- | platform/darwin/src/MGLAccountManager.m | 40 | ||||
-rw-r--r-- | platform/darwin/src/MGLAccountManager_Private.h | 13 | ||||
-rw-r--r-- | platform/darwin/src/http_file_source.mm | 24 |
3 files changed, 63 insertions, 14 deletions
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 <mbgl/util/version.hpp> #import <Foundation/Foundation.h> + #import "MGLLoggingConfiguration_Private.h" #import "MGLNetworkConfiguration_Private.h" +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +#import "MGLAccountManager_Private.h" +#endif + #include <mutex> #include <chrono> @@ -196,16 +201,25 @@ std::unique_ptr<AsyncRequest> 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"]; |