You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.7 KiB

  1. //
  2. // FLEXKeychainQuery.h
  3. //
  4. // Derived from:
  5. // SSKeychainQuery.h in SSKeychain
  6. // Created by Caleb Davenport on 3/19/13.
  7. // Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
  8. //
  9. #import <Foundation/Foundation.h>
  10. #import <Security/Security.h>
  11. #if __IPHONE_7_0 || __MAC_10_9
  12. // Keychain synchronization available at compile time
  13. #define FLEXKEYCHAIN_SYNCHRONIZATION_AVAILABLE 1
  14. #endif
  15. #if __IPHONE_3_0 || __MAC_10_9
  16. // Keychain access group available at compile time
  17. #define FLEXKEYCHAIN_ACCESS_GROUP_AVAILABLE 1
  18. #endif
  19. #ifdef FLEXKEYCHAIN_SYNCHRONIZATION_AVAILABLE
  20. typedef NS_ENUM(NSUInteger, FLEXKeychainQuerySynchronizationMode) {
  21. FLEXKeychainQuerySynchronizationModeAny,
  22. FLEXKeychainQuerySynchronizationModeNo,
  23. FLEXKeychainQuerySynchronizationModeYes
  24. };
  25. #endif
  26. /// Simple interface for querying or modifying keychain items.
  27. @interface FLEXKeychainQuery : NSObject
  28. /// kSecAttrAccount
  29. @property (nonatomic, copy) NSString *account;
  30. /// kSecAttrService
  31. @property (nonatomic, copy) NSString *service;
  32. /// kSecAttrLabel
  33. @property (nonatomic, copy) NSString *label;
  34. #ifdef FLEXKEYCHAIN_ACCESS_GROUP_AVAILABLE
  35. /// kSecAttrAccessGroup (only used on iOS)
  36. @property (nonatomic, copy) NSString *accessGroup;
  37. #endif
  38. #ifdef FLEXKEYCHAIN_SYNCHRONIZATION_AVAILABLE
  39. /// kSecAttrSynchronizable
  40. @property (nonatomic) FLEXKeychainQuerySynchronizationMode synchronizationMode;
  41. #endif
  42. /// Root storage for password information
  43. @property (nonatomic, copy) NSData *passwordData;
  44. /// This property automatically transitions between an object and the value of
  45. /// `passwordData` using NSKeyedArchiver and NSKeyedUnarchiver.
  46. @property (nonatomic, copy) id<NSCoding> passwordObject;
  47. /// Convenience accessor for setting and getting a password string. Passes through
  48. /// to `passwordData` using UTF-8 string encoding.
  49. @property (nonatomic, copy) NSString *password;
  50. #pragma mark Saving & Deleting
  51. /// Save the receiver's attributes as a keychain item. Existing items with the
  52. /// given account, service, and access group will first be deleted.
  53. ///
  54. /// @param error Populated should an error occur.
  55. /// @return `YES` if saving was successful, `NO` otherwise.
  56. - (BOOL)save:(NSError **)error;
  57. /// Delete keychain items that match the given account, service, and access group.
  58. ///
  59. /// @param error Populated should an error occur.
  60. /// @return `YES` if saving was successful, `NO` otherwise.
  61. - (BOOL)deleteItem:(NSError **)error;
  62. #pragma mark Fetching
  63. /// Fetch all keychain items that match the given account, service, and access
  64. /// group. The values of `password` and `passwordData` are ignored when fetching.
  65. ///
  66. /// @param error Populated should an error occur.
  67. /// @return An array of dictionaries that represent all matching keychain items,
  68. /// or `nil` should an error occur. The order of the items is not determined.
  69. - (NSArray<NSDictionary<NSString *, id> *> *)fetchAll:(NSError **)error;
  70. /// Fetch the keychain item that matches the given account, service, and access
  71. /// group. The `password` and `passwordData` properties will be populated unless
  72. /// an error occurs. The values of `password` and `passwordData` are ignored when
  73. /// fetching.
  74. ///
  75. /// @param error Populated should an error occur.
  76. /// @return `YES` if fetching was successful, `NO` otherwise.
  77. - (BOOL)fetch:(NSError **)error;
  78. #pragma mark Synchronization Status
  79. #ifdef FLEXKEYCHAIN_SYNCHRONIZATION_AVAILABLE
  80. /// Returns a boolean indicating if keychain synchronization is available on the device at runtime.
  81. /// The #define FLEXKEYCHAIN_SYNCHRONIZATION_AVAILABLE is only for compile time.
  82. /// If you are checking for the presence of synchronization, you should use this method.
  83. ///
  84. /// @return A value indicating if keychain synchronization is available
  85. + (BOOL)isSynchronizationAvailable;
  86. #endif
  87. @end