FIRInstallationsItem+RegisterInstallationAPI.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseInstallations/Source/Library/InstallationsAPI/FIRInstallationsItem+RegisterInstallationAPI.h"
  17. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  18. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  19. @implementation FIRInstallationsItem (RegisterInstallationAPI)
  20. - (nullable FIRInstallationsItem *)
  21. registeredInstallationWithJSONData:(NSData *)data
  22. date:(NSDate *)date
  23. error:(NSError *__autoreleasing _Nullable *_Nullable)outError {
  24. NSDictionary *responseJSON = [FIRInstallationsItem dictionaryFromJSONData:data error:outError];
  25. if (!responseJSON) {
  26. return nil;
  27. }
  28. NSString *refreshToken = [FIRInstallationsItem validStringOrNilForKey:@"refreshToken"
  29. fromDict:responseJSON];
  30. if (refreshToken == nil) {
  31. FIRInstallationsItemSetErrorToPointer(
  32. [FIRInstallationsErrorUtil FIDRegistrationErrorWithResponseMissingField:@"refreshToken"],
  33. outError);
  34. return nil;
  35. }
  36. NSDictionary *authTokenDict = responseJSON[@"authToken"];
  37. if (![authTokenDict isKindOfClass:[NSDictionary class]]) {
  38. FIRInstallationsItemSetErrorToPointer(
  39. [FIRInstallationsErrorUtil FIDRegistrationErrorWithResponseMissingField:@"authToken"],
  40. outError);
  41. return nil;
  42. }
  43. FIRInstallationsStoredAuthToken *authToken =
  44. [FIRInstallationsItem authTokenWithJSONDict:authTokenDict date:date error:outError];
  45. if (authToken == nil) {
  46. return nil;
  47. }
  48. FIRInstallationsItem *installation =
  49. [[FIRInstallationsItem alloc] initWithAppID:self.appID firebaseAppName:self.firebaseAppName];
  50. NSString *installationID = [FIRInstallationsItem validStringOrNilForKey:@"fid"
  51. fromDict:responseJSON];
  52. installation.firebaseInstallationID = installationID ?: self.firebaseInstallationID;
  53. installation.refreshToken = refreshToken;
  54. installation.authToken = authToken;
  55. installation.registrationStatus = FIRInstallationStatusRegistered;
  56. return installation;
  57. }
  58. #pragma mark - Auth token
  59. + (nullable FIRInstallationsStoredAuthToken *)authTokenWithGenerateTokenAPIJSONData:(NSData *)data
  60. date:(NSDate *)date
  61. error:(NSError **)
  62. outError {
  63. NSDictionary *dict = [self dictionaryFromJSONData:data error:outError];
  64. if (!dict) {
  65. return nil;
  66. }
  67. return [self authTokenWithJSONDict:dict date:date error:outError];
  68. }
  69. + (nullable FIRInstallationsStoredAuthToken *)authTokenWithJSONDict:
  70. (NSDictionary<NSString *, id> *)dict
  71. date:(NSDate *)date
  72. error:(NSError **)outError {
  73. NSString *token = [self validStringOrNilForKey:@"token" fromDict:dict];
  74. if (token == nil) {
  75. FIRInstallationsItemSetErrorToPointer(
  76. [FIRInstallationsErrorUtil FIDRegistrationErrorWithResponseMissingField:@"authToken.token"],
  77. outError);
  78. return nil;
  79. }
  80. NSString *expiresInString = [self validStringOrNilForKey:@"expiresIn" fromDict:dict];
  81. if (expiresInString == nil) {
  82. FIRInstallationsItemSetErrorToPointer(
  83. [FIRInstallationsErrorUtil
  84. FIDRegistrationErrorWithResponseMissingField:@"authToken.expiresIn"],
  85. outError);
  86. return nil;
  87. }
  88. // The response should contain the string in format like "604800s".
  89. // The server should never response with anything else except seconds.
  90. // Just drop the last character and parse a number from string.
  91. NSString *expiresInSeconds = [expiresInString substringToIndex:expiresInString.length - 1];
  92. NSTimeInterval expiresIn = [expiresInSeconds doubleValue];
  93. NSDate *expirationDate = [date dateByAddingTimeInterval:expiresIn];
  94. FIRInstallationsStoredAuthToken *authToken = [[FIRInstallationsStoredAuthToken alloc] init];
  95. authToken.status = FIRInstallationsAuthTokenStatusTokenReceived;
  96. authToken.token = token;
  97. authToken.expirationDate = expirationDate;
  98. return authToken;
  99. }
  100. #pragma mark - JSON
  101. + (nullable NSDictionary<NSString *, id> *)dictionaryFromJSONData:(NSData *)data
  102. error:(NSError **)outError {
  103. NSError *error;
  104. NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  105. if (![responseJSON isKindOfClass:[NSDictionary class]]) {
  106. FIRInstallationsItemSetErrorToPointer([FIRInstallationsErrorUtil JSONSerializationError:error],
  107. outError);
  108. return nil;
  109. }
  110. return responseJSON;
  111. }
  112. + (NSString *)validStringOrNilForKey:(NSString *)key fromDict:(NSDictionary *)dict {
  113. NSString *string = dict[key];
  114. if ([string isKindOfClass:[NSString class]] && string.length > 0) {
  115. return string;
  116. }
  117. return nil;
  118. }
  119. @end