FIRAppAttestStoredArtifact.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2021 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/AppAttestProvider/Storage/FIRAppAttestStoredArtifact.h"
  17. static NSString *const kKeyIDKey = @"keyID";
  18. static NSString *const kArtifactKey = @"artifact";
  19. static NSString *const kStorageVersionKey = @"storageVersion";
  20. static NSInteger const kStorageVersion = 1;
  21. @implementation FIRAppAttestStoredArtifact
  22. - (instancetype)initWithKeyID:(NSString *)keyID artifact:(NSData *)artifact {
  23. self = [super init];
  24. if (self) {
  25. _keyID = keyID;
  26. _artifact = artifact;
  27. }
  28. return self;
  29. }
  30. - (NSInteger)storageVersion {
  31. return kStorageVersion;
  32. }
  33. #pragma mark - NSSecureCoding
  34. + (BOOL)supportsSecureCoding {
  35. return YES;
  36. }
  37. - (void)encodeWithCoder:(nonnull NSCoder *)coder {
  38. [coder encodeObject:self.keyID forKey:kKeyIDKey];
  39. [coder encodeObject:self.artifact forKey:kArtifactKey];
  40. [coder encodeInteger:self.storageVersion forKey:kStorageVersionKey];
  41. }
  42. - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
  43. NSInteger storageVersion = [coder decodeIntegerForKey:kStorageVersionKey];
  44. if (storageVersion < kStorageVersion) {
  45. // Handle migration here when new versions are added
  46. }
  47. // If the version of the stored object is equal or higher than the current version then try the
  48. // best to get enough data to initialize the object.
  49. NSString *keyID = [coder decodeObjectOfClass:[NSString class] forKey:kKeyIDKey];
  50. if (keyID.length < 1) {
  51. return nil;
  52. }
  53. NSData *artifact = [coder decodeObjectOfClass:[NSData class] forKey:kArtifactKey];
  54. if (artifact.length < 1) {
  55. return nil;
  56. }
  57. return [self initWithKeyID:keyID artifact:artifact];
  58. }
  59. @end