consolidate CommonSecurity to Common.dll

This commit is contained in:
Alex Bezdieniezhnykh
2025-06-13 23:06:48 +03:00
parent 904bc688ca
commit 8aa2f563a4
58 changed files with 362 additions and 151 deletions
+118
View File
@@ -0,0 +1,118 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
namespace Azaion.LoaderUI;
public interface IAzaionApi
{
void Login(ApiCredentials credentials);
string GetLastInstallerName(string folder);
(string name, Stream stream) DownloadInstaller(string folder);
}
public class AzaionApi(HttpClient client) : IAzaionApi
{
private string _jwtToken = null!;
const string APP_JSON = "application/json";
private ApiCredentials _credentials = null!;
public void Login(ApiCredentials credentials)
{
_credentials = credentials;
}
public string GetLastInstallerName(string folder)
{
var res = Get<List<string>>($"/resources/list/{folder}");
return res?.FirstOrDefault() ?? "";
}
public (string name, Stream stream) DownloadInstaller(string folder)
{
var response = Send(new HttpRequestMessage(HttpMethod.Get, $"resources/get-installer/{folder}"));
var fileStream = response.Content.ReadAsStream();
var fileName = response.Content.Headers.ContentDisposition?.FileName?.Trim('"') ?? "installer.exe";
return (fileName, fileStream);
}
private HttpResponseMessage Send(HttpRequestMessage request)
{
if (string.IsNullOrEmpty(_jwtToken))
Authorize();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _jwtToken);
var response = client.Send(request);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Authorize();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _jwtToken);
response = client.Send(request);
}
if (response.IsSuccessStatusCode)
return response;
var stream = response.Content.ReadAsStream();
var content = new StreamReader(stream).ReadToEnd();
if (response.StatusCode == HttpStatusCode.Conflict)
{
var result = JsonConvert.DeserializeObject<BusinessExceptionDto>(content);
throw new Exception($"Failed: {response.StatusCode}! Error Code: {result?.ErrorCode}. Message: {result?.Message}");
}
throw new Exception($"Failed: {response.StatusCode}! Result: {content}");
}
private T? Get<T>(string url)
{
var response = Send(new HttpRequestMessage(HttpMethod.Get, url));
var stream = response.Content.ReadAsStream();
var json = new StreamReader(stream).ReadToEnd();
return JsonConvert.DeserializeObject<T>(json);
}
private void Put<T>(string url, T obj)
{
Send(new HttpRequestMessage(HttpMethod.Put, url)
{
Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, APP_JSON)
});
}
private void Authorize()
{
try
{
if (string.IsNullOrEmpty(_credentials.Email) || _credentials.Password.Length == 0)
throw new Exception("Email or password is empty! Please do EnterCredentials first!");
var content = new StringContent(JsonConvert.SerializeObject(new
{
email = _credentials.Email,
password = _credentials.Password
}), Encoding.UTF8, APP_JSON);
var message = new HttpRequestMessage(HttpMethod.Post, "login") { Content = content };
var response = client.Send(message);
if (!response.IsSuccessStatusCode)
throw new Exception($"EnterCredentials failed: {response.StatusCode}");
var stream = response.Content.ReadAsStream();
var json = new StreamReader(stream).ReadToEnd();
var result = JsonConvert.DeserializeObject<LoginResponse>(json);
if (string.IsNullOrEmpty(result?.Token))
throw new Exception("JWT Token not found in response");
_jwtToken = result.Token;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}