GULKeychainUtils.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. NS_ASSUME_NONNULL_BEGIN
  18. FOUNDATION_EXPORT NSString *const kGULKeychainUtilsErrorDomain;
  19. /// A collection of helper functions that abstract away common Keychain operations.
  20. ///
  21. /// When using this API on macOS, the corresponding target must be signed with a provisioning
  22. /// profile that has the Keychain Sharing capability enabled.
  23. @interface GULKeychainUtils : NSObject
  24. /** Fetches a keychain item data matching to the provided query.
  25. * @param query A dictionary with Keychain query parameters. See docs for `SecItemCopyMatching` for
  26. * details.
  27. * @param outError A pointer to `NSError` instance or `NULL`. The instance at `outError` will be
  28. * assigned with an error if there is.
  29. * @returns Data for the first Keychain Item matching the provided query or `nil` if there is not
  30. * such an item (`outError` will be `nil` in this case) or an error occurred.
  31. */
  32. + (nullable NSData *)getItemWithQuery:(NSDictionary *)query
  33. error:(NSError *_Nullable *_Nullable)outError;
  34. /** Stores data to a Keychain Item matching to the provided query. An existing Keychain Item
  35. * matching the query parameters will be updated or a new will be created.
  36. * @param item A Keychain Item data to store.
  37. * @param query A dictionary with Keychain query parameters. See docs for `SecItemAdd` and
  38. * `SecItemUpdate` for details.
  39. * @param outError A pointer to `NSError` instance or `NULL`. The instance at `outError` will be
  40. * assigned with an error if there is.
  41. * @returns `YES` when data was successfully stored, `NO` otherwise.
  42. */
  43. + (BOOL)setItem:(NSData *)item
  44. withQuery:(NSDictionary *)query
  45. error:(NSError *_Nullable *_Nullable)outError;
  46. /** Removes a Keychain Item matching to the provided query.
  47. * @param query A dictionary with Keychain query parameters. See docs for `SecItemDelete` for
  48. * details.
  49. * @param outError A pointer to `NSError` instance or `NULL`. The instance at `outError` will be
  50. * assigned with an error if there is.
  51. * @returns `YES` if the item was removed successfully or doesn't exist, `NO` otherwise.
  52. */
  53. + (BOOL)removeItemWithQuery:(NSDictionary *)query error:(NSError *_Nullable *_Nullable)outError;
  54. @end
  55. NS_ASSUME_NONNULL_END