using System;
using System.Threading.Tasks;
using CmlLib.Core.Auth;
using CmlLib.Core.Auth.Microsoft;
using XboxAuthNet.Game.Msal;
using XboxAuthNet.Game.Msal.OAuth;
namespace ModpackLauncher.Services;
///
/// Microsoft auth wrapper. Two modes:
/// 1. Custom Azure AD client ID (msalClientId set) -> MSAL flow. Requires Microsoft
/// to have approved the app for Minecraft API access.
/// 2. No custom client ID (default) -> CmlLib's BuildDefault() which uses the
/// WebView2-driven Microsoft Live OAuth flow with the Xbox Live SDK client ID.
/// Doesn't require an Azure registration; works out of the box on any Win10/11
/// machine that has the WebView2 Runtime installed (preinstalled since 2021).
///
public sealed class AuthService
{
private readonly string _clientId;
public AuthService(string clientId)
{
_clientId = clientId;
}
/// True when the user has provided their own Azure App Registration ID.
public bool HasCustomClientId => !string.IsNullOrWhiteSpace(_clientId)
&& _clientId != "00000000-0000-0000-0000-000000000000";
/// Auth is always available now (BuildDefault provides a fallback).
public bool IsConfigured => true;
public async Task AuthenticateAsync()
{
var loginHandler = await BuildLoginHandlerAsync();
try
{
return await loginHandler.AuthenticateSilently();
}
catch
{
return await loginHandler.AuthenticateInteractively();
}
}
public async Task SignInInteractivelyAsync()
{
var loginHandler = await BuildLoginHandlerAsync();
return await loginHandler.AuthenticateInteractively();
}
public async Task TryAuthenticateSilentlyAsync()
{
try
{
var loginHandler = await BuildLoginHandlerAsync();
return await loginHandler.AuthenticateSilently();
}
catch
{
return null;
}
}
public async Task SignOutAsync()
{
try
{
var loginHandler = await BuildLoginHandlerAsync();
await loginHandler.Signout();
}
catch
{
// best-effort
}
}
private async Task BuildLoginHandlerAsync()
{
if (HasCustomClientId)
{
// Custom Azure AD MSAL flow -- requires the app to be approved by Microsoft.
var app = await MsalClientHelper.BuildApplicationWithCache(_clientId);
return new JELoginHandlerBuilder()
.WithOAuthProvider(new MsalCodeFlowProvider(app))
.Build();
}
// Default path: WebView2 + Xbox Live SDK community client ID. No Azure registration.
// Note: requires WebView2 Runtime on Windows (preinstalled on Win10/11 since 2021).
return JELoginHandlerBuilder.BuildDefault();
}
}