Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WUTokenHelper/WUTokenHelper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="main.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions WUTokenHelper/WUTokenHelper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand Down
42 changes: 34 additions & 8 deletions WUTokenHelper/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pch.h"
#include "combaseapi.h"
#include <thread>
#include "main.h"

using namespace winrt;
using namespace Windows::Foundation;
Expand All @@ -17,20 +18,45 @@ extern "C" __declspec(dllexport) int __stdcall GetWUToken(wchar_t** retToken) {
wprintf(L"Account count = %i\n", accounts.Size());
if (accounts.Size() == 0)
return WU_NO_ACCOUNT;
auto accountInfo = accounts.GetAt(0);
wprintf(L"ID = %s\n", accountInfo.Id().c_str());
wprintf(L"Name = %s\n", accountInfo.UserName().c_str());

hstring tokenBase64;

// Loop the accounts on the system incase
// the first doesn't have access to the token
for (size_t i = 0; i < accounts.Size(); i++)
{
auto accountInfo = accounts.GetAt(i);
wprintf(L"ID = %s\n", accountInfo.Id().c_str());
wprintf(L"Name = %s\n", accountInfo.UserName().c_str());

try {
TryGetToken(accountInfo, tokenBase64);
}
catch (...) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. I'd be happier if it caught something specific.

Moreover, it shouldn't just silently swallow an error if one occurs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no expert at C++ so wouldn't know the best way of doing that, could you implement it?

if (i == accounts.Size() - 1) {
throw std::current_exception();
}
}

if (tokenBase64.size() >= 1) {
break;
}
}

*retToken = (wchar_t*)::CoTaskMemAlloc((tokenBase64.size() + 1) * sizeof(wchar_t));
memcpy(*retToken, tokenBase64.data(), (tokenBase64.size() + 1) * sizeof(wchar_t));

return S_OK;
}

void TryGetToken(winrt::Windows::Security::Credentials::WebAccount& accountInfo, winrt::hstring& tokenBase64)
{
auto accountProvider = WebAuthenticationCoreManager::FindAccountProviderAsync(L"https://login.microsoft.com", L"consumers").get();
WebTokenRequest request(accountProvider, L"service::dcat.update.microsoft.com::MBI_SSL", L"{28520974-CE92-4F36-A219-3F255AF7E61E}");
auto result = WebAuthenticationCoreManager::GetTokenSilentlyAsync(request, accountInfo).get();
auto token = result.ResponseData().GetAt(0).Token();
wprintf(L"Token = %s\n", token.c_str());
auto tokenBinary = CryptographicBuffer::ConvertStringToBinary(token, BinaryStringEncoding::Utf16LE);
auto tokenBase64 = CryptographicBuffer::EncodeToBase64String(tokenBinary);
tokenBase64 = CryptographicBuffer::EncodeToBase64String(tokenBinary);
wprintf(L"Encoded token = %s\n", tokenBase64.c_str());

*retToken = (wchar_t*)::CoTaskMemAlloc((tokenBase64.size() + 1) * sizeof(wchar_t));
memcpy(*retToken, tokenBase64.data(), (tokenBase64.size() + 1) * sizeof(wchar_t));
return S_OK;
}
1 change: 1 addition & 0 deletions WUTokenHelper/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void GetRawToken(winrt::Windows::Security::Credentials::WebAccount& accountInfo, winrt::hstring& tokenBase64);