GULKeychainStorage.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <Foundation/Foundation.h>
  17. @class FBLPromise<ValueType>;
  18. NS_ASSUME_NONNULL_BEGIN
  19. /// The class provides a convenient abstraction on top of the iOS Keychain API to save data.
  20. @interface GULKeychainStorage : NSObject
  21. - (instancetype)init NS_UNAVAILABLE;
  22. /** Initializes the keychain storage with Keychain Service name.
  23. * @param service A Keychain Service name that will be used to store and retrieve objects. See also
  24. * `kSecAttrService`.
  25. */
  26. - (instancetype)initWithService:(NSString *)service;
  27. /**
  28. * Get an object by key.
  29. * @param key The key.
  30. * @param objectClass The expected object class required by `NSSecureCoding`.
  31. * @param accessGroup The Keychain Access Group.
  32. *
  33. * @return Returns a promise. It is resolved with an object stored by key if exists. It is resolved
  34. * with `nil` when the object not found. It fails on a Keychain error.
  35. */
  36. - (FBLPromise<id<NSSecureCoding>> *)getObjectForKey:(NSString *)key
  37. objectClass:(Class)objectClass
  38. accessGroup:(nullable NSString *)accessGroup;
  39. /**
  40. * Saves the given object by the given key.
  41. * @param object The object to store.
  42. * @param key The key to store the object. If there is an existing object by the key, it will be
  43. * overridden.
  44. * @param accessGroup The Keychain Access Group.
  45. *
  46. * @return Returns which is resolved with `[NSNull null]` on success.
  47. */
  48. - (FBLPromise<NSNull *> *)setObject:(id<NSSecureCoding>)object
  49. forKey:(NSString *)key
  50. accessGroup:(nullable NSString *)accessGroup;
  51. /**
  52. * Removes the object by the given key.
  53. * @param key The key to store the object. If there is an existing object by the key, it will be
  54. * overridden.
  55. * @param accessGroup The Keychain Access Group.
  56. *
  57. * @return Returns which is resolved with `[NSNull null]` on success.
  58. */
  59. - (FBLPromise<NSNull *> *)removeObjectForKey:(NSString *)key
  60. accessGroup:(nullable NSString *)accessGroup;
  61. #if TARGET_OS_OSX
  62. /// If not `nil`, then only this keychain will be used to save and read data (see
  63. /// `kSecMatchSearchList` and `kSecUseKeychain`. It is mostly intended to be used by unit tests.
  64. @property(nonatomic, nullable) SecKeychainRef keychainRef;
  65. #endif // TARGET_OSX
  66. @end
  67. NS_ASSUME_NONNULL_END