From f8aa5785ce170e5881fb9c3aac12b839ab49aaab Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Jan 2021 01:20:47 +0100 Subject: [PATCH] Make wxSecretValue always available, even when !wxUSE_SECRETSTORE This allows to write the code using this class without peppering it with wxUSE_SECRETSTORE checks that would otherwise be necessary to support Unix builds on system without libsecret. No real changes. --- include/wx/secretstore.h | 66 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/include/wx/secretstore.h b/include/wx/secretstore.h index 71c0c4cc64..cf74a5c983 100644 --- a/include/wx/secretstore.h +++ b/include/wx/secretstore.h @@ -12,10 +12,10 @@ #include "wx/defs.h" -#if wxUSE_SECRETSTORE - #include "wx/string.h" +#if wxUSE_SECRETSTORE + // Initial version of wxSecretStore required passing user name to Load(), which // didn't make much sense without support for multiple usernames per service, // so the API was changed to load the username too. Test for this symbol to @@ -168,6 +168,66 @@ private: wxSecretStoreImpl* const m_impl; }; -#endif // wxUSE_SECRETSTORE +#else // !wxUSE_SECRETSTORE + +// Provide stand in for wxSecretValue allowing to use it without having #if +// wxUSE_SECRETSTORE checks everywhere. Unlike the real version, this class +// doesn't provide any added security. +class wxSecretValue +{ +public: + wxSecretValue() { m_valid = false; } + + wxSecretValue(size_t size, const void *data) + { + Init(size, data); + } + + explicit wxSecretValue(const wxString& secret) + { + const wxScopedCharBuffer buf(secret.utf8_str()); + Init(buf.length(), buf.data()); + } + + bool IsOk() const { return m_valid; } + + bool operator==(const wxSecretValue& other) const + { + return m_valid == other.m_valid && m_data == other.m_data; + } + + bool operator!=(const wxSecretValue& other) const + { + return !(*this == other); + } + + size_t GetSize() const { return m_data.utf8_str().length(); } + + const void *GetData() const { return m_data.utf8_str().data(); } + + wxString GetAsString(const wxMBConv& conv = wxConvWhateverWorks) const + { + wxUnusedVar(conv); + return m_data; + } + + static void Wipe(size_t size, void *data) { memset(data, 0, size); } + static void WipeString(wxString& str) + { + str.assign(str.length(), '*'); + str.clear(); + } + +private: + void Init(size_t size, const void *data) + { + m_data = wxString::From8BitData(static_cast(data), size); + } + + wxString m_data; + bool m_valid; +}; + +#endif // wxUSE_SECRETSTORE/!wxUSE_SECRETSTORE #endif // _WX_SECRETSTORE_H_