FLEXKeychain.h 7.1 KB

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