22#include <libsecret/secret.h>
32 "org.kicad.kicad", SECRET_SCHEMA_NONE,
34 {
"service", SECRET_SCHEMA_ATTRIBUTE_STRING },
35 {
"key", SECRET_SCHEMA_ATTRIBUTE_STRING },
36 {
nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING }
40 bool StoreSecret(
const wxString& aService,
const wxString& aKey,
const wxString& aSecret )
42 GError* error =
nullptr;
43 wxString display = aService +
":" + aKey;
46 wxScopedCharBuffer utf8Display = display.utf8_str();
47 wxScopedCharBuffer utf8Secret = aSecret.utf8_str();
48 wxScopedCharBuffer utf8Service = aService.utf8_str();
49 wxScopedCharBuffer utf8Key = aKey.utf8_str();
51 secret_password_store_sync( &
schema,
52 SECRET_COLLECTION_DEFAULT,
57 "service", utf8Service.data(),
58 "key", utf8Key.data(),
63 g_error_free( error );
70 static void storeFinished( GObject* aSource, GAsyncResult* aResult, gpointer aDone )
72 std::unique_ptr<std::function<void(
bool )>> done(
static_cast<std::function<
void(
bool )
>*>( aDone ) );
73 bool stored = secret_password_store_finish( aResult,
nullptr );
79 void StoreSecretAsync(
const wxString& aService,
const wxString& aKey,
const wxString& aSecret,
80 std::function<
void(
bool )> aDone )
82 wxString display = aService +
":" + aKey;
84 wxScopedCharBuffer utf8Display = display.utf8_str();
85 wxScopedCharBuffer utf8Secret = aSecret.utf8_str();
86 wxScopedCharBuffer utf8Service = aService.utf8_str();
87 wxScopedCharBuffer utf8Key = aKey.utf8_str();
91 secret_password_store( &
schema, SECRET_COLLECTION_DEFAULT, utf8Display.data(), utf8Secret.data(),
nullptr,
92 storeFinished,
new std::function<
void(
bool )>( std::move( aDone ) ),
"service",
93 utf8Service.data(),
"key", utf8Key.data(),
nullptr );
96 bool GetSecret(
const wxString& aService,
const wxString& aKey, wxString& aSecret )
98 GError* error =
nullptr;
99 wxScopedCharBuffer utf8Service = aService.utf8_str();
100 wxScopedCharBuffer utf8Key = aKey.utf8_str();
101 gchar* secret = secret_password_lookup_sync( &
schema,
104 "service", utf8Service.data(),
105 "key", utf8Key.data(),
110 g_error_free( error );
118 aSecret = wxString::FromUTF8( secret );
124 static void lookupFinished( GObject* aSource, GAsyncResult* aResult, gpointer aDone )
126 using DONE = std::function<void(
bool,
const wxString& )>;
128 std::unique_ptr<DONE> done(
static_cast<DONE*
>( aDone ) );
129 gchar* secret = secret_password_lookup_finish( aResult,
nullptr );
134 ( *done )(
false, wxEmptyString );
138 wxString value = wxString::FromUTF8( secret );
140 ( *done )(
true, value );
144 std::function<
void(
bool,
const wxString& )> aDone )
146 wxScopedCharBuffer utf8Service = aService.utf8_str();
147 wxScopedCharBuffer utf8Key = aKey.utf8_str();
151 new std::function<
void(
bool,
const wxString& )>( std::move( aDone ) ),
152 "service", utf8Service.data(),
"key", utf8Key.data(),
nullptr );
157 GError* error =
nullptr;
158 wxScopedCharBuffer utf8Service = aService.utf8_str();
159 wxScopedCharBuffer utf8Key = aKey.utf8_str();
160 GHashTable* attributes = secret_attributes_build( &
schema,
161 "service", utf8Service.data(),
162 "key", utf8Key.data(),
166 GList* items = secret_service_search_sync(
nullptr, &
schema, attributes,
167 static_cast<SecretSearchFlags
>( SECRET_SEARCH_ALL
168 | SECRET_SEARCH_LOAD_SECRETS ),
170 g_hash_table_unref( attributes );
174 g_error_free( error );
180 for( GList* it = items; it && !found; it = it->next )
182 SecretItem* item = SECRET_ITEM( it->data );
184 if( secret_item_get_locked( item ) )
187 if( SecretValue* value = secret_item_get_secret( item ) )
189 if(
const gchar*
text = secret_value_get_text( value ) )
191 aSecret = wxString::FromUTF8(
text );
195 secret_value_unref( value );
199 g_list_free_full( items, g_object_unref );
205 GError* error =
nullptr;
206 wxScopedCharBuffer utf8Service = aService.utf8_str();
207 wxScopedCharBuffer utf8Key = aKey.utf8_str();
209 secret_password_clear_sync( &
schema,
212 "service", utf8Service.data(),
213 "key", utf8Key.data(),
218 g_error_free( error );