chore: init commit from GitHub
Some checks are pending
Deploy / update (push) Waiting to run
Build Project .NET / build (push) Waiting to run

This commit is contained in:
Lisoveliy
2025-05-12 19:44:33 +03:00
parent 11a3f40596
commit aebe654c38
107 changed files with 18953 additions and 403 deletions

View File

@@ -0,0 +1,33 @@
using SWAD.API.Consts.Enums;
using SWAD.API.Models.Config.ApiServices.Structure;
namespace SWAD.API.Services.MusicAPI.Auth;
public abstract class AbstractAuthService
{
public MusicService ServiceType { get; protected init; }
// ReSharper disable once NullableWarningSuppressionIsUsed
protected ApiServiceData Data { get; init; } = null!;
public abstract Task<IAuthResponse?> GetToken();
/// <summary>
/// Get all implemented Auth Services
/// </summary>
/// <returns>API Services types</returns>
public static IEnumerable<Type> GetAllImplementations()
{
var type = typeof(AbstractAuthService);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract);
return types;
}
}
public interface IAuthResponse
{
public string Token { get; set; }
public int? ExpireTime { get; set; }
public DateTime? ExpireAt { get; }
}

View File

@@ -0,0 +1,44 @@
using System.Text.Json;
using Microsoft.Extensions.Options;
using SWAD.API.Consts.Enums;
using SWAD.API.Models.Config.ApiServices;
using SWAD.API.Models.JsonStructures.MusicAPI.Spotify;
namespace SWAD.API.Services.MusicAPI.Auth;
/// <inheritdoc />
public class SpotifyAuthService : AbstractAuthService
{
public SpotifyAuthService(IOptions<ApiServicesConfig> data)
{
ServiceType = MusicService.Spotify;
Data = data.Value.ServicesData.First(x => x.Name == ServiceType.ToString());
}
public override async Task<IAuthResponse?> GetToken()
{
using var httpClient = new HttpClient();
var content = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "client_id", Data.ClientId },
{ "client_secret", Data.Secret }
};
try
{
var response = await httpClient.PostAsync(Data.Endpoints.Token, new FormUrlEncodedContent(content));
if (response.IsSuccessStatusCode)
{
var ans = await JsonSerializer.DeserializeAsync<SpotifyAuthResponse>(
await response.Content.ReadAsStreamAsync());
return ans;
}
}
catch (Exception)
{
return null;
}
return null;
}
}

View File

@@ -0,0 +1,46 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Options;
using SWAD.API.Consts.Enums;
using SWAD.API.Models.Config.ApiServices;
using SWAD.API.Models.JsonStructures.MusicAPI.Tidal;
namespace SWAD.API.Services.MusicAPI.Auth;
/// <inheritdoc />
public class TidalAuthService : AbstractAuthService
{
public TidalAuthService(IOptions<ApiServicesConfig> data)
{
ServiceType = MusicService.Tidal;
Data = data.Value.ServicesData.First(x => x.Name == ServiceType.ToString());
}
public override async Task<IAuthResponse?> GetToken()
{
using var httpClient = new HttpClient();
var base64Token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Data.ClientId}:{Data.Secret}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
var content = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" }
};
try
{
var response = await httpClient.PostAsync(Data.Endpoints.Token, new FormUrlEncodedContent(content));
if (response.IsSuccessStatusCode)
{
var ans = await JsonSerializer.DeserializeAsync<TidalAuthResponse>(
await response.Content.ReadAsStreamAsync());
return ans;
}
}
catch (Exception)
{
return null;
}
return null;
}
}