FIRInstallationsItem.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/FIRInstallationsItem.h"
  17. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  18. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredItem.h"
  19. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  20. @implementation FIRInstallationsItem
  21. - (instancetype)initWithAppID:(NSString *)appID firebaseAppName:(NSString *)firebaseAppName {
  22. self = [super init];
  23. if (self) {
  24. _appID = [appID copy];
  25. _firebaseAppName = [firebaseAppName copy];
  26. }
  27. return self;
  28. }
  29. - (nonnull id)copyWithZone:(nullable NSZone *)zone {
  30. FIRInstallationsItem *clone = [[FIRInstallationsItem alloc] initWithAppID:self.appID
  31. firebaseAppName:self.firebaseAppName];
  32. clone.firebaseInstallationID = [self.firebaseInstallationID copy];
  33. clone.refreshToken = [self.refreshToken copy];
  34. clone.authToken = [self.authToken copy];
  35. clone.registrationStatus = self.registrationStatus;
  36. clone.IIDDefaultToken = [self.IIDDefaultToken copy];
  37. return clone;
  38. }
  39. - (void)updateWithStoredItem:(FIRInstallationsStoredItem *)item {
  40. self.firebaseInstallationID = item.firebaseInstallationID;
  41. self.refreshToken = item.refreshToken;
  42. self.authToken = item.authToken;
  43. self.registrationStatus = item.registrationStatus;
  44. self.IIDDefaultToken = item.IIDDefaultToken;
  45. }
  46. - (FIRInstallationsStoredItem *)storedItem {
  47. FIRInstallationsStoredItem *storedItem = [[FIRInstallationsStoredItem alloc] init];
  48. storedItem.firebaseInstallationID = self.firebaseInstallationID;
  49. storedItem.refreshToken = self.refreshToken;
  50. storedItem.authToken = self.authToken;
  51. storedItem.registrationStatus = self.registrationStatus;
  52. storedItem.IIDDefaultToken = self.IIDDefaultToken;
  53. return storedItem;
  54. }
  55. - (nonnull NSString *)identifier {
  56. return [[self class] identifierWithAppID:self.appID appName:self.firebaseAppName];
  57. }
  58. - (BOOL)isValid:(NSError *_Nullable *)outError {
  59. NSMutableArray<NSString *> *validationIssues = [NSMutableArray array];
  60. if (self.appID.length == 0) {
  61. [validationIssues addObject:@"`appID` must not be empty"];
  62. }
  63. if (self.firebaseAppName.length == 0) {
  64. [validationIssues addObject:@"`firebaseAppName` must not be empty"];
  65. }
  66. if (self.firebaseInstallationID.length == 0) {
  67. [validationIssues addObject:@"`firebaseInstallationID` must not be empty"];
  68. }
  69. switch (self.registrationStatus) {
  70. case FIRInstallationStatusUnknown:
  71. [validationIssues addObject:@"invalid `registrationStatus`"];
  72. break;
  73. case FIRInstallationStatusRegistered:
  74. if (self.refreshToken == 0) {
  75. [validationIssues addObject:@"registered installation must have non-empty `refreshToken`"];
  76. }
  77. if (self.authToken.token == 0) {
  78. [validationIssues
  79. addObject:@"registered installation must have non-empty `authToken.token`"];
  80. }
  81. if (self.authToken.expirationDate == nil) {
  82. [validationIssues
  83. addObject:@"registered installation must have non-empty `authToken.expirationDate`"];
  84. }
  85. case FIRInstallationStatusUnregistered:
  86. break;
  87. }
  88. BOOL isValid = validationIssues.count == 0;
  89. if (!isValid && outError) {
  90. NSString *failureReason =
  91. [NSString stringWithFormat:@"FIRInstallationsItem validation errors: %@",
  92. [validationIssues componentsJoinedByString:@", "]];
  93. *outError =
  94. [FIRInstallationsErrorUtil installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  95. failureReason:failureReason
  96. underlyingError:nil];
  97. }
  98. return isValid;
  99. }
  100. + (NSString *)identifierWithAppID:(NSString *)appID appName:(NSString *)appName {
  101. return [appID stringByAppendingString:appName];
  102. }
  103. + (NSString *)generateFID {
  104. NSUUID *UUID = [NSUUID UUID];
  105. uuid_t UUIDBytes;
  106. [UUID getUUIDBytes:UUIDBytes];
  107. NSUInteger UUIDLength = sizeof(uuid_t);
  108. NSData *UUIDData = [NSData dataWithBytes:UUIDBytes length:UUIDLength];
  109. uint8_t UUIDLast4Bits = UUIDBytes[UUIDLength - 1] & 0b00001111;
  110. // FID first 4 bits must be `0111`. The last 4 UUID bits will be cut later to form a proper FID.
  111. // To keep 16 random bytes we copy these last 4 UUID to the FID 1st byte after `0111` prefix.
  112. uint8_t FIDPrefix = 0b01110000 | UUIDLast4Bits;
  113. NSMutableData *FIDData = [NSMutableData dataWithBytes:&FIDPrefix length:1];
  114. [FIDData appendData:UUIDData];
  115. NSString *FIDString = [self base64URLEncodedStringWithData:FIDData];
  116. // A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
  117. // Our generated ID has 16 bytes UUID + 1 byte prefix which after encoding with base64 will become
  118. // 23 characters plus 1 character for "=" padding.
  119. // Remove the 23rd character that was added because of the extra 4 bits at the
  120. // end of our 17 byte data and the '=' padding.
  121. return [FIDString substringWithRange:NSMakeRange(0, 22)];
  122. }
  123. + (NSString *)base64URLEncodedStringWithData:(NSData *)data {
  124. NSString *string = [data base64EncodedStringWithOptions:0];
  125. string = [string stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
  126. string = [string stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
  127. return string;
  128. }
  129. @end