qtkeychain/keychain.h

140 lines
4.5 KiB
C
Raw Normal View History

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-28 19:27:03 +00:00
#include "qkeychain_export.h"
2011-10-27 16:15:46 +00:00
#include <QtCore/QString>
2011-10-27 16:14:37 +00:00
class QSettings;
2011-10-27 19:17:54 +00:00
namespace QKeychain {
2011-10-27 18:46:23 +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
*
* 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-28 19:27:03 +00:00
class QKEYCHAIN_EXPORT Keychain {
2011-10-27 16:14:37 +00:00
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,
2011-10-28 07:23:09 +00:00
* to disambiguate keys and avoid clashes with other applications.
* Must not be empty.
* @param settings An optional settings object that is used to store the encrypted data
* if no keychain is available on the platform. Currently only used on Windows.
* If 0, a default-constructed QSettings object will be used.
2011-10-27 18:46:23 +00:00
*/
explicit Keychain( const QString& service, QSettings* settings=0 );
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-28 07:23:09 +00:00
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 */
EntryAlreadyExists, /**< There is already an entry for the given key and overwriting was not enforced */
2011-11-14 09:56:55 +00:00
NotImplemented, /**< Not implemented on platform */
2011-10-28 07:23:09 +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
/**
* 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
/**
* 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.
*
* @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
*/
void writePassword( const QString& key,
const QString& password );
2011-10-27 18:46:23 +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 );
/**
* 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-28 07:38:02 +00:00
* @param key the key to read the password for
2011-10-27 18:46:23 +00:00
*/
QString readPassword( const QString& key );
2011-10-27 18:46:23 +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-28 07:38:02 +00:00
* @param key the key to read the password for
*/
QByteArray readEntry( const QString& key );
2011-10-28 07:38:02 +00:00
/**
* Returns whether the keychain has an entry with key @p key
* error() and errorString() hold the result of the read operation.
*
* @param key the key to check for
*/
bool entryExists( 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
*/
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