Files
NebulaLauncher/Nebula.Launcher/ViewModels/Pages/AccountInfoViewModel.cs

448 lines
16 KiB
C#
Raw Normal View History

2025-01-27 15:55:30 +03:00
using System;
2025-05-03 18:20:24 +03:00
using System.Collections.Generic;
2024-12-22 21:38:19 +03:00
using System.Collections.ObjectModel;
2024-12-27 08:22:17 +03:00
using System.Linq;
2025-06-20 16:27:42 +03:00
using System.Net.Http;
2025-01-27 15:55:30 +03:00
using System.Threading.Tasks;
2024-12-22 21:38:19 +03:00
using CommunityToolkit.Mvvm.ComponentModel;
2025-08-07 22:34:25 +03:00
using Nebula.Launcher.Configurations;
2025-07-02 21:32:51 +03:00
using Nebula.Launcher.Models.Auth;
2025-01-14 22:10:16 +03:00
using Nebula.Launcher.Services;
using Nebula.Launcher.ViewModels.Popup;
2024-12-21 13:11:30 +03:00
using Nebula.Launcher.Views.Pages;
2025-08-06 21:29:00 +03:00
using Nebula.Shared.Configurations;
2025-07-10 15:22:15 +03:00
using Nebula.Shared.Models.Auth;
2025-01-05 17:05:23 +03:00
using Nebula.Shared.Services;
2025-05-05 22:01:59 +03:00
using Nebula.Shared.Services.Logging;
2025-01-05 17:05:23 +03:00
using Nebula.Shared.Utils;
2025-07-02 21:32:51 +03:00
using Nebula.Shared.ViewHelper;
2024-12-21 13:11:30 +03:00
2025-01-14 22:10:16 +03:00
namespace Nebula.Launcher.ViewModels.Pages;
2024-12-21 13:11:30 +03:00
2025-01-05 17:05:23 +03:00
[ViewModelRegister(typeof(AccountInfoView))]
2025-01-14 22:10:16 +03:00
[ConstructGenerator]
public partial class AccountInfoViewModel : ViewModelBase
2024-12-21 13:11:30 +03:00
{
2025-01-14 22:10:16 +03:00
[ObservableProperty] private bool _authMenuExpand;
2024-12-27 19:15:33 +03:00
[ObservableProperty] private bool _authUrlConfigExpand;
[ObservableProperty] private int _authViewSpan = 1;
2025-01-14 22:10:16 +03:00
[ObservableProperty] private string _currentAuthServer = string.Empty;
[ObservableProperty] private string _currentLogin = string.Empty;
[ObservableProperty] private string _currentPassword = string.Empty;
2024-12-27 19:15:33 +03:00
[ObservableProperty] private bool _isLogged;
2025-06-20 16:27:42 +03:00
[ObservableProperty] private bool _doRetryAuth;
2025-07-15 18:38:53 +03:00
[ObservableProperty] private AuthServerCredentials _authItemSelect;
[ObservableProperty] private string _authServerName;
2024-12-22 21:38:19 +03:00
2025-01-14 22:10:16 +03:00
private bool _isProfilesEmpty;
2025-11-08 13:42:11 +03:00
[GenerateProperty] private PopupMessageService PopupMessageService { get; }
[GenerateProperty] private ConfigurationService ConfigurationService { get; }
2025-05-05 22:01:59 +03:00
[GenerateProperty] private DebugService DebugService { get; }
2025-11-08 13:42:11 +03:00
[GenerateProperty] private AuthService AuthService { get; }
[GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; }
2025-01-14 22:10:16 +03:00
2025-11-08 13:42:11 +03:00
public ObservableCollection<ProfileEntry> Accounts { get; } = new();
2025-05-03 18:20:24 +03:00
public ObservableCollection<AuthServerCredentials> AuthUrls { get; } = new();
2025-08-06 21:29:00 +03:00
2025-08-07 22:34:25 +03:00
public ComplexConVarBinder<AuthTokenCredentials?> Credentials { get; private set; }
2025-01-14 22:10:16 +03:00
2025-05-05 22:01:59 +03:00
private ILogger _logger;
2025-07-15 18:38:53 +03:00
2024-12-22 21:38:19 +03:00
//Design think
2025-01-14 22:10:16 +03:00
protected override void InitialiseInDesignMode()
2024-12-22 21:38:19 +03:00
{
2025-11-08 13:42:11 +03:00
AuthUrls.Add(new AuthServerCredentials("Test",["example.com","variant.lab"]));
2025-07-15 18:38:53 +03:00
2025-11-08 13:42:11 +03:00
AddAccount(new ProfileAuthCredentials("Binka", "","example.com"));
AddAccount(new ProfileAuthCredentials("Vilka","", "variant.lab"));
2024-12-22 21:38:19 +03:00
}
2025-08-06 21:29:00 +03:00
2024-12-22 21:38:19 +03:00
//Real think
2025-01-14 22:10:16 +03:00
protected override void Initialise()
2024-12-22 21:38:19 +03:00
{
2025-05-05 22:01:59 +03:00
_logger = DebugService.GetLogger(this);
2025-08-06 21:29:00 +03:00
Credentials = new AuthTokenCredentialsVar(this);
2025-06-20 16:27:42 +03:00
Task.Run(ReadAuthConfig);
2025-08-17 21:02:45 +03:00
Credentials.Value = Credentials.Value;
2024-12-22 21:38:19 +03:00
}
2025-08-06 21:29:00 +03:00
2025-02-01 18:19:18 +03:00
public void DoAuth(string? code = null)
2024-12-22 21:38:19 +03:00
{
2025-01-14 22:10:16 +03:00
var message = ViewHelperService.GetViewModel<InfoPopupViewModel>();
message.InfoText = LocalizationService.GetString("auth-processing");
2025-01-27 15:55:30 +03:00
message.IsInfoClosable = false;
2025-01-14 22:10:16 +03:00
PopupMessageService.Popup(message);
2025-05-03 18:20:24 +03:00
var serverCandidates = new List<string>();
if (string.IsNullOrWhiteSpace(CurrentAuthServer))
serverCandidates.AddRange(AuthItemSelect.Servers);
else
serverCandidates.Add(CurrentAuthServer);
2025-01-27 15:55:30 +03:00
Task.Run(async () =>
2024-12-27 08:22:17 +03:00
{
2025-05-03 18:20:24 +03:00
Exception? exception = null;
2025-08-06 21:29:00 +03:00
2025-05-03 18:20:24 +03:00
foreach (var server in serverCandidates)
2025-02-01 18:19:18 +03:00
{
2025-05-03 18:20:24 +03:00
try
2025-02-01 18:19:18 +03:00
{
2025-08-17 21:02:45 +03:00
await CatchAuthError(async() =>
{
Credentials.Value = await AuthService.Auth(CurrentLogin, CurrentPassword, server, code);
}, ()=> message.Dispose());
2025-05-03 18:20:24 +03:00
break;
}
2025-06-20 16:27:42 +03:00
catch (Exception ex)
2025-05-03 18:20:24 +03:00
{
exception = new Exception(LocalizationService.GetString("auth-error"), ex);
2025-02-01 18:19:18 +03:00
}
}
2025-05-03 18:20:24 +03:00
message.Dispose();
2025-08-17 21:02:45 +03:00
if (exception != null)
2025-01-27 15:55:30 +03:00
{
2025-08-17 21:02:45 +03:00
PopupMessageService.Popup(new Exception("Error while auth", exception));
2025-01-27 15:55:30 +03:00
}
});
2024-12-27 19:15:33 +03:00
}
2025-06-20 16:27:42 +03:00
private async Task CatchAuthError(Func<Task> a, Action? onError)
{
DoRetryAuth = false;
2025-05-03 18:20:24 +03:00
try
{
2025-06-20 16:27:42 +03:00
await a();
2025-05-03 18:20:24 +03:00
}
catch (AuthException e)
{
2025-06-20 16:27:42 +03:00
onError?.Invoke();
2025-05-03 18:20:24 +03:00
switch (e.Error.Code)
{
case AuthenticateDenyCode.TfaRequired:
case AuthenticateDenyCode.TfaInvalid:
var p = ViewHelperService.GetViewModel<TfaViewModel>();
PopupMessageService.Popup(p);
2025-05-05 22:01:59 +03:00
_logger.Log("TFA required");
2025-05-03 18:20:24 +03:00
break;
case AuthenticateDenyCode.InvalidCredentials:
PopupError(LocalizationService.GetString("auth-invalid-credentials"), e);
2025-05-03 18:20:24 +03:00
break;
2025-08-06 21:29:00 +03:00
case AuthenticateDenyCode.AccountLocked:
PopupError(LocalizationService.GetString("auth-account-locked"), e);
2025-08-06 21:29:00 +03:00
break;
case AuthenticateDenyCode.AccountUnconfirmed:
PopupError(LocalizationService.GetString("auth-account-unconfirmed"), e);
2025-08-06 21:29:00 +03:00
break;
case AuthenticateDenyCode.None:
PopupError(LocalizationService.GetString("auth-none"),e);
2025-08-06 21:29:00 +03:00
break;
2025-05-03 18:20:24 +03:00
default:
PopupError(LocalizationService.GetString("auth-error-fuck"), e);
2025-08-06 21:29:00 +03:00
break;
2025-05-03 18:20:24 +03:00
}
}
2025-06-20 16:27:42 +03:00
catch (HttpRequestException e)
{
onError?.Invoke();
switch (e.HttpRequestError)
{
case HttpRequestError.ConnectionError:
PopupError(LocalizationService.GetString("auth-connection-error"), e);
2025-06-20 16:27:42 +03:00
DoRetryAuth = true;
break;
case HttpRequestError.NameResolutionError:
PopupError(LocalizationService.GetString("auth-name-resolution-error"), e);
2025-06-20 16:27:42 +03:00
DoRetryAuth = true;
break;
2025-06-23 19:48:11 +03:00
case HttpRequestError.SecureConnectionError:
PopupError(LocalizationService.GetString("auth-secure-error"), e);
2025-06-23 19:48:11 +03:00
DoRetryAuth = true;
break;
2025-08-06 21:29:00 +03:00
case HttpRequestError.UserAuthenticationError:
PopupError(LocalizationService.GetString("auth-user-authentication-error"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.Unknown:
PopupError(LocalizationService.GetString("auth-unknown"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.HttpProtocolError:
PopupError(LocalizationService.GetString("auth-http-protocol-error"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.ExtendedConnectNotSupported:
PopupError(LocalizationService.GetString("auth-extended-connect-not-support"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.VersionNegotiationError:
PopupError(LocalizationService.GetString("auth-version-negotiation-error"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.ProxyTunnelError:
PopupError(LocalizationService.GetString("auth-proxy-tunnel-error"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.InvalidResponse:
PopupError(LocalizationService.GetString("auth-invalid-response"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.ResponseEnded:
PopupError(LocalizationService.GetString("auth-response-ended"), e);
2025-08-06 21:29:00 +03:00
break;
case HttpRequestError.ConfigurationLimitExceeded:
PopupError(LocalizationService.GetString("auth-configuration-limit-exceeded"), e);
2025-08-06 21:29:00 +03:00
break;
2025-06-20 16:27:42 +03:00
default:
var authError = new Exception(LocalizationService.GetString("auth-error"), e);
2025-06-20 16:27:42 +03:00
_logger.Error(authError);
PopupMessageService.Popup(authError);
break;
}
}
2025-05-03 18:20:24 +03:00
}
2024-12-27 19:15:33 +03:00
public void Logout()
{
2025-08-06 21:29:00 +03:00
Credentials.Value = null;
2025-07-15 18:38:53 +03:00
CurrentAuthServer = "";
}
2025-11-08 13:42:11 +03:00
public string GetServerAuthName(string? url)
2025-07-15 18:38:53 +03:00
{
2025-11-08 13:42:11 +03:00
if (url is null) return "";
return AuthUrls.FirstOrDefault(p => p.Servers.Contains(url))?.Name ?? "CustomAuth";
2024-12-27 19:15:33 +03:00
}
private void UpdateAuthMenu()
{
if (AuthMenuExpand || _isProfilesEmpty)
AuthViewSpan = 2;
else
AuthViewSpan = 1;
2024-12-22 21:38:19 +03:00
}
2025-11-08 13:42:11 +03:00
private void AddAccount(ProfileAuthCredentials credentials)
2024-12-22 21:38:19 +03:00
{
2025-11-08 13:42:11 +03:00
var onDelete = new DelegateCommand<ProfileEntry>(OnDeleteProfile);
var onSelect = new DelegateCommand<ProfileEntry>((p) =>
{
CurrentLogin = p.Credentials.Login;
CurrentPassword = p.Credentials.Password;
CurrentAuthServer = p.Credentials.AuthServer;
DoAuth();
});
2025-07-15 18:38:53 +03:00
2025-11-08 13:42:11 +03:00
var serverName = GetServerAuthName(credentials.AuthServer);
2025-01-14 22:10:16 +03:00
2025-11-08 13:42:11 +03:00
var alpm = new ProfileEntry(
2025-07-10 15:22:15 +03:00
credentials,
2025-07-15 18:38:53 +03:00
serverName,
2025-01-14 22:10:16 +03:00
onSelect,
2024-12-22 21:38:19 +03:00
onDelete);
onDelete.TRef.Value = alpm;
onSelect.TRef.Value = alpm;
2025-01-14 22:10:16 +03:00
2024-12-22 21:38:19 +03:00
Accounts.Add(alpm);
}
2025-08-17 21:02:45 +03:00
private async Task ReadAuthConfig()
2024-12-22 21:38:19 +03:00
{
2025-02-01 18:19:18 +03:00
var message = ViewHelperService.GetViewModel<InfoPopupViewModel>();
message.InfoText = LocalizationService.GetString("auth-config-read");
2025-02-01 18:19:18 +03:00
message.IsInfoClosable = false;
PopupMessageService.Popup(message);
2025-07-15 18:38:53 +03:00
2025-08-06 21:29:00 +03:00
_logger.Log("Reading auth config");
2025-07-15 18:38:53 +03:00
AuthUrls.Clear();
var authUrls = ConfigurationService.GetConfigValue(LauncherConVar.AuthServers)!;
foreach (var url in authUrls) AuthUrls.Add(url);
if(authUrls.Length > 0) AuthItemSelect = authUrls[0];
2025-11-08 13:42:11 +03:00
var profileCandidates = new List<string>();
2025-08-06 21:29:00 +03:00
2025-11-08 13:42:11 +03:00
foreach (var profileRaw in
2025-02-01 18:19:18 +03:00
ConfigurationService.GetConfigValue(LauncherConVar.AuthProfiles)!)
2025-08-06 21:29:00 +03:00
{
2025-11-08 13:42:11 +03:00
_logger.Log($"Decrypting profile...");
try
2025-08-06 21:29:00 +03:00
{
2025-11-08 13:42:11 +03:00
var decoded =
await CryptographicStore.Decrypt<ProfileAuthCredentials>(profileRaw,
CryptographicStore.GetComputerKey());
2025-08-06 21:29:00 +03:00
2025-11-08 13:42:11 +03:00
_logger.Log($"Decrypted profile: {decoded.Login}");
profileCandidates.Add(profileRaw);
AddAccount(decoded);
}
catch (Exception e)
{
_logger.Error("Error while decrypting profile");
_logger.Error(e);
}
2025-08-06 21:29:00 +03:00
}
ConfigurationService.SetConfigValue(LauncherConVar.AuthProfiles, profileCandidates.ToArray());
2024-12-27 08:22:17 +03:00
2025-01-14 22:10:16 +03:00
if (Accounts.Count == 0) UpdateAuthMenu();
2025-07-15 18:38:53 +03:00
2025-06-20 16:27:42 +03:00
message.Dispose();
}
2025-08-06 21:29:00 +03:00
public void DoCurrentAuth()
2025-06-20 16:27:42 +03:00
{
2025-08-06 21:29:00 +03:00
DoAuth();
}
2024-12-27 08:22:17 +03:00
2025-08-06 21:29:00 +03:00
private async Task<AuthTokenCredentials?> CheckOrRenewToken(AuthTokenCredentials? authTokenCredentials)
{
if(authTokenCredentials is null)
return null;
2025-08-17 21:02:45 +03:00
var daysLeft = (int)(authTokenCredentials.Token.ExpireTime - DateTime.Now).TotalDays;
2025-08-06 21:29:00 +03:00
2025-08-17 21:02:45 +03:00
if(daysLeft >= 4)
{
_logger.Log("Token " + authTokenCredentials.Login + " is active, "+daysLeft+" days left, undo renewing!");
2025-08-06 21:29:00 +03:00
return authTokenCredentials;
2025-08-17 21:02:45 +03:00
}
2025-11-08 13:42:11 +03:00
2025-08-06 21:29:00 +03:00
try
2024-12-27 08:22:17 +03:00
{
2025-08-06 21:29:00 +03:00
_logger.Log($"Renewing token for {authTokenCredentials.Login}");
2025-11-08 13:42:11 +03:00
return await ExceptionHelper.TryRun(() => AuthService.Refresh(authTokenCredentials), 3,
(attempt, e) => { _logger.Error(new Exception("Error while renewing, attempts: " + attempt, e)); });
}
catch (AuthTokenExpiredException e)
{
_logger.Error(e);
return null;
2025-08-06 21:29:00 +03:00
}
catch (Exception e)
{
var unexpectedError = new Exception(LocalizationService.GetString("auth-error"), e);
2025-08-06 21:29:00 +03:00
_logger.Error(unexpectedError);
2025-08-07 22:34:25 +03:00
return authTokenCredentials;
2024-12-27 08:22:17 +03:00
}
2024-12-22 21:38:19 +03:00
}
2025-08-06 21:29:00 +03:00
public void OnSaveProfile()
2024-12-22 21:38:19 +03:00
{
2025-11-08 13:42:11 +03:00
if(Credentials.Value is null ||
string.IsNullOrEmpty(CurrentPassword)) return;
2025-07-10 15:22:15 +03:00
2025-11-08 13:42:11 +03:00
AddAccount(new ProfileAuthCredentials(CurrentLogin, CurrentPassword, Credentials.Value.AuthServer));
2024-12-27 19:15:33 +03:00
_isProfilesEmpty = Accounts.Count == 0;
UpdateAuthMenu();
DirtyProfile();
}
2025-01-14 22:10:16 +03:00
2025-11-08 13:42:11 +03:00
private void OnDeleteProfile(ProfileEntry account)
2024-12-27 19:15:33 +03:00
{
Accounts.Remove(account);
_isProfilesEmpty = Accounts.Count == 0;
UpdateAuthMenu();
DirtyProfile();
}
2025-06-20 16:27:42 +03:00
private void PopupError(string message, Exception e)
{
message = LocalizationService.GetString("auth-error-occured") + message;
2025-06-20 16:27:42 +03:00
_logger.Error(new Exception(message, e));
var messageView = ViewHelperService.GetViewModel<InfoPopupViewModel>();
messageView.InfoText = message;
messageView.IsInfoClosable = true;
PopupMessageService.Popup(messageView);
}
2025-08-06 21:29:00 +03:00
public void OnExpandAuthUrl()
2024-12-27 19:15:33 +03:00
{
AuthUrlConfigExpand = !AuthUrlConfigExpand;
}
2025-08-06 21:29:00 +03:00
public void OnExpandAuthView()
2024-12-27 19:15:33 +03:00
{
AuthMenuExpand = !AuthMenuExpand;
UpdateAuthMenu();
}
private void DirtyProfile()
{
2025-02-01 18:19:18 +03:00
ConfigurationService.SetConfigValue(LauncherConVar.AuthProfiles,
2025-11-08 13:42:11 +03:00
Accounts.Select(a => CryptographicStore.Encrypt(a.Credentials, CryptographicStore.GetComputerKey())).ToArray());
2024-12-22 21:38:19 +03:00
}
2025-08-06 21:29:00 +03:00
public sealed class AuthTokenCredentialsVar(AccountInfoViewModel accountInfoViewModel)
: ComplexConVarBinder<AuthTokenCredentials?>(
accountInfoViewModel.ConfigurationService.SubscribeVarChanged(LauncherConVar.AuthCurrent))
2025-07-15 18:38:53 +03:00
{
2025-08-06 21:29:00 +03:00
protected override async Task<AuthTokenCredentials?> OnValueChange(AuthTokenCredentials? currProfile)
{
if (currProfile is null)
{
accountInfoViewModel.IsLogged = false;
accountInfoViewModel._logger.Log("clearing credentials");
return null;
}
2025-07-15 18:38:53 +03:00
2025-08-06 21:29:00 +03:00
var message = accountInfoViewModel.ViewHelperService.GetViewModel<InfoPopupViewModel>();
message.InfoText = LocalizationService.GetString("auth-try-auth-config");
2025-08-06 21:29:00 +03:00
message.IsInfoClosable = false;
accountInfoViewModel.PopupMessageService.Popup(message);
accountInfoViewModel._logger.Log($"trying auth with {currProfile.Login}");
2025-07-15 18:38:53 +03:00
2025-08-06 21:29:00 +03:00
var errorRun = false;
2025-08-17 21:02:45 +03:00
currProfile = await accountInfoViewModel.CheckOrRenewToken(currProfile);
if (currProfile is null)
{
message.Dispose();
accountInfoViewModel._logger.Log("profile credentials update required!");
accountInfoViewModel.PopupMessageService.Popup("profile credentials update required!");
accountInfoViewModel.IsLogged = false;
return null;
}
2025-08-06 21:29:00 +03:00
try
{
await accountInfoViewModel.CatchAuthError(async () =>
{
await accountInfoViewModel.AuthService.EnsureToken(currProfile);
}, () =>
{
message.Dispose();
errorRun = true;
});
message.Dispose();
}
catch (Exception ex)
{
accountInfoViewModel.CurrentLogin = currProfile.Login;
accountInfoViewModel.CurrentAuthServer = currProfile.AuthServer;
var unexpectedError = new Exception(LocalizationService.GetString("auth-error"), ex);
2025-08-06 21:29:00 +03:00
accountInfoViewModel._logger.Error(unexpectedError);
accountInfoViewModel.PopupMessageService.Popup(unexpectedError);
errorRun = true;
}
if (errorRun)
{
accountInfoViewModel.IsLogged = false;
return null;
}
accountInfoViewModel.IsLogged = true;
accountInfoViewModel.AuthServerName = accountInfoViewModel.GetServerAuthName(currProfile.AuthServer);
2025-08-07 22:34:25 +03:00
2025-08-06 21:29:00 +03:00
return currProfile;
}
2025-07-15 18:38:53 +03:00
}
2025-08-06 21:29:00 +03:00
}