2011-10-27 16:15:46 +00:00
|
|
|
/******************************************************************************
|
2015-03-17 13:31:48 +00:00
|
|
|
* Copyright (C) 2011-2015 Frank Osterfeld <frank.osterfeld@gmail.com> *
|
2011-10-27 16:15:46 +00:00
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution *
|
|
|
|
* details, check the accompanying file 'COPYING'. *
|
|
|
|
*****************************************************************************/
|
2011-10-27 16:14:37 +00:00
|
|
|
#ifndef KEYCHAIN_H
|
|
|
|
#define KEYCHAIN_H
|
|
|
|
|
2011-10-28 19:27:03 +00:00
|
|
|
#include "qkeychain_export.h"
|
|
|
|
|
2012-04-05 14:16:22 +00:00
|
|
|
#include <QtCore/QObject>
|
2011-10-27 16:15:46 +00:00
|
|
|
#include <QtCore/QString>
|
2011-10-27 16:14:37 +00:00
|
|
|
|
2011-12-09 19:47:11 +00:00
|
|
|
class QSettings;
|
|
|
|
|
2013-01-16 17:53:49 +00:00
|
|
|
#define QTKEYCHAIN_VERSION 0x000100
|
|
|
|
|
2011-10-27 19:17:54 +00:00
|
|
|
namespace QKeychain {
|
2012-04-05 14:16:22 +00:00
|
|
|
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
2012-04-05 14:16:22 +00:00
|
|
|
* Error codes
|
2011-10-27 18:46:23 +00:00
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
enum Error {
|
|
|
|
NoError=0, /**< No error occurred, operation was successful */
|
|
|
|
EntryNotFound, /**< For the given key no data was found */
|
|
|
|
CouldNotDeleteEntry, /**< Could not delete existing secret data */
|
|
|
|
AccessDeniedByUser, /**< User denied access to keychain */
|
|
|
|
AccessDenied, /**< Access denied for other reasons */
|
2012-05-08 15:23:26 +00:00
|
|
|
NoBackendAvailable, /**< No platform-specific keychain service available */
|
2012-04-05 14:16:22 +00:00
|
|
|
NotImplemented, /**< Not implemented on platform */
|
|
|
|
OtherError /**< Something else went wrong (errorString() might provide details) */
|
|
|
|
};
|
|
|
|
|
2012-07-27 21:10:47 +00:00
|
|
|
class JobExecutor;
|
|
|
|
class JobPrivate;
|
|
|
|
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* @brief Abstract base class for all QKeychain jobs.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
class QKEYCHAIN_EXPORT Job : public QObject {
|
|
|
|
Q_OBJECT
|
2015-11-27 13:37:41 +00:00
|
|
|
public:
|
2012-04-05 14:16:22 +00:00
|
|
|
~Job();
|
|
|
|
|
|
|
|
QSettings* settings() const;
|
|
|
|
void setSettings( QSettings* settings );
|
|
|
|
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* Call this method to start the job.
|
|
|
|
* Tipically you want to connect some slot to the finished() signal first.
|
|
|
|
* You can run the job either synchronously or asynchronously.
|
|
|
|
*
|
|
|
|
* In the first case you tipically use an inner event loop:
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* SomeClass::startJob()
|
|
|
|
* {
|
|
|
|
* QEventLoop eventLoop;
|
|
|
|
* connect(job, &Job::finished, &eventLoop, &QEventLoop::quit);
|
|
|
|
* job->start();
|
|
|
|
* eventLoop.exec();
|
|
|
|
*
|
|
|
|
* if (job->error() {
|
|
|
|
* // handle error
|
|
|
|
* } else {
|
|
|
|
* // do job-specific stuff
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* In the asynchronous case you just connect some slot to the finished() signal
|
|
|
|
* and you will handle the job's completion there:
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* SomeClass::startJob()
|
|
|
|
* {
|
|
|
|
* connect(job, &Job::finished, this, &SomeClass::slotJobFinished);
|
|
|
|
* job->start();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* SomeClass::slotJobFinished(Job *job)
|
|
|
|
* {
|
|
|
|
* if (job->error() {
|
|
|
|
* // handle error
|
|
|
|
* } else {
|
|
|
|
* // do job-specific stuff
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @see finished()
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
void start();
|
|
|
|
|
2011-10-27 16:14:37 +00:00
|
|
|
QString service() const;
|
2011-10-27 18:46:23 +00:00
|
|
|
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* @note Call this method only after finished() has been emitted.
|
|
|
|
* @return The error code of the job (0 if no error).
|
|
|
|
*/
|
2011-10-27 16:15:46 +00:00
|
|
|
Error error() const;
|
2016-08-17 12:40:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return An error message that might provide details if error() returns OtherError.
|
|
|
|
*/
|
2011-10-27 16:15:46 +00:00
|
|
|
QString errorString() const;
|
2011-10-27 16:14:37 +00:00
|
|
|
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* @return Whether this job autodeletes itself once finished() has been emitted. Default is true.
|
|
|
|
* @see setAutoDelete()
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
bool autoDelete() const;
|
2016-08-17 12:40:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this job should autodelete itself once finished() has been emitted.
|
|
|
|
* @see autoDelete()
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
void setAutoDelete( bool autoDelete );
|
|
|
|
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* @return Whether this job will use plaintext storage on unsupported platforms. Default is false.
|
|
|
|
* @see setInsecureFallback()
|
|
|
|
*/
|
2012-06-09 20:11:16 +00:00
|
|
|
bool insecureFallback() const;
|
2016-08-17 12:40:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this job should use plaintext storage on unsupported platforms.
|
|
|
|
* @see insecureFallback()
|
|
|
|
*/
|
2012-06-09 20:11:16 +00:00
|
|
|
void setInsecureFallback( bool insecureFallback );
|
|
|
|
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* @return The string used as key by this job.
|
|
|
|
* @see setKey()
|
|
|
|
*/
|
2015-11-27 13:37:41 +00:00
|
|
|
QString key() const;
|
2016-08-17 12:40:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the @p key that this job will use to read or write data from/to the keychain.
|
|
|
|
* The key can be an empty string.
|
|
|
|
* @see key()
|
|
|
|
*/
|
2015-11-27 13:37:41 +00:00
|
|
|
void setKey( const QString& key );
|
|
|
|
|
2012-04-05 14:16:22 +00:00
|
|
|
Q_SIGNALS:
|
2016-08-17 12:40:55 +00:00
|
|
|
/**
|
|
|
|
* Emitted when this job is finished.
|
|
|
|
* You can connect to this signal to be notified about the job's completion.
|
|
|
|
* @see start()
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
void finished( QKeychain::Job* );
|
|
|
|
|
|
|
|
protected:
|
2015-11-27 13:37:41 +00:00
|
|
|
explicit Job( JobPrivate *q, QObject* parent=0 );
|
|
|
|
Q_INVOKABLE void doStart();
|
2012-04-05 14:16:22 +00:00
|
|
|
|
2015-11-27 13:37:41 +00:00
|
|
|
private:
|
2012-04-05 14:16:22 +00:00
|
|
|
void setError( Error error );
|
|
|
|
void setErrorString( const QString& errorString );
|
|
|
|
void emitFinished();
|
|
|
|
void emitFinishedWithError(Error, const QString& errorString);
|
|
|
|
|
2015-11-27 13:37:41 +00:00
|
|
|
void scheduledStart();
|
|
|
|
|
|
|
|
protected:
|
2012-07-27 21:10:47 +00:00
|
|
|
JobPrivate* const d;
|
2015-11-27 13:37:41 +00:00
|
|
|
|
|
|
|
friend class JobExecutor;
|
|
|
|
friend class JobPrivate;
|
|
|
|
friend class ReadPasswordJobPrivate;
|
|
|
|
friend class WritePasswordJobPrivate;
|
|
|
|
friend class DeletePasswordJobPrivate;
|
2012-04-05 14:16:22 +00:00
|
|
|
};
|
|
|
|
|
2012-07-27 21:10:47 +00:00
|
|
|
class ReadPasswordJobPrivate;
|
|
|
|
|
2016-08-17 13:20:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Job for reading secrets from the keychain.
|
|
|
|
* You can use a ReadPasswordJob to read passwords or binary data from the keychain.
|
|
|
|
* This job requires a "service" string, which is basically a namespace of keys within the keychain.
|
|
|
|
* This means that you can read all the pairs <key, secret> stored in the same service string.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
class QKEYCHAIN_EXPORT ReadPasswordJob : public Job {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2016-08-17 13:20:32 +00:00
|
|
|
/**
|
|
|
|
* Create a new ReadPasswordJob.
|
|
|
|
* @param service The service string used by this job (can be empty).
|
|
|
|
* @param parent The parent of this job.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
explicit ReadPasswordJob( const QString& service, QObject* parent=0 );
|
|
|
|
~ReadPasswordJob();
|
|
|
|
|
2016-08-17 13:20:32 +00:00
|
|
|
/**
|
|
|
|
* @return The binary data stored as value of this job's key().
|
|
|
|
* @see Job::key()
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
QByteArray binaryData() const;
|
2016-08-17 13:20:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The string stored as value of this job's key().
|
|
|
|
* @see Job::key()
|
|
|
|
* @warning Returns meaningless data if the data was stored as binary data.
|
|
|
|
* @see WritePasswordJob::setTextData()
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
QString textData() const;
|
|
|
|
|
|
|
|
private:
|
2012-07-27 21:10:47 +00:00
|
|
|
friend class QKeychain::ReadPasswordJobPrivate;
|
2012-04-05 14:16:22 +00:00
|
|
|
};
|
|
|
|
|
2012-07-27 21:10:47 +00:00
|
|
|
class WritePasswordJobPrivate;
|
|
|
|
|
2016-08-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
* @brief Job for writing secrets to the keychain.
|
|
|
|
* You can use a WritePasswordJob to store passwords or binary data in the keychain.
|
|
|
|
* This job requires a "service" string, which is basically a namespace of keys within the keychain.
|
|
|
|
* This means that you can store different pairs <key, secret> under the same service string.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
class QKEYCHAIN_EXPORT WritePasswordJob : public Job {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2016-08-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
* Create a new WritePasswordJob.
|
|
|
|
* @param service The service string used by this job (can be empty).
|
|
|
|
* @param parent The parent of this job.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
explicit WritePasswordJob( const QString& service, QObject* parent=0 );
|
|
|
|
~WritePasswordJob();
|
|
|
|
|
2016-08-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
* Set the @p data that the job will store in the keychain as binary data.
|
|
|
|
* @warning setBinaryData() and setTextData() are mutually exclusive.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
void setBinaryData( const QByteArray& data );
|
2016-08-17 13:07:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the @p data that the job will store in the keychain as string.
|
|
|
|
* Tipically @p data is a password.
|
|
|
|
* @warning setBinaryData() and setTextData() are mutually exclusive.
|
|
|
|
*/
|
2012-04-05 14:16:22 +00:00
|
|
|
void setTextData( const QString& data );
|
|
|
|
|
2011-10-27 16:14:37 +00:00
|
|
|
private:
|
2015-11-27 13:37:41 +00:00
|
|
|
|
2012-07-27 21:10:47 +00:00
|
|
|
friend class QKeychain::WritePasswordJobPrivate;
|
2011-10-27 16:14:37 +00:00
|
|
|
};
|
|
|
|
|
2012-07-27 21:10:47 +00:00
|
|
|
class DeletePasswordJobPrivate;
|
|
|
|
|
2012-05-07 14:56:52 +00:00
|
|
|
class QKEYCHAIN_EXPORT DeletePasswordJob : public Job {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit DeletePasswordJob( const QString& service, QObject* parent=0 );
|
|
|
|
~DeletePasswordJob();
|
|
|
|
|
|
|
|
private:
|
2012-07-27 21:10:47 +00:00
|
|
|
friend class QKeychain::DeletePasswordJobPrivate;
|
2012-05-07 14:56:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace QtKeychain
|
2011-10-27 19:17:54 +00:00
|
|
|
|
2011-10-27 16:14:37 +00:00
|
|
|
#endif
|