Debian Codegraph @

plugins/vcs/CredentialManager.h

raw
1 #pragma once
2
3 #include <string>
4
5 #ifdef _MSC_VER
6 #define WIN32_LEAN_AND_MEAN
7 #include <windows.h>
8 #include <wincred.h>
9 #include <tchar.h>
10 #endif
11 #include "string/encoding.h"
12
13 namespace vcs
14 {
15
16 class CredentialManager
17 {
18 public:
19 static std::pair<std::string, std::string> RetrievePassword(const std::string& accountName)
20 {
21 return RetrievePassword(string::utf8_to_unicode(accountName));
22 }
23
24 static std::pair<std::string, std::string> RetrievePassword(const std::wstring& accountName)
25 {
26 #ifdef _MSC_VER
27 PCREDENTIALW pcred;
28 auto ok = ::CredReadW(accountName.c_str(), CRED_TYPE_GENERIC, 0, &pcred);
29
30 if (!ok)
31 {
32 return std::make_pair("", "");
33 }
34
35 auto user = string::unicode_to_utf8(pcred->UserName);
36
37 // Extract N characters from the credential blob
38 std::wstring blob;
39 blob.assign(reinterpret_cast<wchar_t*>(pcred->CredentialBlob), pcred->CredentialBlobSize / sizeof(wchar_t));
40
41 auto pw = string::unicode_to_utf8(blob);
42
43 // Clear out the temporary string
44 memset(blob.data(), '\0', blob.size() * sizeof(wchar_t));
45
46 ::CredFree(pcred);
47
48 // Move-return the result
49 return std::make_pair(std::move(user), std::move(pw));
50 #else
51 return { "", "" };
52 #endif
53 }
54 };
55
56 }