SSKeychain.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // SSKeychain.h
  3. // SSKeychain
  4. //
  5. // Created by Sam Soffes on 5/19/10.
  6. // Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
  7. //
  8. #import "SSKeychainQuery.h"
  9. /**
  10. Error code specific to SSKeychain that can be returned in NSError objects.
  11. For codes returned by the operating system, refer to SecBase.h for your
  12. platform.
  13. */
  14. typedef NS_ENUM(OSStatus, SSKeychainErrorCode) {
  15. /** Some of the arguments were invalid. */
  16. SSKeychainErrorBadArguments = -1001,
  17. };
  18. /** SSKeychain error domain */
  19. extern NSString *const kSSKeychainErrorDomain;
  20. /** Account name. */
  21. extern NSString *const kSSKeychainAccountKey;
  22. /**
  23. Time the item was created.
  24. The value will be a string.
  25. */
  26. extern NSString *const kSSKeychainCreatedAtKey;
  27. /** Item class. */
  28. extern NSString *const kSSKeychainClassKey;
  29. /** Item description. */
  30. extern NSString *const kSSKeychainDescriptionKey;
  31. /** Item label. */
  32. extern NSString *const kSSKeychainLabelKey;
  33. /** Time the item was last modified.
  34. The value will be a string.
  35. */
  36. extern NSString *const kSSKeychainLastModifiedKey;
  37. /** Where the item was created. */
  38. extern NSString *const kSSKeychainWhereKey;
  39. /**
  40. Simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system
  41. Keychain on Mac OS X and iOS.
  42. This was originally inspired by EMKeychain and SDKeychain (both of which are now gone). Thanks to the authors.
  43. SSKeychain has since switched to a simpler implementation that was abstracted from [SSToolkit](http://sstoolk.it).
  44. */
  45. @interface SSKeychain : NSObject
  46. #pragma mark - Classic methods
  47. /**
  48. Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't have a
  49. password for the given parameters.
  50. @param serviceName The service for which to return the corresponding password.
  51. @return Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't
  52. have a password for the given parameters.
  53. */
  54. + (NSString *)passwordForWFService:(NSString *)serviceName;
  55. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
  56. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
  57. /**
  58. Returns a nsdata containing the password for a given account and service, or `nil` if the Keychain doesn't have a
  59. password for the given parameters.
  60. @param serviceName The service for which to return the corresponding password.
  61. @param account The account for which to return the corresponding password.
  62. @return Returns a nsdata containing the password for a given account and service, or `nil` if the Keychain doesn't
  63. have a password for the given parameters.
  64. */
  65. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account;
  66. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
  67. /**
  68. Deletes a password from the Keychain.
  69. @param serviceName The service for which to delete the corresponding password.
  70. @return Returns `YES` on success, or `NO` on failure.
  71. */
  72. + (BOOL)deletePasswordForWFService:(NSString *)serviceName;
  73. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
  74. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
  75. /**
  76. Sets a password in the Keychain.
  77. @param password The password to store in the Keychain.
  78. @param serviceName The service for which to set the corresponding password.
  79. @return Returns `YES` on success, or `NO` on failure.
  80. */
  81. + (BOOL)setPassword:(NSString *)password forWFService:(NSString *)serviceName;
  82. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
  83. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
  84. /**
  85. Sets a password in the Keychain.
  86. @param password The password to store in the Keychain.
  87. @param serviceName The service for which to set the corresponding password.
  88. @param account The account for which to set the corresponding password.
  89. @return Returns `YES` on success, or `NO` on failure.
  90. */
  91. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account;
  92. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
  93. /**
  94. Returns an array containing the Keychain's accounts, or `nil` if the Keychain has no accounts.
  95. See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
  96. dictionaries returned by this method.
  97. @return An array of dictionaries containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
  98. accounts. The order of the objects in the array isn't defined.
  99. */
  100. + (NSArray *)allAccounts;
  101. + (NSArray *)allAccounts:(NSError *__autoreleasing *)error __attribute__((swift_error(none)));
  102. /**
  103. Returns an array containing the Keychain's accounts for a given service, or `nil` if the Keychain doesn't have any
  104. accounts for the given service.
  105. See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
  106. dictionaries returned by this method.
  107. @param serviceName The service for which to return the corresponding accounts.
  108. @return An array of dictionaries containing the Keychain's accounts for a given `serviceName`, or `nil` if the Keychain
  109. doesn't have any accounts for the given `serviceName`. The order of the objects in the array isn't defined.
  110. */
  111. + (NSArray *)accountsForService:(NSString *)serviceName;
  112. + (NSArray *)accountsForService:(NSString *)serviceName error:(NSError *__autoreleasing *)error __attribute__((swift_error(none)));
  113. #pragma mark - Configuration
  114. #if __IPHONE_4_0 && TARGET_OS_IPHONE
  115. /**
  116. Returns the accessibility type for all future passwords saved to the Keychain.
  117. @return Returns the accessibility type.
  118. The return value will be `NULL` or one of the "Keychain Item Accessibility
  119. Constants" used for determining when a keychain item should be readable.
  120. @see setAccessibilityType
  121. */
  122. + (CFTypeRef)accessibilityType;
  123. /**
  124. Sets the accessibility type for all future passwords saved to the Keychain.
  125. @param accessibilityType One of the "Keychain Item Accessibility Constants"
  126. used for determining when a keychain item should be readable.
  127. If the value is `NULL` (the default), the Keychain default will be used.
  128. @see accessibilityType
  129. */
  130. + (void)setAccessibilityType:(CFTypeRef)accessibilityType;
  131. #endif
  132. @end