FIRAppAttestKeyIDStorage.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/FIRAppAttestKeyIDStorage.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  23. /// The `NSUserDefaults` suite name for the storage location of the app attest key ID.
  24. static NSString *const kKeyIDStorageDefaultsSuiteName = @"com.firebase.FIRAppAttestKeyIDStorage";
  25. @interface FIRAppAttestKeyIDStorage ()
  26. @property(nonatomic, readonly) NSString *appName;
  27. @property(nonatomic, readonly) NSString *appID;
  28. /// The app attest key ID is stored using `NSUserDefaults` .
  29. @property(nonatomic, readonly) NSUserDefaults *userDefaults;
  30. @end
  31. @implementation FIRAppAttestKeyIDStorage
  32. - (instancetype)initWithAppName:(NSString *)appName appID:(NSString *)appID {
  33. self = [super init];
  34. if (self) {
  35. _appName = [appName copy];
  36. _appID = [appID copy];
  37. _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:kKeyIDStorageDefaultsSuiteName];
  38. }
  39. return self;
  40. }
  41. - (nonnull FBLPromise<NSString *> *)setAppAttestKeyID:(nullable NSString *)keyID {
  42. [self storeAppAttestKeyID:keyID];
  43. return [FBLPromise resolvedWith:keyID];
  44. }
  45. - (nonnull FBLPromise<NSString *> *)getAppAttestKeyID {
  46. NSString *appAttestKeyID = [self appAttestKeyIDFromStorage];
  47. if (appAttestKeyID) {
  48. return [FBLPromise resolvedWith:appAttestKeyID];
  49. } else {
  50. NSError *error = [FIRAppCheckErrorUtil appAttestKeyIDNotFound];
  51. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  52. [rejectedPromise reject:error];
  53. return rejectedPromise;
  54. }
  55. }
  56. #pragma mark - Helpers
  57. - (void)storeAppAttestKeyID:(nullable NSString *)keyID {
  58. if (keyID) {
  59. [self.userDefaults setObject:keyID forKey:[self keyIDStorageKey]];
  60. } else {
  61. [self.userDefaults removeObjectForKey:[self keyIDStorageKey]];
  62. }
  63. }
  64. - (nullable NSString *)appAttestKeyIDFromStorage {
  65. NSString *appAttestKeyID = nil;
  66. appAttestKeyID = [self.userDefaults objectForKey:[self keyIDStorageKey]];
  67. return appAttestKeyID;
  68. }
  69. - (NSString *)keyIDStorageKey {
  70. return [[self class] keyIDStorageKeyForAppName:self.appName appID:self.appID];
  71. }
  72. + (NSString *)keyIDStorageKeyForAppName:(NSString *)appName appID:(NSString *)appID {
  73. return [NSString stringWithFormat:@"app_attest_keyID.%@.%@", appName, appID];
  74. }
  75. @end