2024-02-13 02:59:40 +01:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2024-03-10 00:35:11 +01:00
|
|
|
|
using CommunityToolkit.WinUI.Helpers;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2024-03-10 00:35:11 +01:00
|
|
|
|
using System.Net.Http;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
using VDownload.Services.Data.Configuration;
|
|
|
|
|
|
using VDownload.Services.UI.Dialogs;
|
|
|
|
|
|
using VDownload.Services.UI.StringResources;
|
|
|
|
|
|
using VDownload.Services.UI.WebView;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
using VDownload.Sources.Twitch.Authentication;
|
|
|
|
|
|
|
2024-02-14 02:07:22 +01:00
|
|
|
|
namespace VDownload.Core.ViewModels.Authentication
|
2024-02-13 02:59:40 +01:00
|
|
|
|
{
|
|
|
|
|
|
public partial class AuthenticationViewModel : ObservableObject
|
|
|
|
|
|
{
|
|
|
|
|
|
#region ENUMS
|
|
|
|
|
|
|
|
|
|
|
|
public enum AuthenticationButton
|
|
|
|
|
|
{
|
|
|
|
|
|
SignIn,
|
|
|
|
|
|
SignOut,
|
|
|
|
|
|
Loading
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region SERVICES
|
|
|
|
|
|
|
2024-02-14 02:07:22 +01:00
|
|
|
|
protected readonly IDialogsService _dialogsService;
|
|
|
|
|
|
protected readonly IWebViewService _webViewService;
|
|
|
|
|
|
protected readonly IConfigurationService _configurationService;
|
|
|
|
|
|
protected readonly IStringResourcesService _stringResourcesService;
|
|
|
|
|
|
protected readonly ITwitchAuthenticationService _twitchAuthenticationService;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PROPERTIES
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2024-03-10 00:35:11 +01:00
|
|
|
|
protected AuthenticationButton _twitchButtonState = AuthenticationButton.Loading;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2024-03-10 00:35:11 +01:00
|
|
|
|
protected bool _twitchButtonEnable = true;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
protected string _twitchDescription;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region CONSTRUCTORS
|
|
|
|
|
|
|
2024-02-14 02:07:22 +01:00
|
|
|
|
public AuthenticationViewModel(IDialogsService dialogsService, IWebViewService webViewService, IConfigurationService configurationService, IStringResourcesService stringResourcesService, ITwitchAuthenticationService twitchAuthenticationService)
|
2024-02-13 02:59:40 +01:00
|
|
|
|
{
|
2024-02-14 02:07:22 +01:00
|
|
|
|
_dialogsService = dialogsService;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
_webViewService = webViewService;
|
2024-02-14 02:07:22 +01:00
|
|
|
|
_configurationService = configurationService;
|
|
|
|
|
|
_stringResourcesService = stringResourcesService;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
_twitchAuthenticationService = twitchAuthenticationService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PUBLIC METHODS
|
|
|
|
|
|
|
2024-02-14 02:07:22 +01:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async Task Navigation()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Task> refreshTasks = new List<Task>
|
|
|
|
|
|
{
|
|
|
|
|
|
TwitchAuthenticationRefresh()
|
|
|
|
|
|
};
|
|
|
|
|
|
await Task.WhenAll(refreshTasks);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-13 02:59:40 +01:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async Task TwitchAuthentication()
|
|
|
|
|
|
{
|
|
|
|
|
|
AuthenticationButton state = TwitchButtonState;
|
|
|
|
|
|
TwitchButtonState = AuthenticationButton.Loading;
|
|
|
|
|
|
|
|
|
|
|
|
if (state == AuthenticationButton.SignOut)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _twitchAuthenticationService.DeleteToken();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-02-14 02:07:22 +01:00
|
|
|
|
Sources.Twitch.Configuration.Models.Authentication auth = _configurationService.Twitch.Authentication;
|
|
|
|
|
|
string authUrl = string.Format(auth.Url, auth.ClientId, auth.RedirectUrl, auth.ResponseType, string.Join(' ', auth.Scopes));
|
|
|
|
|
|
|
|
|
|
|
|
string url = await _webViewService.Show(new Uri(authUrl), (url) => url.StartsWith(auth.RedirectUrl), _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationWindowTitle"));
|
2024-02-13 02:59:40 +01:00
|
|
|
|
|
2024-02-14 02:07:22 +01:00
|
|
|
|
Regex regex = new Regex(auth.RedirectUrlRegex);
|
|
|
|
|
|
Match match = regex.Match(url);
|
2024-02-13 02:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
if (match.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
string token = match.Groups[1].Value;
|
|
|
|
|
|
await _twitchAuthenticationService.SetToken(Encoding.UTF8.GetBytes(token));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-03-10 00:35:11 +01:00
|
|
|
|
string title = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDialogTitle");
|
|
|
|
|
|
string message = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDialogMessage");
|
|
|
|
|
|
await _dialogsService.ShowOk(title, message);
|
2024-02-13 02:59:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
await TwitchAuthenticationRefresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PRIVATE METHODS
|
|
|
|
|
|
|
|
|
|
|
|
private async Task TwitchAuthenticationRefresh()
|
|
|
|
|
|
{
|
|
|
|
|
|
TwitchButtonState = AuthenticationButton.Loading;
|
2024-03-10 00:35:11 +01:00
|
|
|
|
TwitchButtonEnable = true;
|
2024-02-13 02:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
byte[]? token = await _twitchAuthenticationService.GetToken();
|
|
|
|
|
|
|
|
|
|
|
|
if (token is null)
|
|
|
|
|
|
{
|
2024-03-10 00:35:11 +01:00
|
|
|
|
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
|
|
|
|
|
|
{
|
|
|
|
|
|
TwitchButtonEnable = false;
|
|
|
|
|
|
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionNotAuthenticatedNoInternetConnection");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionNotAuthenticated");
|
|
|
|
|
|
}
|
2024-02-13 02:59:40 +01:00
|
|
|
|
TwitchButtonState = AuthenticationButton.SignIn;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-03-10 00:35:11 +01:00
|
|
|
|
TwitchValidationResult validationResult;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
validationResult = await _twitchAuthenticationService.ValidateToken(token);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (ex is TaskCanceledException || ex is HttpRequestException)
|
|
|
|
|
|
{
|
|
|
|
|
|
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionCannotValidate");
|
|
|
|
|
|
TwitchButtonState = AuthenticationButton.SignIn;
|
|
|
|
|
|
TwitchButtonEnable = false;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-13 02:59:40 +01:00
|
|
|
|
if (validationResult.Success)
|
|
|
|
|
|
{
|
2024-02-14 02:07:22 +01:00
|
|
|
|
TwitchDescription = string.Format(_stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionAuthenticated"), validationResult.TokenData.Login, validationResult.TokenData.ExpirationDate);
|
2024-02-13 02:59:40 +01:00
|
|
|
|
TwitchButtonState = AuthenticationButton.SignOut;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await _twitchAuthenticationService.DeleteToken();
|
2024-02-14 02:07:22 +01:00
|
|
|
|
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionAuthenticationInvalid");
|
2024-02-13 02:59:40 +01:00
|
|
|
|
TwitchButtonState = AuthenticationButton.SignIn;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|