GULKeychainUtils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /// Helper functions to access Keychain.
  20. @interface GULKeychainUtils : NSObject
  21. /** Fetches a keychain item data matching to the provided query.
  22. * @param query A dictionary with Keychain query parameters. See docs for `SecItemCopyMatching` for
  23. * details.
  24. * @param outError A pointer to `NSError` instance or `NULL`. The instance at `outError` will be
  25. * assigned with an error if there is.
  26. * @returns Data for the first Keychain Item matching the provided query or `nil` if there is not
  27. * such an item (`outError` will be `nil` in this case) or an error occurred.
  28. */
  29. + (nullable NSData *)getItemWithQuery:(NSDictionary *)query
  30. error:(NSError *_Nullable *_Nullable)outError;
  31. /** Stores data to a Keychain Item matching to the provided query. An existing Keychain Item
  32. * matching the query parameters will be updated or a new will be created.
  33. * @param item A Keychain Item data to store.
  34. * @param query A dictionary with Keychain query parameters. See docs for `SecItemAdd` and
  35. * `SecItemUpdate` for details.
  36. * @param outError A pointer to `NSError` instance or `NULL`. The instance at `outError` will be
  37. * assigned with an error if there is.
  38. * @returns `YES` when data was successfully stored, `NO` otherwise.
  39. */
  40. + (BOOL)setItem:(NSData *)item
  41. withQuery:(NSDictionary *)query
  42. error:(NSError *_Nullable *_Nullable)outError;
  43. /** Removes a Keychain Item matching to the provided query.
  44. * @param query A dictionary with Keychain query parameters. See docs for `SecItemDelete` for
  45. * details.
  46. * @param outError A pointer to `NSError` instance or `NULL`. The instance at `outError` will be
  47. * assigned with an error if there is.
  48. * @returns `YES` if the item was removed successfully or doesn't exist, `NO` otherwise.
  49. */
  50. + (BOOL)removeItemWithQuery:(NSDictionary *)query error:(NSError *_Nullable *_Nullable)outError;
  51. @end
  52. NS_ASSUME_NONNULL_END