FIRInstallationsStoredAuthToken.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  17. #import "FirebaseInstallations/Source/Library/FIRInstallationsLogger.h"
  18. NSString *const kFIRInstallationsStoredAuthTokenStatusKey = @"status";
  19. NSString *const kFIRInstallationsStoredAuthTokenTokenKey = @"token";
  20. NSString *const kFIRInstallationsStoredAuthTokenExpirationDateKey = @"expirationDate";
  21. NSString *const kFIRInstallationsStoredAuthTokenStorageVersionKey = @"storageVersion";
  22. NSInteger const kFIRInstallationsStoredAuthTokenStorageVersion = 1;
  23. @implementation FIRInstallationsStoredAuthToken
  24. - (NSInteger)storageVersion {
  25. return kFIRInstallationsStoredAuthTokenStorageVersion;
  26. }
  27. - (nonnull id)copyWithZone:(nullable NSZone *)zone {
  28. FIRInstallationsStoredAuthToken *clone = [[FIRInstallationsStoredAuthToken alloc] init];
  29. clone.status = self.status;
  30. clone.token = [self.token copy];
  31. clone.expirationDate = self.expirationDate;
  32. return clone;
  33. }
  34. - (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
  35. [aCoder encodeInteger:self.status forKey:kFIRInstallationsStoredAuthTokenStatusKey];
  36. [aCoder encodeObject:self.token forKey:kFIRInstallationsStoredAuthTokenTokenKey];
  37. [aCoder encodeObject:self.expirationDate
  38. forKey:kFIRInstallationsStoredAuthTokenExpirationDateKey];
  39. [aCoder encodeInteger:self.storageVersion
  40. forKey:kFIRInstallationsStoredAuthTokenStorageVersionKey];
  41. }
  42. - (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
  43. NSInteger storageVersion =
  44. [aDecoder decodeIntegerForKey:kFIRInstallationsStoredAuthTokenStorageVersionKey];
  45. if (storageVersion > kFIRInstallationsStoredAuthTokenStorageVersion) {
  46. FIRLogWarning(kFIRLoggerInstallations,
  47. kFIRInstallationsMessageCodeAuthTokenCoderVersionMismatch,
  48. @"FIRInstallationsStoredAuthToken was encoded by a newer coder version %ld. "
  49. @"Current coder version is %ld. Some auth token data may be lost.",
  50. (long)storageVersion, (long)kFIRInstallationsStoredAuthTokenStorageVersion);
  51. }
  52. FIRInstallationsStoredAuthToken *object = [[FIRInstallationsStoredAuthToken alloc] init];
  53. object.status = [aDecoder decodeIntegerForKey:kFIRInstallationsStoredAuthTokenStatusKey];
  54. object.token = [aDecoder decodeObjectOfClass:[NSString class]
  55. forKey:kFIRInstallationsStoredAuthTokenTokenKey];
  56. object.expirationDate =
  57. [aDecoder decodeObjectOfClass:[NSDate class]
  58. forKey:kFIRInstallationsStoredAuthTokenExpirationDateKey];
  59. return object;
  60. }
  61. + (BOOL)supportsSecureCoding {
  62. return YES;
  63. }
  64. @end