2011-10-27 16:15:46 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (C) 2011 Frank Osterfeld <frank.osterfeld@gmail.com> *
|
|
|
|
* *
|
|
|
|
* 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-27 16:15:46 +00:00
|
|
|
#include <QtCore/QString>
|
2011-10-27 16:14:37 +00:00
|
|
|
|
2011-10-27 19:17:54 +00:00
|
|
|
namespace QKeychain {
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
2011-10-27 19:36:51 +00:00
|
|
|
* Provides access to platform-specific key stores for secure persistence of
|
|
|
|
* passwords and other sensitive user data.
|
2011-10-27 18:46:23 +00:00
|
|
|
*
|
2011-10-27 19:36:51 +00:00
|
|
|
* On Windows, TODO
|
|
|
|
* On Mac OS X, the OS X keychain is used.
|
|
|
|
* On other Unixes, TODO
|
|
|
|
*
|
|
|
|
* TODO we don't guarantee anything
|
2011-10-27 18:46:23 +00:00
|
|
|
*/
|
2011-10-27 16:14:37 +00:00
|
|
|
class Keychain {
|
|
|
|
public:
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a Keychain object.
|
|
|
|
*
|
|
|
|
* @param service The service name of your service/application. Used as identifier,
|
|
|
|
* to disambiguate and avoid clashes with other applications.
|
|
|
|
*/
|
2011-10-27 16:14:37 +00:00
|
|
|
explicit Keychain( const QString& service );
|
2011-10-27 18:46:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
2011-10-27 16:14:37 +00:00
|
|
|
~Keychain();
|
|
|
|
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
|
|
|
* Error codes
|
|
|
|
*/
|
2011-10-27 16:15:46 +00:00
|
|
|
enum Error {
|
2011-10-27 18:46:23 +00:00
|
|
|
NoError=0, /*< No error occurred, operation was successful */
|
2011-10-27 19:36:51 +00:00
|
|
|
EntryNotFound, /*< For the given key no data was found */
|
|
|
|
CouldNotDeleteEntry, /*< Could not delete existing secret data */
|
2011-10-27 18:46:23 +00:00
|
|
|
AccessDeniedByUser, /*< User denied access to keychain */
|
|
|
|
AccessDenied, /*< Access denied for other reasons */
|
2011-10-27 19:36:51 +00:00
|
|
|
EntryAlreadyExists, /*< There is already an entry for the given key and overwriting was not enforced */
|
2011-10-27 18:46:23 +00:00
|
|
|
OtherError /*< Something else went wrong (errorString() might provide details) */
|
2011-10-27 16:15:46 +00:00
|
|
|
};
|
|
|
|
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
|
|
|
* Overwrite mode when writing passwords to the keychain
|
|
|
|
*/
|
2011-10-27 16:15:46 +00:00
|
|
|
enum OverwriteMode {
|
2011-10-27 18:46:23 +00:00
|
|
|
DoNotOverwrite, /*< Do not overwrite existing entries */
|
2011-10-27 19:36:51 +00:00
|
|
|
ForceOverwrite /*< Replace old data by new one */
|
2011-10-27 16:15:46 +00:00
|
|
|
};
|
|
|
|
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
|
|
|
* The service name used as identifier.
|
|
|
|
*/
|
2011-10-27 16:14:37 +00:00
|
|
|
QString service() const;
|
2011-10-27 18:46:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The error code of the last operation.
|
|
|
|
*/
|
2011-10-27 16:15:46 +00:00
|
|
|
Error error() const;
|
2011-10-27 18:46:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Human-readable error description of the last operation.
|
|
|
|
*/
|
2011-10-27 16:15:46 +00:00
|
|
|
QString errorString() const;
|
2011-10-27 16:14:37 +00:00
|
|
|
|
2011-10-27 18:46:23 +00:00
|
|
|
/**
|
2011-10-27 19:36:51 +00:00
|
|
|
* Stores a @p password in the keychain, for a given @p key.
|
2011-10-27 18:46:23 +00:00
|
|
|
* error() and errorString() hold the result of the write operation.
|
|
|
|
*
|
2011-10-27 19:36:51 +00:00
|
|
|
* @param key the key to store a password for
|
|
|
|
* @param password the password to store
|
2011-10-27 18:46:23 +00:00
|
|
|
* @param om Whether to overwrite existing passwords
|
|
|
|
*/
|
2011-10-27 19:36:51 +00:00
|
|
|
void writePassword( const QString& key,
|
2011-10-27 16:15:46 +00:00
|
|
|
const QString& password,
|
|
|
|
OverwriteMode om=DoNotOverwrite );
|
2011-10-27 18:46:23 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-27 19:36:51 +00:00
|
|
|
* Stores @p data in the keychain, for a given @p key.
|
|
|
|
* error() and errorString() hold the result of the write operation.
|
|
|
|
*
|
|
|
|
* @param key the key to store a password for
|
|
|
|
* @param data the data to store
|
|
|
|
* @param om Whether to overwrite existing passwords
|
|
|
|
*/
|
|
|
|
void writeEntry( const QString& key,
|
|
|
|
const QByteArray& data,
|
|
|
|
OverwriteMode om=DoNotOverwrite );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the password for a given @p key from the keychain.
|
2011-10-27 18:46:23 +00:00
|
|
|
* error() and errorString() hold the result of the read operation.
|
|
|
|
*
|
2011-10-27 19:36:51 +00:00
|
|
|
* @param key the key ot read the password for
|
2011-10-27 18:46:23 +00:00
|
|
|
*/
|
2011-10-27 19:36:51 +00:00
|
|
|
QString readPassword( const QString& key );
|
2011-10-27 18:46:23 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-27 19:36:51 +00:00
|
|
|
* Reads data for a given @p key from the keychain.
|
2011-10-27 18:46:23 +00:00
|
|
|
* error() and errorString() hold the result of the read operation.
|
|
|
|
*
|
2011-10-27 19:36:51 +00:00
|
|
|
* @param key the key ot read the password for
|
|
|
|
*/
|
|
|
|
QByteArray readEntry( const QString& key );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the data for a @p key from the keychain.
|
|
|
|
* error() and errorString() hold the result of the delete operation.
|
|
|
|
*
|
|
|
|
* @param key The key to delete the data for
|
2011-10-27 18:46:23 +00:00
|
|
|
*/
|
2011-10-27 19:36:51 +00:00
|
|
|
void deleteEntry( const QString& key );
|
2011-10-27 16:14:37 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
class Private;
|
|
|
|
Private* const d;
|
|
|
|
Q_DISABLE_COPY(Keychain)
|
|
|
|
};
|
|
|
|
|
2011-10-27 19:17:54 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 16:14:37 +00:00
|
|
|
#endif
|