FIRAppCheckStoredToken.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FirebaseAppCheck/Sources/Core/Storage/FIRAppCheckStoredToken.h"
  17. static NSString *const kTokenKey = @"token";
  18. static NSString *const kExpirationDateKey = @"expirationDate";
  19. static NSString *const kReceivedAtDateKey = @"receivedAtDate";
  20. static NSString *const kStorageVersionKey = @"storageVersion";
  21. static const NSInteger kStorageVersion = 2;
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation FIRAppCheckStoredToken
  24. - (NSInteger)storageVersion {
  25. return kStorageVersion;
  26. }
  27. + (BOOL)supportsSecureCoding {
  28. return YES;
  29. }
  30. - (void)encodeWithCoder:(NSCoder *)coder {
  31. [coder encodeObject:self.token forKey:kTokenKey];
  32. [coder encodeObject:self.expirationDate forKey:kExpirationDateKey];
  33. [coder encodeObject:self.receivedAtDate forKey:kReceivedAtDateKey];
  34. [coder encodeInteger:self.storageVersion forKey:kStorageVersionKey];
  35. }
  36. - (nullable instancetype)initWithCoder:(NSCoder *)coder {
  37. self = [super init];
  38. if (self) {
  39. NSInteger decodedStorageVersion = [coder decodeIntegerForKey:kStorageVersionKey];
  40. if (decodedStorageVersion > kStorageVersion) {
  41. // TODO: Log a message.
  42. }
  43. _token = [coder decodeObjectOfClass:[NSString class] forKey:kTokenKey];
  44. _expirationDate = [coder decodeObjectOfClass:[NSDate class] forKey:kExpirationDateKey];
  45. _receivedAtDate = [coder decodeObjectOfClass:[NSDate class] forKey:kReceivedAtDateKey];
  46. }
  47. return self;
  48. }
  49. @end
  50. NS_ASSUME_NONNULL_END