forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWslcCredentialStore.cpp
More file actions
69 lines (54 loc) · 1.98 KB
/
WslcCredentialStore.cpp
File metadata and controls
69 lines (54 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WslcCredentialStore.cpp
Abstract:
Implementation of credential store helpers.
--*/
#include "WslcCredentialStore.h"
#include <format>
#include <wincrypt.h>
std::string wsl::windows::common::BuildRegistryAuthHeader(
const std::string& username, const std::string& password, const std::string& serverAddress)
{
auto authJson = std::format(
R"({{"username":"{}","password":"{}","serveraddress":"{}"}})", username, password, serverAddress);
DWORD base64Size = 0;
THROW_IF_WIN32_BOOL_FALSE(CryptBinaryToStringA(
reinterpret_cast<const BYTE*>(authJson.c_str()),
static_cast<DWORD>(authJson.size()),
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
nullptr,
&base64Size));
std::string result(base64Size, '\0');
THROW_IF_WIN32_BOOL_FALSE(CryptBinaryToStringA(
reinterpret_cast<const BYTE*>(authJson.c_str()),
static_cast<DWORD>(authJson.size()),
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
result.data(),
&base64Size));
result.resize(base64Size);
return result;
}
std::string wsl::windows::common::BuildRegistryAuthHeader(
const std::string& identityToken, const std::string& serverAddress)
{
auto authJson = std::format(
R"({{"identitytoken":"{}","serveraddress":"{}"}})", identityToken, serverAddress);
DWORD base64Size = 0;
THROW_IF_WIN32_BOOL_FALSE(CryptBinaryToStringA(
reinterpret_cast<const BYTE*>(authJson.c_str()),
static_cast<DWORD>(authJson.size()),
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
nullptr,
&base64Size));
std::string result(base64Size, '\0');
THROW_IF_WIN32_BOOL_FALSE(CryptBinaryToStringA(
reinterpret_cast<const BYTE*>(authJson.c_str()),
static_cast<DWORD>(authJson.size()),
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
result.data(),
&base64Size));
result.resize(base64Size);
return result;
}