You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
6.2 KiB

  1. //
  2. // FLEXKeychain.h
  3. //
  4. // Derived from:
  5. // SSKeychain.h in SSKeychain
  6. // Created by Sam Soffes on 5/19/10.
  7. // Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
  8. //
  9. #import <Foundation/Foundation.h>
  10. /// Error code specific to FLEXKeychain that can be returned in NSError objects.
  11. /// For codes returned by the operating system, refer to SecBase.h for your
  12. /// platform.
  13. typedef NS_ENUM(OSStatus, FLEXKeychainErrorCode) {
  14. /// Some of the arguments were invalid.
  15. FLEXKeychainErrorBadArguments = -1001,
  16. };
  17. /// FLEXKeychain error domain
  18. extern NSString *const kFLEXKeychainErrorDomain;
  19. /// Account name.
  20. extern NSString *const kFLEXKeychainAccountKey;
  21. /// Time the item was created.
  22. ///
  23. /// The value will be a string.
  24. extern NSString *const kFLEXKeychainCreatedAtKey;
  25. /// Item class.
  26. extern NSString *const kFLEXKeychainClassKey;
  27. /// Item description.
  28. extern NSString *const kFLEXKeychainDescriptionKey;
  29. /// Item label.
  30. extern NSString *const kFLEXKeychainLabelKey;
  31. /// Time the item was last modified.
  32. ///
  33. /// The value will be a string.
  34. extern NSString *const kFLEXKeychainLastModifiedKey;
  35. /// Where the item was created.
  36. extern NSString *const kFLEXKeychainWhereKey;
  37. /// A simple wrapper for accessing accounts, getting passwords,
  38. /// setting passwords, and deleting passwords using the system Keychain.
  39. @interface FLEXKeychain : NSObject
  40. #pragma mark - Classic methods
  41. /// @param serviceName The service for which to return the corresponding password.
  42. /// @param account The account for which to return the corresponding password.
  43. /// @return Returns a string containing the password for a given account and service,
  44. /// or `nil` if the Keychain doesn't have a password for the given parameters.
  45. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
  46. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  47. /// Returns a nsdata containing the password for a given account and service,
  48. /// or `nil` if the Keychain doesn't have a password for the given parameters.
  49. ///
  50. /// @param serviceName The service for which to return the corresponding password.
  51. /// @param account The account for which to return the corresponding password.
  52. /// @return Returns a nsdata containing the password for a given account and service,
  53. /// or `nil` if the Keychain doesn't have a password for the given parameters.
  54. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account;
  55. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  56. /// Deletes a password from the Keychain.
  57. ///
  58. /// @param serviceName The service for which to delete the corresponding password.
  59. /// @param account The account for which to delete the corresponding password.
  60. /// @return Returns `YES` on success, or `NO` on failure.
  61. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
  62. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  63. /// Sets a password in the Keychain.
  64. ///
  65. /// @param password The password to store in the Keychain.
  66. /// @param serviceName The service for which to set the corresponding password.
  67. /// @param account The account for which to set the corresponding password.
  68. /// @return Returns `YES` on success, or `NO` on failure.
  69. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
  70. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  71. /// Sets a password in the Keychain.
  72. ///
  73. /// @param password The password to store in the Keychain.
  74. /// @param serviceName The service for which to set the corresponding password.
  75. /// @param account The account for which to set the corresponding password.
  76. /// @return Returns `YES` on success, or `NO` on failure.
  77. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account;
  78. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
  79. /// @return An array of dictionaries containing the Keychain's accounts, or `nil` if
  80. /// the Keychain doesn't have any accounts. The order of the objects in the array isn't defined.
  81. ///
  82. /// @note See the `NSString` constants declared in FLEXKeychain.h for a list of keys that
  83. /// can be used when accessing the dictionaries returned by this method.
  84. + (NSArray<NSDictionary<NSString *, id> *> *)allAccounts;
  85. + (NSArray<NSDictionary<NSString *, id> *> *)allAccounts:(NSError *__autoreleasing *)error;
  86. /// @param serviceName The service for which to return the corresponding accounts.
  87. /// @return An array of dictionaries containing the Keychain's accounts for a given `serviceName`,
  88. /// or `nil` if the Keychain doesn't have any accounts for the given `serviceName`.
  89. /// The order of the objects in the array isn't defined.
  90. ///
  91. /// @note See the `NSString` constants declared in FLEXKeychain.h for a list of keys that
  92. /// can be used when accessing the dictionaries returned by this method.
  93. + (NSArray<NSDictionary<NSString *, id> *> *)accountsForService:(NSString *)serviceName;
  94. + (NSArray<NSDictionary<NSString *, id> *> *)accountsForService:(NSString *)serviceName error:(NSError *__autoreleasing *)error;
  95. #pragma mark - Configuration
  96. #if __IPHONE_4_0 && TARGET_OS_IPHONE
  97. /// Returns the accessibility type for all future passwords saved to the Keychain.
  98. ///
  99. /// @return `NULL` or one of the "Keychain Item Accessibility
  100. /// Constants" used for determining when a keychain item should be readable.
  101. + (CFTypeRef)accessibilityType;
  102. /// Sets the accessibility type for all future passwords saved to the Keychain.
  103. ///
  104. /// @param accessibilityType One of the "Keychain Item Accessibility Constants"
  105. /// used for determining when a keychain item should be readable.
  106. /// If the value is `NULL` (the default), the Keychain default will be used which
  107. /// is highly insecure. You really should use at least `kSecAttrAccessibleAfterFirstUnlock`
  108. /// for background applications or `kSecAttrAccessibleWhenUnlocked` for all
  109. /// other applications.
  110. ///
  111. /// @note See Security/SecItem.h
  112. + (void)setAccessibilityType:(CFTypeRef)accessibilityType;
  113. #endif
  114. @end