Skip to content
Open
Changes from all commits
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
47 changes: 36 additions & 11 deletions WUTokenHelper/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,52 @@ using namespace Windows::Security::Cryptography;

#define WU_NO_ACCOUNT MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x200)

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);
tokenBase64 = CryptographicBuffer::EncodeToBase64String(tokenBinary);
wprintf(L"Encoded token = %s\n", tokenBase64.c_str());
}

extern "C" __declspec(dllexport) int __stdcall GetWUToken(wchar_t** retToken) {
auto tokenBrokerStatics = get_activation_factory<TokenBrokerInternal, Windows::Foundation::IUnknown>();
auto statics = tokenBrokerStatics.as<ITokenBrokerInternalStatics>();
auto accounts = statics.FindAllAccountsAsync().get();
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());

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);
wprintf(L"Encoded token = %s\n", tokenBase64.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;
}