SSKeychain.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. @return Returns a the password data for the given account and service, or `nil` if the Keychain doesn't
  136. have data for the given parameters.
  137. @see passwordDataForService:account:error:
  138. */
  139. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account;
  140. /**
  141. Returns the password data for a given account and service, or `nil` if the Keychain doesn't have data
  142. for the given parameters.
  143. @param serviceName The service for which to return the corresponding password.
  144. @param account The account for which to return the corresponding password.
  145. @param error If accessing the password fails, upon return contains an error that describes the problem.
  146. @return Returns a the password data for the given account and service, or `nil` if the Keychain doesn't
  147. have a password for the given parameters.
  148. @see passwordDataForService:account:
  149. */
  150. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  151. ///-------------------------
  152. /// @name Deleting Passwords
  153. ///-------------------------
  154. /**
  155. Deletes a password from the Keychain.
  156. @param serviceName The service for which to delete the corresponding password.
  157. @param account The account for which to delete the corresponding password.
  158. @return Returns `YES` on success, or `NO` on failure.
  159. @see deletePasswordForService:account:error:
  160. */
  161. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
  162. /**
  163. Deletes a password from the Keychain.
  164. @param serviceName The service for which to delete the corresponding password.
  165. @param account The account for which to delete the corresponding password.
  166. @param error If deleting the password fails, upon return contains an error that describes the problem.
  167. @return Returns `YES` on success, or `NO` on failure.
  168. @see deletePasswordForService:account:
  169. */
  170. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  171. ///------------------------
  172. /// @name Setting Passwords
  173. ///------------------------
  174. /**
  175. Sets a password in the Keychain.
  176. @param password The password to store in the Keychain.
  177. @param serviceName The service for which to set the corresponding password.
  178. @param account The account for which to set the corresponding password.
  179. @return Returns `YES` on success, or `NO` on failure.
  180. @see setPassword:forService:account:error:
  181. */
  182. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
  183. /**
  184. Sets a password in the Keychain.
  185. @param password The password to store in the Keychain.
  186. @param serviceName The service for which to set the corresponding password.
  187. @param account The account for which to set the corresponding password.
  188. @param error If setting the password fails, upon return contains an error that describes the problem.
  189. @return Returns `YES` on success, or `NO` on failure.
  190. @see setPassword:forService:account:
  191. */
  192. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  193. /**
  194. Sets arbirary data in the Keychain.
  195. @param password The data to store in the Keychain.
  196. @param serviceName The service for which to set the corresponding password.
  197. @param account The account for which to set the corresponding password.
  198. @return Returns `YES` on success, or `NO` on failure.
  199. @see setPasswordData:forService:account:error:
  200. */
  201. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account;
  202. /**
  203. Sets arbirary data in the Keychain.
  204. @param password The data to store in the Keychain.
  205. @param serviceName The service for which to set the corresponding password.
  206. @param account The account for which to set the corresponding password.
  207. @param error If setting the password fails, upon return contains an error that describes the problem.
  208. @return Returns `YES` on success, or `NO` on failure.
  209. @see setPasswordData:forService:account:
  210. */
  211. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  212. ///--------------------
  213. /// @name Configuration
  214. ///--------------------
  215. #if __IPHONE_4_0 && TARGET_OS_IPHONE
  216. /**
  217. Returns the accessibility type for all future passwords saved to the Keychain.
  218. @return Returns the accessibility type.
  219. The return value will be `NULL` or one of the "Keychain Item Accessibility Constants" used for determining when a
  220. keychain item should be readable.
  221. @see accessibilityType
  222. */
  223. + (CFTypeRef)accessibilityType;
  224. /**
  225. Sets the accessibility type for all future passwords saved to the Keychain.
  226. @param accessibilityType One of the "Keychain Item Accessibility Constants" used for determining when a keychain item
  227. should be readable.
  228. If the value is `NULL` (the default), the Keychain default will be used.
  229. @see accessibilityType
  230. */
  231. + (void)setAccessibilityType:(CFTypeRef)accessibilityType;
  232. #endif
  233. @end