SAMKeychain.h 7.2 KB

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