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
65 lines (49 loc) · 1.55 KB
/
WslcCredentialStore.cpp
File metadata and controls
65 lines (49 loc) · 1.55 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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WslcCredentialStore.cpp
Abstract:
Implementation of credential store helpers.
--*/
#include "WslcCredentialStore.h"
#include <nlohmann/json.hpp>
#include <wincrypt.h>
namespace {
std::string Base64Encode(const std::string& input)
{
DWORD base64Size = 0;
THROW_IF_WIN32_BOOL_FALSE(CryptBinaryToStringA(
reinterpret_cast<const BYTE*>(input.c_str()),
static_cast<DWORD>(input.size()),
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
nullptr,
&base64Size));
auto buffer = std::make_unique<char[]>(base64Size);
THROW_IF_WIN32_BOOL_FALSE(CryptBinaryToStringA(
reinterpret_cast<const BYTE*>(input.c_str()),
static_cast<DWORD>(input.size()),
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
buffer.get(),
&base64Size));
return std::string(buffer.get());
}
} // namespace
std::string wsl::windows::common::BuildRegistryAuthHeader(
const std::string& username, const std::string& password, const std::string& serverAddress)
{
nlohmann::json authJson = {
{"username", username},
{"password", password},
{"serveraddress", serverAddress}
};
return Base64Encode(authJson.dump());
}
std::string wsl::windows::common::BuildRegistryAuthHeader(
const std::string& identityToken, const std::string& serverAddress)
{
nlohmann::json authJson = {
{"identitytoken", identityToken},
{"serveraddress", serverAddress}
};
return Base64Encode(authJson.dump());
}