FIRInstallationsItem.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "FIRInstallationsItem.h"
  17. #import "FIRInstallationsStoredAuthToken.h"
  18. #import "FIRInstallationsStoredItem.h"
  19. #import "FIRInstallationsStoredRegistrationError.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. return clone;
  37. }
  38. - (void)updateWithStoredItem:(FIRInstallationsStoredItem *)item {
  39. self.firebaseInstallationID = item.firebaseInstallationID;
  40. self.refreshToken = item.refreshToken;
  41. self.authToken = item.authToken;
  42. self.registrationStatus = item.registrationStatus;
  43. self.registrationError = item.registrationError;
  44. }
  45. - (FIRInstallationsStoredItem *)storedItem {
  46. FIRInstallationsStoredItem *storedItem = [[FIRInstallationsStoredItem alloc] init];
  47. storedItem.firebaseInstallationID = self.firebaseInstallationID;
  48. storedItem.refreshToken = self.refreshToken;
  49. storedItem.authToken = self.authToken;
  50. storedItem.registrationStatus = self.registrationStatus;
  51. storedItem.registrationError = self.registrationError;
  52. return storedItem;
  53. }
  54. - (nonnull NSString *)identifier {
  55. return [[self class] identifierWithAppID:self.appID appName:self.firebaseAppName];
  56. }
  57. + (NSString *)identifierWithAppID:(NSString *)appID appName:(NSString *)appName {
  58. return [appID stringByAppendingString:appName];
  59. }
  60. + (NSString *)generateFID {
  61. NSUUID *UUID = [NSUUID UUID];
  62. uuid_t UUIDBytes;
  63. [UUID getUUIDBytes:UUIDBytes];
  64. NSUInteger UUIDLength = sizeof(uuid_t);
  65. NSData *UUIDData = [NSData dataWithBytes:UUIDBytes length:UUIDLength];
  66. uint8_t UUIDLast4Bits = UUIDBytes[UUIDLength - 1] & 0b00001111;
  67. // FID first 4 bits must be `0111`. The last 4 UUID bits will be cut later to form a proper FID.
  68. // To keep 16 random bytes we copy these last 4 UUID to the FID 1st byte after `0111` prefix.
  69. uint8_t FIDPrefix = 0b01110000 | UUIDLast4Bits;
  70. NSMutableData *FIDData = [NSMutableData dataWithBytes:&FIDPrefix length:1];
  71. [FIDData appendData:UUIDData];
  72. NSString *FIDString = [self base64URLEncodedStringWithData:FIDData];
  73. // A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
  74. // Our generated ID has 16 bytes UUID + 1 byte prefix which after encoding with base64 will become
  75. // 23 characters plus 1 character for "=" padding.
  76. // Remove the 23rd character that was added because of the extra 4 bits at the
  77. // end of our 17 byte data and the '=' padding.
  78. return [FIDString substringWithRange:NSMakeRange(0, 22)];
  79. }
  80. + (NSString *)base64URLEncodedStringWithData:(NSData *)data {
  81. NSString *string = [data base64EncodedStringWithOptions:0];
  82. string = [string stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
  83. string = [string stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
  84. return string;
  85. }
  86. - (void)updateWithRegistrationError:(NSError *)error
  87. date:(NSDate *)date
  88. registrationParameters:
  89. (FIRInstallationsStoredRegistrationParameters *)registrationParameters {
  90. self.registrationStatus = FIRInstallationStatusRegistrationFailed;
  91. self.registrationError = [[FIRInstallationsStoredRegistrationError alloc]
  92. initWithRegistrationParameters:registrationParameters
  93. date:date
  94. APIError:error];
  95. self.authToken = nil;
  96. self.refreshToken = nil;
  97. }
  98. @end