FIRInstallationsItem.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. NSData *uuidData = [NSData dataWithBytes:uuidBytes length:16];
  62. uint8_t prefix = 0b01110000;
  63. NSMutableData *fidData = [NSMutableData dataWithBytes:&prefix length:1];
  64. [fidData appendData:uuidData];
  65. NSString *fidString = [self base64URLEncodedStringWithData:fidData];
  66. // TODO: Consider implementation which does not modify UUID.
  67. // A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
  68. // Our generated ID has 16 bytes UUID + 1 byte prefix which after encoding with base64 will become
  69. // 23 characters plus 1 character for "=" padding.
  70. // Remove the 23rd character that was added because of the extra 4 bits at the
  71. // end of our 17 byte data. It should be pretty safe to do because UUID ends with the random part,
  72. // so we will not affect probability of the collisions much.
  73. // Also remove the '=' padding.
  74. return [fidString substringWithRange:NSMakeRange(0, 22)];
  75. }
  76. + (NSString *)base64URLEncodedStringWithData:(NSData *)data {
  77. NSString *string = [data base64EncodedStringWithOptions:0];
  78. string = [string stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
  79. string = [string stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
  80. return string;
  81. }
  82. @end