EMKeychainItem.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*Copyright (c) 2009 Extendmac, LLC. <support@extendmac.com>
  2. Permission is hereby granted, free of charge, to any person
  3. obtaining a copy of this software and associated documentation
  4. files (the "Software"), to deal in the Software without
  5. restriction, including without limitation the rights to use,
  6. copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the
  8. Software is furnished to do so, subject to the following
  9. conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  14. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  19. OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. //Last Updated February 8th, 2011.
  22. #import <Cocoa/Cocoa.h>
  23. #import <Carbon/Carbon.h>
  24. #import <Security/Security.h>
  25. /*!
  26. @abstract EMKeychainItem is a self-contained wrapper class for two-way communication with the keychain. You can add, retrieve, and remove both generic and internet keychain items.
  27. @dicussion All keychain items have a username, password, and optionally a label.
  28. */
  29. @interface EMKeychainItem : NSObject
  30. {
  31. @private
  32. NSString *mUsername;
  33. NSString *mPassword;
  34. NSString *mLabel;
  35. @protected
  36. SecKeychainItemRef mCoreKeychainItem;
  37. }
  38. /*!
  39. @abstract Returns whether or not errors are logged.
  40. @discussion Errors occur whenever a keychain item fails to appropriately update a property, or when a given keychain item cannot be found.
  41. */
  42. + (BOOL)logsErrors;
  43. //! @abstracts Sets whether or not errors are logged.
  44. + (void)setLogsErrors:(BOOL)logsErrors;
  45. //! @abstracts Locks the keychain.
  46. + (void)lockKeychain;
  47. //! @abstract Unlocks the keychain.
  48. + (void)unlockKeychain;
  49. //! @abstract The keychain item's username.
  50. @property (readwrite, copy) NSString *username;
  51. //! @abstract The keychain item's password.
  52. @property (readwrite, copy) NSString *password;
  53. //! @abstract The keychain item's label.
  54. @property (readwrite, copy) NSString *label;
  55. /*!
  56. @abstract Removes the receiver from the keychain.
  57. @discussion After calling this method, you should generally discard of the receiver. The receiver cannot be "re-added" to the keychain; invoke either addGenericKeychainItemForService:... or addInternetKeychainItemForServer:... instead.
  58. */
  59. - (void)removeFromKeychain;
  60. @end
  61. #pragma mark -
  62. /*!
  63. @abstract An EMGenericKeychainItem wraps the functionality and data-members associated with a generic keychain item.
  64. @discussion Generic keychain items have a service name in addition to the standard keychain item properties.
  65. */
  66. @interface EMGenericKeychainItem : EMKeychainItem
  67. {
  68. @private
  69. NSString *mServiceName;
  70. }
  71. //! @abstract The keychain item's service name.
  72. @property (readwrite, copy) NSString *serviceName;
  73. /*!
  74. @abstract Returns, if possible, a generic keychain item that corresponds to the given service.
  75. @param serviceName The service name. Cannot be nil.
  76. @param username The username. Cannot be nil.
  77. @result An EMGenericKeychainItem if the keychain item can be discovered. Otherwise, nil.
  78. */
  79. + (EMGenericKeychainItem *)genericKeychainItemForService:(NSString *)serviceName
  80. withUsername:(NSString *)username;
  81. /*!
  82. @abstract Adds a keychain item for the given service.
  83. @param serviceName The service name. Cannot be nil.
  84. @param username The username. Cannot be nil.
  85. @param password The password to associate with the username and service. Cannot be nil.
  86. @result An EMGenericKeychainItem if the service can be added to the keychain. Otherwise, nil.
  87. */
  88. + (EMGenericKeychainItem *)addGenericKeychainItemForService:(NSString *)serviceName
  89. withUsername:(NSString *)username
  90. password:(NSString *)password;
  91. @end
  92. #pragma mark -
  93. /*!
  94. @abstract An EMInternetKeychainItem wraps the functionality and data-members associated with an internet keychain item.
  95. @discussion Internet keychain items can optionally have a server, path, port, and protocol in addition to the standard keychain item properties.
  96. */
  97. @interface EMInternetKeychainItem : EMKeychainItem
  98. {
  99. @private
  100. NSString *mServer;
  101. NSString *mPath;
  102. NSInteger mPort;
  103. SecProtocolType mProtocol;
  104. }
  105. /*!
  106. @abstract Returns, if possible, an internet keychain item that corresponds to the given server.
  107. @param server The server. Cannot be nil.
  108. @param username The username. Cannot be nil.
  109. @param path The path.
  110. @param port The port.
  111. @param protocol The protocol.
  112. @result An EMInternetKeychainItem if the keychain item can be discovered. Otherwise, nil.
  113. */
  114. + (EMInternetKeychainItem *)internetKeychainItemForServer:(NSString *)server
  115. withUsername:(NSString *)username
  116. path:(NSString *)path
  117. port:(NSInteger)port
  118. protocol:(SecProtocolType)protocol;
  119. /*!
  120. @abstract Adds a keychain item for the given server.
  121. @param server The server. Cannot be nil.
  122. @param username The username. Cannot be nil.
  123. @param password The password to associate with the server, username, path, port, and protocol. Cannot be nil.
  124. @param path The path.
  125. @param port The port.
  126. @param protocol The protocol.
  127. @result An EMInternetKeychainItem if the item can be added to the keychain. Otherwise, nil.
  128. */
  129. + (EMInternetKeychainItem *)addInternetKeychainItemForServer:(NSString *)server
  130. withUsername:(NSString *)username
  131. password:(NSString *)password
  132. path:(NSString *)path
  133. port:(NSInteger)port
  134. protocol:(SecProtocolType)protocol;
  135. //! @abstract The keychain item's server.
  136. @property (readwrite, copy) NSString *server;
  137. //! @abstract The keychain item's path.
  138. @property (readwrite, copy) NSString *path;
  139. //! @abstract The keychain item's port.
  140. @property (readwrite, assign) NSInteger port;
  141. //! @abstract The keychain item's protocol.
  142. @property (readwrite, assign) SecProtocolType protocol;
  143. @end