Skip to content

Commit 5bdda3f

Browse files
committed
Improve messages displayed on authentication failure
1 parent d0038e7 commit 5bdda3f

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

MCLauncher/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ private void InvokeDownload(Version v) {
338338
Debug.WriteLine("Authentication complete");
339339
} catch (WUTokenHelper.WUTokenException e) {
340340
Debug.WriteLine("Authentication failed:\n" + e.ToString());
341-
MessageBox.Show("Failed to authenticate because: " + e.Message + "\n\n" + e.ToString(), "Authentication failed");
341+
MessageBox.Show("Failed to authenticate because: " + e.Message, "Authentication failed");
342342
v.StateChangeInfo = null;
343343
return;
344344
} catch (Exception e) {

MCLauncher/WUTokenHelper.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using Windows.Security.Authentication.Web.Core;
34

45
namespace MCLauncher {
56
class WUTokenHelper {
@@ -19,9 +20,13 @@ public static string GetWUToken() {
1920
}
2021
}
2122

22-
private const int WU_ERRORS_START = 0x7ffc0200;
23-
private const int WU_NO_ACCOUNT = 0x7ffc0200;
24-
private const int WU_ERRORS_END = 0x7ffc0200;
23+
private const int WU_ERRORS_START = unchecked((int) 0x80040200);
24+
private const int WU_NO_ACCOUNT = unchecked((int) 0x80040200);
25+
26+
private const int WU_TOKEN_FETCH_ERROR_BASE = unchecked((int) 0x80040300);
27+
private const int WU_TOKEN_FETCH_ERROR_END = unchecked((int) 0x80040400);
28+
29+
private const int WU_ERRORS_END = unchecked((int) 0x80040400);
2530

2631
[DllImport("WUTokenHelper.dll", CallingConvention = CallingConvention.StdCall)]
2732
private static extern int GetWUToken([MarshalAs(UnmanagedType.LPWStr)] out string token);
@@ -31,8 +36,33 @@ public WUTokenException(int exception) : base(GetExceptionText(exception)) {
3136
HResult = exception;
3237
}
3338
private static String GetExceptionText(int e) {
39+
if (e >= WU_TOKEN_FETCH_ERROR_BASE && e < WU_TOKEN_FETCH_ERROR_END)
40+
{
41+
var actualCode = (byte) e & 0xff;
42+
43+
if(!Enum.IsDefined(typeof(WebTokenRequestStatus), e))
44+
{
45+
return $"WUTokenHelper returned bogus HRESULT: {e} (THIS IS A BUG)";
46+
}
47+
var status = (WebTokenRequestStatus) Enum.ToObject(typeof(WebTokenRequestStatus), actualCode);
48+
switch (status)
49+
{
50+
case WebTokenRequestStatus.Success:
51+
return "Success (THIS IS A BUG)";
52+
case WebTokenRequestStatus.UserCancel:
53+
return "User cancelled token request (THIS IS A BUG)"; //TODO: should never happen?
54+
case WebTokenRequestStatus.AccountSwitch:
55+
return "User requested account switch (THIS IS A BUG)"; //TODO: should never happen?
56+
case WebTokenRequestStatus.UserInteractionRequired:
57+
return "User interaction required to complete token request (THIS IS A BUG)";
58+
case WebTokenRequestStatus.AccountProviderNotAvailable:
59+
return "Xbox Live account services are currently unavailable";
60+
case WebTokenRequestStatus.ProviderError:
61+
return "Unknown Xbox Live error";
62+
}
63+
}
3464
switch (e) {
35-
case WU_NO_ACCOUNT: return "No account";
65+
case WU_NO_ACCOUNT: return "No Microsoft account found";
3666
default: return "Unknown " + e;
3767
}
3868
}

WUTokenHelper/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using namespace Windows::Internal::Security::Authentication::Web;
99
using namespace Windows::Security::Cryptography;
1010

1111
#define WU_NO_ACCOUNT MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x200)
12+
#define WU_TOKEN_FETCH_ERROR_BASE MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x400)
1213

1314
extern "C" __declspec(dllexport) int __stdcall GetWUToken(wchar_t** retToken) {
1415
auto tokenBrokerStatics = get_activation_factory<TokenBrokerInternal, Windows::Foundation::IUnknown>();
@@ -24,6 +25,9 @@ extern "C" __declspec(dllexport) int __stdcall GetWUToken(wchar_t** retToken) {
2425
auto accountProvider = WebAuthenticationCoreManager::FindAccountProviderAsync(L"https://login.microsoft.com", L"consumers").get();
2526
WebTokenRequest request(accountProvider, L"service::dcat.update.microsoft.com::MBI_SSL", L"{28520974-CE92-4F36-A219-3F255AF7E61E}");
2627
auto result = WebAuthenticationCoreManager::GetTokenSilentlyAsync(request, accountInfo).get();
28+
if (result.ResponseStatus() != WebTokenRequestStatus::Success) {
29+
return WU_TOKEN_FETCH_ERROR_BASE | static_cast<int32_t>(result.ResponseStatus());
30+
}
2731
auto token = result.ResponseData().GetAt(0).Token();
2832
wprintf(L"Token = %s\n", token.c_str());
2933
auto tokenBinary = CryptographicBuffer::ConvertStringToBinary(token, BinaryStringEncoding::Utf16LE);

0 commit comments

Comments
 (0)