KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unix/secrets.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5*
6* This program is free software: you can redistribute it and/or modify it
7* under the terms of the GNU General Public License as published by the
8* Free Software Foundation, either version 3 of the License, or (at your
9* option) any later version.
10*
11* This program is distributed in the hope that it will be useful, but
12* WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20#include <kiplatform/secrets.h>
21
22#include <libsecret/secret.h>
23
24#include <memory>
25
26namespace KIPLATFORM
27{
28 namespace SECRETS
29 {
30 static const SecretSchema schema =
31 {
32 "org.kicad.kicad", SECRET_SCHEMA_NONE,
33 {
34 { "service", SECRET_SCHEMA_ATTRIBUTE_STRING },
35 { "key", SECRET_SCHEMA_ATTRIBUTE_STRING },
36 { nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING }
37 }
38 };
39
40 bool StoreSecret( const wxString& aService, const wxString& aKey, const wxString& aSecret )
41 {
42 GError* error = nullptr;
43 wxString display = aService + ":" + aKey;
44
45 // libsecret takes UTF-8, not the locale encoding
46 wxScopedCharBuffer utf8Display = display.utf8_str();
47 wxScopedCharBuffer utf8Secret = aSecret.utf8_str();
48 wxScopedCharBuffer utf8Service = aService.utf8_str();
49 wxScopedCharBuffer utf8Key = aKey.utf8_str();
50
51 secret_password_store_sync( &schema,
52 SECRET_COLLECTION_DEFAULT,
53 utf8Display.data(), // Display name
54 utf8Secret.data(), // Secret value
55 nullptr,
56 &error,
57 "service", utf8Service.data(),
58 "key", utf8Key.data(),
59 nullptr );
60
61 if( error )
62 {
63 g_error_free( error );
64 return false;
65 }
66
67 return true;
68 }
69
70 static void storeFinished( GObject* aSource, GAsyncResult* aResult, gpointer aDone )
71 {
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 );
74
75 if( *done )
76 ( *done )( stored );
77 }
78
79 void StoreSecretAsync( const wxString& aService, const wxString& aKey, const wxString& aSecret,
80 std::function<void( bool )> aDone )
81 {
82 wxString display = aService + ":" + aKey;
83
84 wxScopedCharBuffer utf8Display = display.utf8_str();
85 wxScopedCharBuffer utf8Secret = aSecret.utf8_str();
86 wxScopedCharBuffer utf8Service = aService.utf8_str();
87 wxScopedCharBuffer utf8Key = aKey.utf8_str();
88
89 // The sync call spins a private main context while any unlock prompt is up, which starves the
90 // GTK loop; the async one finishes on the default context the GTK loop already iterates
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 );
94 }
95
96 bool GetSecret( const wxString& aService, const wxString& aKey, wxString& aSecret )
97 {
98 GError* error = nullptr;
99 wxScopedCharBuffer utf8Service = aService.utf8_str();
100 wxScopedCharBuffer utf8Key = aKey.utf8_str();
101 gchar* secret = secret_password_lookup_sync( &schema,
102 nullptr,
103 &error,
104 "service", utf8Service.data(),
105 "key", utf8Key.data(),
106 nullptr );
107
108 if( error )
109 {
110 g_error_free( error );
111 return false;
112 }
113
114 // libsecret reports a missing entry as a null secret with no error
115 if( !secret )
116 return false;
117
118 aSecret = wxString::FromUTF8( secret );
119 g_free( secret );
120
121 return true;
122 }
123
124 static void lookupFinished( GObject* aSource, GAsyncResult* aResult, gpointer aDone )
125 {
126 using DONE = std::function<void( bool, const wxString& )>;
127
128 std::unique_ptr<DONE> done( static_cast<DONE*>( aDone ) );
129 gchar* secret = secret_password_lookup_finish( aResult, nullptr );
130
131 // A missing entry and a failed lookup both come back as a null secret
132 if( !secret )
133 {
134 ( *done )( false, wxEmptyString );
135 return;
136 }
137
138 wxString value = wxString::FromUTF8( secret );
139 g_free( secret );
140 ( *done )( true, value );
141 }
142
143 void GetSecretAsync( const wxString& aService, const wxString& aKey,
144 std::function<void( bool, const wxString& )> aDone )
145 {
146 wxScopedCharBuffer utf8Service = aService.utf8_str();
147 wxScopedCharBuffer utf8Key = aKey.utf8_str();
148
149 // Finishes on the default context the GTK loop iterates, so an unlock prompt stays drawable
150 secret_password_lookup( &schema, nullptr, lookupFinished,
151 new std::function<void( bool, const wxString& )>( std::move( aDone ) ),
152 "service", utf8Service.data(), "key", utf8Key.data(), nullptr );
153 }
154
155 bool GetSecretNoPrompt( const wxString& aService, const wxString& aKey, wxString& aSecret )
156 {
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(),
163 nullptr );
164
165 // Without SECRET_SEARCH_UNLOCK, items in a locked collection come back locked and unread
166 GList* items = secret_service_search_sync( nullptr, &schema, attributes,
167 static_cast<SecretSearchFlags>( SECRET_SEARCH_ALL
168 | SECRET_SEARCH_LOAD_SECRETS ),
169 nullptr, &error );
170 g_hash_table_unref( attributes );
171
172 if( error )
173 {
174 g_error_free( error );
175 return false;
176 }
177
178 bool found = false;
179
180 for( GList* it = items; it && !found; it = it->next )
181 {
182 SecretItem* item = SECRET_ITEM( it->data );
183
184 if( secret_item_get_locked( item ) )
185 continue;
186
187 if( SecretValue* value = secret_item_get_secret( item ) )
188 {
189 if( const gchar* text = secret_value_get_text( value ) )
190 {
191 aSecret = wxString::FromUTF8( text );
192 found = true;
193 }
194
195 secret_value_unref( value );
196 }
197 }
198
199 g_list_free_full( items, g_object_unref );
200 return found;
201 }
202
203 bool DeleteSecret( const wxString& aService, const wxString& aKey )
204 {
205 GError* error = nullptr;
206 wxScopedCharBuffer utf8Service = aService.utf8_str();
207 wxScopedCharBuffer utf8Key = aKey.utf8_str();
208
209 secret_password_clear_sync( &schema,
210 nullptr,
211 &error,
212 "service", utf8Service.data(),
213 "key", utf8Key.data(),
214 nullptr );
215
216 if( error )
217 {
218 g_error_free( error );
219 return false;
220 }
221
222 return true;
223 }
224 }
225}
static void lookupFinished(GObject *aSource, GAsyncResult *aResult, gpointer aDone)
bool DeleteSecret(const wxString &aService, const wxString &aKey)
bool StoreSecret(const wxString &aService, const wxString &aKey, const wxString &aSecret)
bool GetSecret(const wxString &aService, const wxString &aKey, wxString &aSecret)
static const SecretSchema schema
bool GetSecretNoPrompt(const wxString &aService, const wxString &aKey, wxString &aSecret)
Like GetSecret(), but never waits on the user.
void StoreSecretAsync(const wxString &aService, const wxString &aKey, const wxString &aSecret, std::function< void(bool)> aDone=nullptr)
Like StoreSecret(), but never blocks the event loop while the platform asks the user to unlock the st...
static void storeFinished(GObject *aSource, GAsyncResult *aResult, gpointer aDone)
void GetSecretAsync(const wxString &aService, const wxString &aKey, std::function< void(bool, const wxString &)> aDone)
Like GetSecret(), but never blocks the event loop while the platform asks the user to unlock the stor...