Version: 3.1.1
wxSecretStore Class Reference

#include <wx/secretstore.h>

Detailed Description

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 OS X 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 OS X 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:

if ( store.IsOk() )
{
if ( !store.Save("MyApp/MyService", username, password) )
wxLogWarning("Failed to save credentials to the system secret store.");
}
else
{
wxLogWarning("This system doesn't support storing passwords securely.");
}

And to load it back:

if ( store.IsOk() )
{
wxString username;
wxSecretValue password;
if ( store.Load("MyApp/MyService", username, password) )
... use the password ...
}

Library:  wxBase
Category:  Miscellaneous
Since
3.1.1

Public Member Functions

bool IsOk () const
 Check if this object is valid. 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...
 

Member Function Documentation

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 wxSecretStore wxSecretStore::GetDefault ( )
static

Returns the default secrets collection to use.

Call IsOk() on the returned object to check if this method succeeded.

bool wxSecretStore::IsOk ( ) const

Check if this object is valid.

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.