keychain.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /******************************************************************************
  2. * Copyright (C) 2011-2015 Frank Osterfeld <frank.osterfeld@gmail.com> *
  3. * *
  4. * This program is distributed in the hope that it will be useful, but *
  5. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
  6. * or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution *
  7. * details, check the accompanying file 'COPYING'. *
  8. *****************************************************************************/
  9. #ifndef KEYCHAIN_H
  10. #define KEYCHAIN_H
  11. #if !defined(QTKEYCHAIN_NO_EXPORT)
  12. #include "qkeychain_export.h"
  13. #else
  14. #define QKEYCHAIN_EXPORT
  15. #endif
  16. #include <QtCore/QObject>
  17. #include <QtCore/QString>
  18. class QSettings;
  19. #define QTKEYCHAIN_VERSION 0x000100
  20. namespace QKeychain {
  21. /**
  22. * Error codes
  23. */
  24. enum Error {
  25. NoError=0, /**< No error occurred, operation was successful */
  26. EntryNotFound, /**< For the given key no data was found */
  27. CouldNotDeleteEntry, /**< Could not delete existing secret data */
  28. AccessDeniedByUser, /**< User denied access to keychain */
  29. AccessDenied, /**< Access denied for other reasons */
  30. NoBackendAvailable, /**< No platform-specific keychain service available */
  31. NotImplemented, /**< Not implemented on platform */
  32. OtherError /**< Something else went wrong (errorString() might provide details) */
  33. };
  34. class JobExecutor;
  35. class JobPrivate;
  36. /**
  37. * @brief Abstract base class for all QKeychain jobs.
  38. */
  39. class QKEYCHAIN_EXPORT Job : public QObject {
  40. Q_OBJECT
  41. public:
  42. ~Job() override;
  43. /**
  44. * @return The QSettings instance used as plaintext storage if insecureFallback() is true.
  45. * @see setSettings()
  46. * @see insecureFallback()
  47. */
  48. QSettings* settings() const;
  49. /**
  50. * @return Set the QSettings instance that will be used as plaintext storage if insecureFallback() is true.
  51. * @see settings()
  52. * @see insecureFallback()
  53. */
  54. void setSettings( QSettings* settings );
  55. /**
  56. * Call this method to start the job.
  57. * Typically you want to connect some slot to the finished() signal first:
  58. *
  59. * \code
  60. * SomeClass::startJob()
  61. * {
  62. * connect(job, &Job::finished, this, &SomeClass::slotJobFinished);
  63. * job->start();
  64. * }
  65. *
  66. * SomeClass::slotJobFinished(Job *job)
  67. * {
  68. * if (job->error()) {
  69. * // handle error
  70. * } else {
  71. * // do job-specific stuff
  72. * }
  73. * }
  74. * \endcode
  75. *
  76. * @see finished()
  77. */
  78. void start();
  79. QString service() const;
  80. /**
  81. * @note Call this method only after finished() has been emitted.
  82. * @return The error code of the job (0 if no error).
  83. */
  84. Error error() const;
  85. /**
  86. * @return An error message that might provide details if error() returns OtherError.
  87. */
  88. QString errorString() const;
  89. /**
  90. * @return Whether this job autodeletes itself once finished() has been emitted. Default is true.
  91. * @see setAutoDelete()
  92. */
  93. bool autoDelete() const;
  94. /**
  95. * Set whether this job should autodelete itself once finished() has been emitted.
  96. * @see autoDelete()
  97. */
  98. void setAutoDelete( bool autoDelete );
  99. /**
  100. * @return Whether this job will use plaintext storage on unsupported platforms. Default is false.
  101. * @see setInsecureFallback()
  102. */
  103. bool insecureFallback() const;
  104. /**
  105. * Set whether this job should use plaintext storage on unsupported platforms.
  106. * @see insecureFallback()
  107. */
  108. void setInsecureFallback( bool insecureFallback );
  109. /**
  110. * @return The string used as key by this job.
  111. * @see setKey()
  112. */
  113. QString key() const;
  114. /**
  115. * Set the @p key that this job will use to read or write data from/to the keychain.
  116. * The key can be an empty string.
  117. * @see key()
  118. */
  119. void setKey( const QString& key );
  120. void emitFinished();
  121. void emitFinishedWithError(Error, const QString& errorString);
  122. Q_SIGNALS:
  123. /**
  124. * Emitted when this job is finished.
  125. * You can connect to this signal to be notified about the job's completion.
  126. * @see start()
  127. */
  128. void finished( QKeychain::Job* );
  129. protected:
  130. explicit Job( JobPrivate *q, QObject* parent=nullptr );
  131. Q_INVOKABLE void doStart();
  132. private:
  133. void setError( Error error );
  134. void setErrorString( const QString& errorString );
  135. void scheduledStart();
  136. protected:
  137. JobPrivate* const d;
  138. friend class JobExecutor;
  139. friend class JobPrivate;
  140. friend class ReadPasswordJobPrivate;
  141. friend class WritePasswordJobPrivate;
  142. friend class DeletePasswordJobPrivate;
  143. };
  144. class ReadPasswordJobPrivate;
  145. /**
  146. * @brief Job for reading secrets from the keychain.
  147. * You can use a ReadPasswordJob to read passwords or binary data from the keychain.
  148. * This job requires a "service" string, which is basically a namespace of keys within the keychain.
  149. * This means that you can read all the pairs <key, secret> stored in the same service string.
  150. */
  151. class QKEYCHAIN_EXPORT ReadPasswordJob : public Job {
  152. Q_OBJECT
  153. public:
  154. /**
  155. * Create a new ReadPasswordJob.
  156. * @param service The service string used by this job (can be empty).
  157. * @param parent The parent of this job.
  158. */
  159. explicit ReadPasswordJob( const QString& service, QObject* parent=nullptr );
  160. ~ReadPasswordJob() override;
  161. /**
  162. * @return The binary data stored as value of this job's key().
  163. * @see Job::key()
  164. */
  165. QByteArray binaryData() const;
  166. /**
  167. * @return The string stored as value of this job's key().
  168. * @see Job::key()
  169. * @warning Returns meaningless data if the data was stored as binary data.
  170. * @see WritePasswordJob::setTextData()
  171. */
  172. QString textData() const;
  173. private:
  174. friend class QKeychain::ReadPasswordJobPrivate;
  175. };
  176. class WritePasswordJobPrivate;
  177. /**
  178. * @brief Job for writing secrets to the keychain.
  179. * You can use a WritePasswordJob to store passwords or binary data in the keychain.
  180. * This job requires a "service" string, which is basically a namespace of keys within the keychain.
  181. * This means that you can store different pairs <key, secret> under the same service string.
  182. */
  183. class QKEYCHAIN_EXPORT WritePasswordJob : public Job {
  184. Q_OBJECT
  185. public:
  186. /**
  187. * Create a new WritePasswordJob.
  188. * @param service The service string used by this job (can be empty).
  189. * @param parent The parent of this job.
  190. */
  191. explicit WritePasswordJob( const QString& service, QObject* parent=nullptr );
  192. ~WritePasswordJob() override;
  193. /**
  194. * Set the @p data that the job will store in the keychain as binary data.
  195. * @warning setBinaryData() and setTextData() are mutually exclusive.
  196. */
  197. void setBinaryData( const QByteArray& data );
  198. /**
  199. * Set the @p data that the job will store in the keychain as string.
  200. * Typically @p data is a password.
  201. * @warning setBinaryData() and setTextData() are mutually exclusive.
  202. */
  203. void setTextData( const QString& data );
  204. private:
  205. friend class QKeychain::WritePasswordJobPrivate;
  206. };
  207. class DeletePasswordJobPrivate;
  208. /**
  209. * @brief Job for deleting secrets from the keychain.
  210. * You can use a DeletePasswordJob to delete passwords or binary data from the keychain.
  211. * This job requires a "service" string, which is basically a namespace of keys within the keychain.
  212. * This means that you can delete all the pairs <key, secret> stored in the same service string.
  213. */
  214. class QKEYCHAIN_EXPORT DeletePasswordJob : public Job {
  215. Q_OBJECT
  216. public:
  217. /**
  218. * Create a new DeletePasswordJob.
  219. * @param service The service string used by this job (can be empty).
  220. * @param parent The parent of this job.
  221. */
  222. explicit DeletePasswordJob( const QString& service, QObject* parent=nullptr );
  223. ~DeletePasswordJob() override;
  224. private:
  225. friend class QKeychain::DeletePasswordJobPrivate;
  226. };
  227. } // namespace QtKeychain
  228. #endif