Files
VDownload/VDownload.Sources/VDownload.Sources.Twitch/VDownload.Sources.Twitch.Authentication/TwitchAuthenticationService.cs

114 lines
3.3 KiB
C#
Raw Normal View History

2024-02-13 02:59:40 +01:00
using Newtonsoft.Json;
using VDownload.Services.Data.Authentication;
using VDownload.Services.Data.Configuration;
using VDownload.Services.Utility.Encryption;
using VDownload.Sources.Twitch.Api;
2024-02-13 02:59:40 +01:00
using VDownload.Sources.Twitch.Authentication.Models;
namespace VDownload.Sources.Twitch.Authentication
{
public interface ITwitchAuthenticationService
{
Task DeleteToken();
2024-02-13 02:59:40 +01:00
Task<byte[]?> GetToken();
Task SetToken(byte[] token);
Task<TwitchValidationResult> ValidateToken(byte[] token);
}
public class TwitchAuthenticationService : ITwitchAuthenticationService
{
#region SERVICES
protected readonly IConfigurationService _configurationService;
protected readonly ITwitchApiService _apiService;
protected readonly IAuthenticationDataService _authenticationDataService;
protected readonly IEncryptionService _encryptionService;
2024-02-13 02:59:40 +01:00
#endregion
#region CONSTRUCTORS
public TwitchAuthenticationService(IConfigurationService configurationService, ITwitchApiService apiService, IAuthenticationDataService authenticationDataService, IEncryptionService encryptionService)
2024-02-13 02:59:40 +01:00
{
_configurationService = configurationService;
_apiService = apiService;
_authenticationDataService = authenticationDataService;
2024-02-13 02:59:40 +01:00
_encryptionService = encryptionService;
}
#endregion
#region PUBLIC METHODS
public async Task<byte[]?> GetToken()
{
await _authenticationDataService.Load();
2024-02-13 02:59:40 +01:00
byte[]? tokenEncrypted = _authenticationDataService.Data.Twitch.Token;
2024-02-13 02:59:40 +01:00
if (tokenEncrypted is not null && tokenEncrypted.Length == 0)
2024-02-13 02:59:40 +01:00
{
tokenEncrypted = null;
}
if (tokenEncrypted is not null)
{
tokenEncrypted = _encryptionService.Decrypt(tokenEncrypted);
}
return tokenEncrypted;
}
public async Task SetToken(byte[] token)
{
Task loadTask = _authenticationDataService.Load();
2024-02-13 02:59:40 +01:00
byte[] tokenEncrypted = _encryptionService.Encrypt(token);
await loadTask;
_authenticationDataService.Data.Twitch.Token = tokenEncrypted;
2024-02-13 02:59:40 +01:00
await _authenticationDataService.Save();
2024-02-13 02:59:40 +01:00
}
public async Task DeleteToken()
{
await _authenticationDataService.Load();
_authenticationDataService.Data.Twitch.Token = null;
await _authenticationDataService.Save();
2024-02-13 02:59:40 +01:00
}
public async Task<TwitchValidationResult> ValidateToken(byte[] token)
{
string response = await _apiService.AuthValidate(token);
2024-02-13 02:59:40 +01:00
try
{
ValidateResponseSuccess success = JsonConvert.DeserializeObject<ValidateResponseSuccess>(response);
return new TwitchValidationResult(success);
}
catch (JsonSerializationException)
{ }
2024-02-13 02:59:40 +01:00
try
{
ValidateResponseFail fail = JsonConvert.DeserializeObject<ValidateResponseFail>(response);
return new TwitchValidationResult(fail);
}
catch (JsonSerializationException)
{ }
2024-02-13 02:59:40 +01:00
throw new Exception(response);
}
#endregion
}
}