SSKeychain.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // SSKeychain.h
  3. // SSToolkit
  4. //
  5. // Created by Sam Soffes on 5/19/10.
  6. // Copyright (c) 2009-2011 Sam Soffes. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <Security/Security.h>
  10. /** Error codes that can be returned in NSError objects. */
  11. typedef enum {
  12. /** No error. */
  13. SSKeychainErrorNone = noErr,
  14. /** Some of the arguments were invalid. */
  15. SSKeychainErrorBadArguments = -1001,
  16. /** There was no password. */
  17. SSKeychainErrorNoPassword = -1002,
  18. /** One or more parameters passed internally were not valid. */
  19. SSKeychainErrorInvalidParameter = errSecParam,
  20. /** Failed to allocate memory. */
  21. SSKeychainErrorFailedToAllocated = errSecAllocate,
  22. /** No trust results are available. */
  23. SSKeychainErrorNotAvailable = errSecNotAvailable,
  24. /** Authorization/Authentication failed. */
  25. SSKeychainErrorAuthorizationFailed = errSecAuthFailed,
  26. /** The item already exists. */
  27. SSKeychainErrorDuplicatedItem = errSecDuplicateItem,
  28. /** The item cannot be found.*/
  29. SSKeychainErrorNotFound = errSecItemNotFound,
  30. /** Interaction with the Security Server is not allowed. */
  31. SSKeychainErrorInteractionNotAllowed = errSecInteractionNotAllowed,
  32. /** Unable to decode the provided data. */
  33. SSKeychainErrorFailedToDecode = errSecDecode
  34. } SSKeychainErrorCode;
  35. extern NSString *const kSSKeychainErrorDomain;
  36. /** Account name. */
  37. extern NSString *const kSSKeychainAccountKey;
  38. /**
  39. Time the item was created.
  40. The value will be a string.
  41. */
  42. extern NSString *const kSSKeychainCreatedAtKey;
  43. /** Item class. */
  44. extern NSString *const kSSKeychainClassKey;
  45. /** Item description. */
  46. extern NSString *const kSSKeychainDescriptionKey;
  47. /** Item label. */
  48. extern NSString *const kSSKeychainLabelKey;
  49. /** Time the item was last modified.
  50. The value will be a string.
  51. */
  52. extern NSString *const kSSKeychainLastModifiedKey;
  53. /** Where the item was created. */
  54. extern NSString *const kSSKeychainWhereKey;
  55. /**
  56. Simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system
  57. Keychain on Mac OS X and iOS.
  58. This was originally inspired by EMKeychain and SDKeychain (both of which are now gone). Thanks to the authors.
  59. SSKeychain has since switched to a simpler implementation that was abstracted from [SSToolkit](http://sstoolk.it).
  60. */
  61. @interface SSKeychain : NSObject
  62. ///-----------------------
  63. /// @name Getting Accounts
  64. ///-----------------------
  65. /**
  66. Returns an array containing the Keychain's accounts, or `nil` if the Keychain has no accounts.
  67. See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
  68. dictionaries returned by this method.
  69. @return An array of dictionaries containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
  70. accounts. The order of the objects in the array isn't defined.
  71. @see allAccounts:
  72. */
  73. + (NSArray *)allAccounts;
  74. /**
  75. Returns an array containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
  76. accounts.
  77. See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
  78. dictionaries returned by this method.
  79. @param error If accessing the accounts fails, upon return contains an error that describes the problem.
  80. @return An array of dictionaries containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
  81. accounts. The order of the objects in the array isn't defined.
  82. @see allAccounts
  83. */
  84. + (NSArray *)allAccounts:(NSError **)error;
  85. /**
  86. Returns an array containing the Keychain's accounts for a given service, or `nil` if the Keychain doesn't have any
  87. accounts for the given service.
  88. See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
  89. dictionaries returned by this method.
  90. @param serviceName The service for which to return the corresponding accounts.
  91. @return An array of dictionaries containing the Keychain's accountsfor a given `serviceName`, or `nil` if the Keychain
  92. doesn't have any accounts for the given `serviceName`. The order of the objects in the array isn't defined.
  93. @see accountsForService:error:
  94. */
  95. + (NSArray *)accountsForService:(NSString *)serviceName;
  96. /**
  97. Returns an array containing the Keychain's accounts for a given service, or `nil` if the Keychain doesn't have any
  98. accounts for the given service.
  99. @param serviceName The service for which to return the corresponding accounts.
  100. @param error If accessing the accounts fails, upon return contains an error that describes the problem.
  101. @return An array of dictionaries containing the Keychain's accountsfor a given `serviceName`, or `nil` if the Keychain
  102. doesn't have any accounts for the given `serviceName`. The order of the objects in the array isn't defined.
  103. @see accountsForService:
  104. */
  105. + (NSArray *)accountsForService:(NSString *)serviceName error:(NSError **)error;
  106. ///------------------------
  107. /// @name Getting Passwords
  108. ///------------------------
  109. /**
  110. Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't have a
  111. password for the given parameters.
  112. @param serviceName The service for which to return the corresponding password.
  113. @param account The account for which to return the corresponding password.
  114. @return Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't
  115. have a password for the given parameters.
  116. @see passwordForService:account:error:
  117. */
  118. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
  119. /**
  120. Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't have a
  121. password for the given parameters.
  122. @param serviceName The service for which to return the corresponding password.
  123. @param account The account for which to return the corresponding password.
  124. @param error If accessing the password fails, upon return contains an error that describes the problem.
  125. @return Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't
  126. have a password for the given parameters.
  127. @see passwordForService:account:
  128. */
  129. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  130. /**
  131. Returns the password data for a given account and service, or `nil` if the Keychain doesn't have data
  132. for the given parameters.
  133. @param serviceName The service for which to return the corresponding password.
  134. @param account The account for which to return the corresponding password.
  135. @param error If accessing the password fails, upon return contains an error that describes the problem.
  136. @return Returns a the password data for the given account and service, or `nil` if the Keychain doesn't
  137. have data for the given parameters.
  138. @see passwordDataForService:account:error:
  139. */
  140. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account;
  141. /**
  142. Returns the password data for a given account and service, or `nil` if the Keychain doesn't have data
  143. for the given parameters.
  144. @param serviceName The service for which to return the corresponding password.
  145. @param account The account for which to return the corresponding password.
  146. @param error If accessing the password fails, upon return contains an error that describes the problem.
  147. @return Returns a the password data for the given account and service, or `nil` if the Keychain doesn't
  148. have a password for the given parameters.
  149. @see passwordDataForService:account:
  150. */
  151. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  152. ///-------------------------
  153. /// @name Deleting Passwords
  154. ///-------------------------
  155. /**
  156. Deletes a password from the Keychain.
  157. @param serviceName The service for which to delete the corresponding password.
  158. @param account The account for which to delete the corresponding password.
  159. @return Returns `YES` on success, or `NO` on failure.
  160. @see deletePasswordForService:account:error:
  161. */
  162. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
  163. /**
  164. Deletes a password from the Keychain.
  165. @param serviceName The service for which to delete the corresponding password.
  166. @param account The account for which to delete the corresponding password.
  167. @param error If deleting the password fails, upon return contains an error that describes the problem.
  168. @return Returns `YES` on success, or `NO` on failure.
  169. @see deletePasswordForService:account:
  170. */
  171. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  172. ///------------------------
  173. /// @name Setting Passwords
  174. ///------------------------
  175. /**
  176. Sets a password in the Keychain.
  177. @param password The password to store in the Keychain.
  178. @param serviceName The service for which to set the corresponding password.
  179. @param account The account for which to set the corresponding password.
  180. @return Returns `YES` on success, or `NO` on failure.
  181. @see setPassword:forService:account:error:
  182. */
  183. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
  184. /**
  185. Sets a password in the Keychain.
  186. @param password The password to store in the Keychain.
  187. @param serviceName The service for which to set the corresponding password.
  188. @param account The account for which to set the corresponding password.
  189. @param error If setting the password fails, upon return contains an error that describes the problem.
  190. @return Returns `YES` on success, or `NO` on failure.
  191. @see setPassword:forService:account:
  192. */
  193. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  194. /**
  195. Sets arbirary data in the Keychain.
  196. @param password The data to store in the Keychain.
  197. @param serviceName The service for which to set the corresponding password.
  198. @param account The account for which to set the corresponding password.
  199. @param error If setting the password fails, upon return contains an error that describes the problem.
  200. @return Returns `YES` on success, or `NO` on failure.
  201. @see setPasswordData:forService:account:error:
  202. */
  203. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account;
  204. /**
  205. Sets arbirary data in the Keychain.
  206. @param password The data to store in the Keychain.
  207. @param serviceName The service for which to set the corresponding password.
  208. @param account The account for which to set the corresponding password.
  209. @param error If setting the password fails, upon return contains an error that describes the problem.
  210. @return Returns `YES` on success, or `NO` on failure.
  211. @see setPasswordData:forService:account:
  212. */
  213. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  214. ///--------------------
  215. /// @name Configuration
  216. ///--------------------
  217. #if __IPHONE_4_0 && TARGET_OS_IPHONE
  218. /**
  219. Returns the accessibility type for all future passwords saved to the Keychain.
  220. @return Returns the accessibility type.
  221. The return value will be `NULL` or one of the "Keychain Item Accessibility Constants" used for determining when a
  222. keychain item should be readable.
  223. @see accessibilityType
  224. */
  225. + (CFTypeRef)accessibilityType;
  226. /**
  227. Sets the accessibility type for all future passwords saved to the Keychain.
  228. @param accessibilityType One of the "Keychain Item Accessibility Constants" used for determining when a keychain item
  229. should be readable.
  230. If the value is `NULL` (the default), the Keychain default will be used.
  231. @see accessibilityType
  232. */
  233. + (void)setAccessibilityType:(CFTypeRef)accessibilityType;
  234. #endif
  235. @end