FIRInstallationsItem.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. @implementation FIRInstallationsItem
  20. - (instancetype)initWithAppID:(NSString *)appID firebaseAppName:(NSString *)firebaseAppName {
  21. self = [super init];
  22. if (self) {
  23. _appID = [appID copy];
  24. _firebaseAppName = [firebaseAppName copy];
  25. }
  26. return self;
  27. }
  28. - (nonnull id)copyWithZone:(nullable NSZone *)zone {
  29. FIRInstallationsItem *clone = [[FIRInstallationsItem alloc] initWithAppID:self.appID
  30. firebaseAppName:self.firebaseAppName];
  31. clone.firebaseInstallationID = [self.firebaseInstallationID copy];
  32. clone.refreshToken = [self.refreshToken copy];
  33. clone.authToken = [self.authToken copy];
  34. clone.registrationStatus = self.registrationStatus;
  35. return clone;
  36. }
  37. - (void)updateWithStoredItem:(FIRInstallationsStoredItem *)item {
  38. self.firebaseInstallationID = item.firebaseInstallationID;
  39. self.refreshToken = item.refreshToken;
  40. self.authToken = item.authToken;
  41. self.registrationStatus = item.registrationStatus;
  42. }
  43. - (FIRInstallationsStoredItem *)storedItem {
  44. FIRInstallationsStoredItem *storedItem = [[FIRInstallationsStoredItem alloc] init];
  45. storedItem.firebaseInstallationID = self.firebaseInstallationID;
  46. storedItem.refreshToken = self.refreshToken;
  47. storedItem.authToken = self.authToken;
  48. storedItem.registrationStatus = self.registrationStatus;
  49. return storedItem;
  50. }
  51. - (nonnull NSString *)identifier {
  52. return [[self class] identifierWithAppID:self.appID appName:self.firebaseAppName];
  53. }
  54. + (NSString *)identifierWithAppID:(NSString *)appID appName:(NSString *)appName {
  55. return [appID stringByAppendingString:appName];
  56. }
  57. + (NSString *)generateFID {
  58. NSUUID *UUID = [NSUUID UUID];
  59. uuid_t UUIDBytes;
  60. [UUID getUUIDBytes:UUIDBytes];
  61. NSUInteger UUIDLength = sizeof(uuid_t);
  62. NSData *UUIDData = [NSData dataWithBytes:UUIDBytes length:UUIDLength];
  63. uint8_t UUIDLast4Bits = UUIDBytes[UUIDLength - 1] & 0b00001111;
  64. // FID first 4 bits must be `0111`. The last 4 UUID bits will be cut later to form a proper FID.
  65. // To keep 16 random bytes we copy these last 4 UUID to the FID 1st byte after `0111` prefix.
  66. uint8_t FIDPrefix = 0b01110000 | UUIDLast4Bits;
  67. NSMutableData *FIDData = [NSMutableData dataWithBytes:&FIDPrefix length:1];
  68. [FIDData appendData:UUIDData];
  69. NSString *FIDString = [self base64URLEncodedStringWithData:FIDData];
  70. // A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
  71. // Our generated ID has 16 bytes UUID + 1 byte prefix which after encoding with base64 will become
  72. // 23 characters plus 1 character for "=" padding.
  73. // Remove the 23rd character that was added because of the extra 4 bits at the
  74. // end of our 17 byte data and the '=' padding.
  75. return [FIDString substringWithRange:NSMakeRange(0, 22)];
  76. }
  77. + (NSString *)base64URLEncodedStringWithData:(NSData *)data {
  78. NSString *string = [data base64EncodedStringWithOptions:0];
  79. string = [string stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
  80. string = [string stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
  81. return string;
  82. }
  83. @end