#include <wx/secretstore.h>
A collection of secrets, sometimes called a key chain.
This class provides access to the secrets stored in the OS-provided facility, e.g. credentials manager under MSW, keychain under macOS or Freedesktop-compliant password storage mechanism such as GNOME keyring under Unix systems.
Currently only the access to the default keychain/ring is provided using GetDefault() method, support for other ones could be added in the future. After calling this method just call Save() to store a password entered by user and then call Load() to retrieve it during next program execution. See Secret Store Sample for an example of using this class.
The service
parameter of the methods in this class should describe the purpose of the password and be unique to your program, e.g. it could be "MyCompany/MyProgram/SomeServer". Note that the server name must be included in the string to allow storing passwords for more than one server.
Notice that this class is always available under MSW (except when using MinGW32 which doesn't provide the required wincred.h
header) and macOS but requires libsecret (see https://developer.gnome.org/libsecret/) under Unix and may not be compiled in if it wasn't found. You can check wxUSE_SECRETSTORE
to test for this. Moreover, retrieving the default secret store may also fail under Unix during run-time if the desktop environment doesn't provide one, so don't forget to call IsOk() to check for this too.
Example of storing credentials using this class:
And to load it back:
Public Member Functions | |
bool | IsOk (wxString *errmsg=NULL) const |
Check if this object can actually be used. More... | |
bool | Save (const wxString &service, const wxString &username, const wxSecretValue &password) |
Store a username/password combination. More... | |
bool | Load (const wxString &service, wxString &username, wxSecretValue &password) const |
Look up the username/password for the given service. More... | |
bool | Delete (const wxString &service) |
Delete a previously stored username/password combination. More... | |
Static Public Member Functions | |
static wxSecretStore | GetDefault () |
Returns the default secrets collection to use. More... | |
bool wxSecretStore::Delete | ( | const wxString & | service | ) |
Delete a previously stored username/password combination.
If anything was deleted, returns true. Otherwise returns false and logs an error if any error other than not finding any matches occurred.
|
static |
Returns the default secrets collection to use.
Call IsOk() on the returned object to check if this method succeeded.
Note that this method may show a dialog to the user under some platforms, so it can take an arbitrarily long time to return.
bool wxSecretStore::IsOk | ( | wxString * | errmsg = NULL | ) | const |
Check if this object can actually be used.
errmsg | If not NULL, this parameter is filled with a user-readable error message explaining why the secret store can't be used (this argument is new since wxWidgets 3.1.4) |
bool wxSecretStore::Load | ( | const wxString & | service, |
wxString & | username, | ||
wxSecretValue & | password | ||
) | const |
Look up the username/password for the given service.
If no username/password is found for the given service, false is returned.
Otherwise the function returns true and updates the provided username and password arguments.
bool wxSecretStore::Save | ( | const wxString & | service, |
const wxString & | username, | ||
const wxSecretValue & | password | ||
) |
Store a username/password combination.
The service name should be user readable and unique.
If a secret with the same service name already exists, it will be overwritten with the new value. In particular, notice that it is not currently allowed to store passwords for different usernames for the same service, even if the underlying platform API supports this (as is the case for macOS but not MSW).
Returns false after logging an error message if an error occurs, otherwise returns true indicating that the secret has been stored and can be retrieved by calling Load() later.