FIRFADApiService.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. + (NSString *)tryParseGoogleAPIErrorFromResponse:(NSData *)data {
  70. NSError *parseError;
  71. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
  72. options:0
  73. error:&parseError];
  74. if (parseError) {
  75. return @"Could not parse additional details about this API error.";
  76. } else {
  77. NSDictionary *errorDict = [responseDict objectForKey:@"error"];
  78. if (!errorDict) {
  79. return @"Could not parse additional details about this API error.";
  80. }
  81. NSString *message = [errorDict objectForKey:@"message"];
  82. if (!message) {
  83. return @"Could not parse additional details about this API error.";
  84. }
  85. return message;
  86. }
  87. }
  88. + (NSArray *)handleReleaseResponse:(NSData *)data
  89. response:(NSURLResponse *)response
  90. error:(NSError **_Nullable)error {
  91. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  92. FIRFADInfoLog(@"HTTPResonse status code %ld response %@", (long)[httpResponse statusCode],
  93. httpResponse);
  94. if ([self handleHttpResponseError:httpResponse error:error]) {
  95. FIRFADErrorLog(@"App Tester API service error: %@. %@", [*error localizedDescription],
  96. [self tryParseGoogleAPIErrorFromResponse:data]);
  97. return nil;
  98. }
  99. return [self parseApiResponseWithData:data error:error];
  100. }
  101. + (void)fetchReleasesWithCompletion:(FIRFADFetchReleasesCompletion)completion {
  102. void (^executeFetch)(NSString *_Nullable, FIRInstallationsAuthTokenResult *, NSError *_Nullable) =
  103. ^(NSString *_Nullable identifier, FIRInstallationsAuthTokenResult *authTokenResult,
  104. NSError *_Nullable error) {
  105. NSString *urlString =
  106. [NSString stringWithFormat:kReleasesEndpointURLTemplate,
  107. [[FIRApp defaultApp] options].googleAppID, identifier];
  108. NSMutableURLRequest *request = [self createHTTPRequest:@"GET"
  109. withUrl:urlString
  110. withAuthToken:authTokenResult];
  111. FIRFADInfoLog(@"Url : %@, Auth token: %@ API KEY: %@", urlString, authTokenResult.authToken,
  112. [[FIRApp defaultApp] options].APIKey);
  113. NSURLSessionDataTask *listReleasesDataTask = [[NSURLSession sharedSession]
  114. dataTaskWithRequest:request
  115. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  116. NSArray *releases = [self handleReleaseResponse:data
  117. response:response
  118. error:&error];
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. completion(releases, error);
  121. });
  122. }];
  123. [listReleasesDataTask resume];
  124. };
  125. [self generateAuthTokenWithCompletion:executeFetch];
  126. }
  127. + (BOOL)handleHttpResponseError:(NSHTTPURLResponse *)httpResponse error:(NSError **_Nullable)error {
  128. if (*error || !httpResponse) {
  129. return [self handleError:error
  130. description:@"Unknown http error occurred"
  131. code:FIRApiErrorUnknownFailure];
  132. ;
  133. }
  134. if ([httpResponse statusCode] != 200) {
  135. *error = [self createErrorFromStatusCode:[httpResponse statusCode]];
  136. return YES;
  137. }
  138. return NO;
  139. }
  140. + (NSError *)createErrorFromStatusCode:(NSInteger)statusCode {
  141. if (statusCode == 401) {
  142. return [self createErrorWithDescription:@"Tester not authenticated"
  143. code:FIRFADApiErrorUnauthenticated];
  144. }
  145. if (statusCode == 403 || statusCode == 400) {
  146. return [self createErrorWithDescription:@"Tester not authorized"
  147. code:FIRFADApiErrorUnauthorized];
  148. }
  149. if (statusCode == 404) {
  150. return [self createErrorWithDescription:@"Tester or releases not found"
  151. code:FIRFADApiErrorUnauthorized];
  152. }
  153. if (statusCode == 408 || statusCode == 504) {
  154. return [self createErrorWithDescription:@"Request timeout" code:FIRFADApiErrorTimeout];
  155. }
  156. FIRFADErrorLog(@"Encountered unmapped status code: %ld", (long)statusCode);
  157. NSString *description = [NSString stringWithFormat:@"Unknown status code: %ld", (long)statusCode];
  158. return [self createErrorWithDescription:description code:FIRApiErrorUnknownFailure];
  159. }
  160. + (BOOL)handleError:(NSError **_Nullable)error
  161. description:(NSString *)description
  162. code:(FIRFADApiError)code {
  163. if (*error) {
  164. *error = [self createErrorWithDescription:description code:code];
  165. return YES;
  166. }
  167. return NO;
  168. }
  169. + (NSError *)createErrorWithDescription:description code:(FIRFADApiError)code {
  170. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description};
  171. return [NSError errorWithDomain:kFIRFADApiErrorDomain code:code userInfo:userInfo];
  172. }
  173. + (NSArray *_Nullable)parseApiResponseWithData:(NSData *)data error:(NSError **_Nullable)error {
  174. NSDictionary *serializedResponse = [NSJSONSerialization JSONObjectWithData:data
  175. options:0
  176. error:error];
  177. if (*error) {
  178. FIRFADErrorLog(@"Tester API - Error deserializing json response");
  179. NSString *description = (*error).userInfo[NSLocalizedDescriptionKey]
  180. ? (*error).userInfo[NSLocalizedDescriptionKey]
  181. : @"Failed to parse response";
  182. [self handleError:error description:description code:FIRApiErrorParseFailure];
  183. return nil;
  184. }
  185. NSArray *releases = [serializedResponse objectForKey:kResponseReleasesKey];
  186. return releases;
  187. }
  188. @end