KiCad PCB EDA Suite
Loading...
Searching...
No Matches
windows/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 <windows.h>
23#include <wincred.h>
24#include <wx/string.h>
25
26namespace KIPLATFORM
27{
28 namespace SECRETS
29 {
30 bool StoreSecret( const wxString& aService, const wxString& aKey, const wxString& aSecret )
31 {
32 wxString display = aService + wxS( ":" ) + aKey;
33
34 // Store the UTF-8 string in a variable to keep it alive during the API call
35 wxScopedCharBuffer utf8Secret = aSecret.utf8_str();
36
37 CREDENTIALW cred = { 0 };
38 cred.Type = CRED_TYPE_GENERIC;
39 cred.TargetName = (LPWSTR) display.wc_str();
40 cred.CredentialBlobSize = (DWORD) utf8Secret.length();
41 cred.CredentialBlob = (LPBYTE) utf8Secret.data();
42 cred.Persist = CRED_PERSIST_ENTERPRISE;
43
44 return CredWriteW( &cred, 0 );
45 }
46
47 void StoreSecretAsync( const wxString& aService, const wxString& aKey, const wxString& aSecret,
48 std::function<void( bool )> aDone )
49 {
50 // Credential Manager never prompts, so storing cannot hold the event loop
51 bool stored = StoreSecret( aService, aKey, aSecret );
52
53 if( aDone )
54 aDone( stored );
55 }
56
57 bool GetSecret( const wxString& aService, const wxString& aKey, wxString& aSecret )
58 {
59 wxString display = aService + wxS( ":" ) + aKey;
60
61 CREDENTIALW* cred = nullptr;
62 bool result = CredReadW( display.wc_str(), CRED_TYPE_GENERIC, 0, &cred );
63
64 if( result )
65 {
66 aSecret = wxString::FromUTF8( (const char*) cred->CredentialBlob,
67 cred->CredentialBlobSize );
68 CredFree( cred );
69 }
70
71 return result;
72 }
73
74 void GetSecretAsync( const wxString& aService, const wxString& aKey,
75 std::function<void( bool, const wxString& )> aDone )
76 {
77 // Credential Manager never prompts, so reading cannot hold the event loop
78 wxString secret;
79 bool found = GetSecret( aService, aKey, secret );
80
81 aDone( found, secret );
82 }
83
84 bool GetSecretNoPrompt( const wxString& aService, const wxString& aKey, wxString& aSecret )
85 {
86 return GetSecret( aService, aKey, aSecret );
87 }
88
89 bool DeleteSecret( const wxString& aService, const wxString& aKey )
90 {
91 wxString display = aService + wxS( ":" ) + aKey;
92 return CredDeleteW( display.wc_str(), CRED_TYPE_GENERIC, 0 );
93 }
94 }
95}
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)
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...
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...
wxString result
Test unit parsing edge cases and error handling.