SSKeychainQuery.h 3.6 KB

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