FIRFADApiService.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseAppDistribution/Sources/FIRFADApiService.h"
  15. #import <Foundation/Foundation.h>
  16. #import "FirebaseAppDistribution/Sources/FIRFADLogger.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  19. NSString *const kFIRFADApiErrorDomain = @"com.firebase.appdistribution.api";
  20. NSString *const kFIRFADApiErrorDetailsKey = @"details";
  21. NSString *const kHTTPGet = @"GET";
  22. // The App Distribution Tester API endpoint used to retrieve releases
  23. NSString *const kReleasesEndpointURLTemplate =
  24. @"https://firebaseapptesters.googleapis.com/v1alpha/devices/"
  25. @"-/testerApps/%@/installations/%@/releases";
  26. NSString *const kInstallationAuthHeader = @"X-Goog-Firebase-Installations-Auth";
  27. NSString *const kApiHeaderKey = @"X-Goog-Api-Key";
  28. NSString *const kApiBundleKey = @"X-Ios-Bundle-Identifier";
  29. NSString *const kResponseReleasesKey = @"releases";
  30. @implementation FIRFADApiService
  31. + (void)generateAuthTokenWithCompletion:(FIRFADGenerateAuthTokenCompletion)completion {
  32. FIRInstallations *installations = [FIRInstallations installations];
  33. // Get a FIS Authentication Token.
  34. [installations authTokenWithCompletion:^(
  35. FIRInstallationsAuthTokenResult *_Nullable authTokenResult,
  36. NSError *_Nullable error) {
  37. if ([self handleError:&error
  38. description:@"Failed to generate Firebase Installation Auth Token."
  39. code:FIRFADApiTokenGenerationFailure]) {
  40. FIRFADErrorLog(@"Error getting fresh auth tokens. Error: %@", [error localizedDescription]);
  41. completion(nil, nil, error);
  42. return;
  43. }
  44. [installations installationIDWithCompletion:^(NSString *__nullable identifier,
  45. NSError *__nullable error) {
  46. if ([self handleError:&error
  47. description:@"Failed to fetch Firebase Installation ID."
  48. code:FIRFADApiInstallationIdentifierError]) {
  49. FIRFADErrorLog(@"Error getting installation id. Error: %@", [error localizedDescription]);
  50. completion(nil, nil, error);
  51. return;
  52. }
  53. completion(identifier, authTokenResult, nil);
  54. }];
  55. }];
  56. }
  57. + (NSMutableURLRequest *)createHTTPRequest:(NSString *)method
  58. withUrl:(NSString *)urlString
  59. withAuthToken:(FIRInstallationsAuthTokenResult *)authTokenResult {
  60. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  61. FIRFADInfoLog(@"Requesting releases for app id - %@", [[FIRApp defaultApp] options].googleAppID);
  62. [request setURL:[NSURL URLWithString:urlString]];
  63. [request setHTTPMethod:method];
  64. [request setValue:authTokenResult.authToken forHTTPHeaderField:kInstallationAuthHeader];
  65. [request setValue:[[FIRApp defaultApp] options].APIKey forHTTPHeaderField:kApiHeaderKey];
  66. [request setValue:[NSBundle mainBundle].bundleIdentifier forHTTPHeaderField:kApiBundleKey];
  67. return request;
  68. }
  69. + (NSArray *)handleReleaseResponse:(NSData *)data
  70. response:(NSURLResponse *)response
  71. error:(NSError **_Nullable)error {
  72. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  73. FIRFADInfoLog(@"HTTPResonse status code %ld response %@", (long)[httpResponse statusCode],
  74. httpResponse);
  75. if ([self handleHttpResponseError:httpResponse error:error]) {
  76. FIRFADErrorLog(@"App Tester API service error - %@", [*error localizedDescription]);
  77. return nil;
  78. }
  79. return [self parseApiResponseWithData:data error:error];
  80. }
  81. + (void)fetchReleasesWithCompletion:(FIRFADFetchReleasesCompletion)completion {
  82. void (^executeFetch)(NSString *_Nullable, FIRInstallationsAuthTokenResult *, NSError *_Nullable) =
  83. ^(NSString *_Nullable identifier, FIRInstallationsAuthTokenResult *authTokenResult,
  84. NSError *_Nullable error) {
  85. NSString *urlString =
  86. [NSString stringWithFormat:kReleasesEndpointURLTemplate,
  87. [[FIRApp defaultApp] options].googleAppID, identifier];
  88. NSMutableURLRequest *request = [self createHTTPRequest:@"GET"
  89. withUrl:urlString
  90. withAuthToken:authTokenResult];
  91. FIRFADInfoLog(@"Url : %@, Auth token: %@ API KEY: %@", urlString, authTokenResult.authToken,
  92. [[FIRApp defaultApp] options].APIKey);
  93. NSURLSessionDataTask *listReleasesDataTask = [[NSURLSession sharedSession]
  94. dataTaskWithRequest:request
  95. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  96. NSArray *releases = [self handleReleaseResponse:data
  97. response:response
  98. error:&error];
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. completion(releases, error);
  101. });
  102. }];
  103. [listReleasesDataTask resume];
  104. };
  105. [self generateAuthTokenWithCompletion:executeFetch];
  106. }
  107. + (BOOL)handleHttpResponseError:(NSHTTPURLResponse *)httpResponse error:(NSError **_Nullable)error {
  108. if (*error || !httpResponse) {
  109. return [self handleError:error
  110. description:@"Unknown http error occurred"
  111. code:FIRApiErrorUnknownFailure];
  112. ;
  113. }
  114. if ([httpResponse statusCode] != 200) {
  115. *error = [self createErrorFromStatusCode:[httpResponse statusCode]];
  116. return YES;
  117. }
  118. return NO;
  119. }
  120. + (NSError *)createErrorFromStatusCode:(NSInteger)statusCode {
  121. if (statusCode == 401) {
  122. return [self createErrorWithDescription:@"Tester not authenticated."
  123. code:FIRFADApiErrorUnauthenticated];
  124. }
  125. if (statusCode == 403 || statusCode == 400) {
  126. return [self createErrorWithDescription:@"Tester not authorized."
  127. code:FIRFADApiErrorUnauthorized];
  128. }
  129. if (statusCode == 404) {
  130. return [self createErrorWithDescription:@"Tester or releases not found"
  131. code:FIRFADApiErrorUnauthorized];
  132. }
  133. if (statusCode == 408 || statusCode == 504) {
  134. return [self createErrorWithDescription:@"Request timeout." code:FIRFADApiErrorTimeout];
  135. }
  136. FIRFADErrorLog(@"Encountered unmapped status code: %ld", (long)statusCode);
  137. NSString *description = [NSString stringWithFormat:@"Unknown status code: %ld", (long)statusCode];
  138. return [self createErrorWithDescription:description code:FIRApiErrorUnknownFailure];
  139. }
  140. + (BOOL)handleError:(NSError **_Nullable)error
  141. description:(NSString *)description
  142. code:(FIRFADApiError)code {
  143. if (*error) {
  144. *error = [self createErrorWithDescription:description code:code];
  145. return YES;
  146. }
  147. return NO;
  148. }
  149. + (NSError *)createErrorWithDescription:description code:(FIRFADApiError)code {
  150. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description};
  151. return [NSError errorWithDomain:kFIRFADApiErrorDomain code:code userInfo:userInfo];
  152. }
  153. + (NSArray *_Nullable)parseApiResponseWithData:(NSData *)data error:(NSError **_Nullable)error {
  154. NSDictionary *serializedResponse = [NSJSONSerialization JSONObjectWithData:data
  155. options:0
  156. error:error];
  157. if (*error) {
  158. FIRFADErrorLog(@"Tester API - Error deserializing json response");
  159. NSString *description = (*error).userInfo[NSLocalizedDescriptionKey]
  160. ? (*error).userInfo[NSLocalizedDescriptionKey]
  161. : @"Failed to parse response";
  162. [self handleError:error description:description code:FIRApiErrorParseFailure];
  163. return nil;
  164. }
  165. NSArray *releases = [serializedResponse objectForKey:kResponseReleasesKey];
  166. return releases;
  167. }
  168. @end