SAMKeychainQuery.h 4.0 KB

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