FLEXKeychainQuery.h 3.9 KB

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