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
+10
View File
@@ -0,0 +1,10 @@
namespace Azaion.LoaderUI;
public class ApiCredentials(string email, string pw)
{
public string Email { get; set; } = email;
public string Password { get; set; } = pw;
public bool IsValid() =>
!string.IsNullOrWhiteSpace(Email) && !string.IsNullOrWhiteSpace(Password);
}
+9 -11
View File
@@ -1,18 +1,14 @@
using System.Windows;
using Azaion.CommonSecurity;
using Azaion.CommonSecurity.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Serilog;
namespace Azaion.LoaderUI;
public partial class App
{
private LoaderClient _loaderClient = null!;
private readonly CancellationTokenSource _loaderUITokenSource = new();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
@@ -30,19 +26,21 @@ public partial class App
rollingInterval: RollingInterval.Day)
.CreateLogger();
var initConfig = SecurityConstants.ReadInitConfig();
_loaderClient = new LoaderClient(initConfig.LoaderClientConfig, Log.Logger, _loaderUITokenSource.Token);
_loaderClient.StartClient();
_loaderClient.Connect();
var host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((_, config) => config
.AddCommandLine(Environment.GetCommandLineArgs())
.AddJsonFile(SecurityConstants.CONFIG_PATH, optional: true, reloadOnChange: true))
.AddJsonFile(Constants.CONFIG_JSON_FILE))
.UseSerilog()
.ConfigureServices((context, services) =>
{
services.AddSingleton<Login>();
services.Configure<DirectoriesConfig>(context.Configuration.GetSection(nameof(DirectoriesConfig)));
services.AddHttpClient<IAzaionApi, AzaionApi>((sp, client) =>
{
client.BaseAddress = new Uri(Constants.API_URL);
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("User-Agent", "Azaion.LoaderUI");
});
})
.Build();
host.Start();
+8 -4
View File
@@ -8,14 +8,11 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Azaion.CommonSecurity\Azaion.CommonSecurity.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
@@ -23,4 +20,11 @@
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<None Remove="loaderconfig.json" />
<Content Include="loaderconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
+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;
}
}
}
+7
View File
@@ -0,0 +1,7 @@
namespace Azaion.LoaderUI;
public class BusinessExceptionDto
{
public int ErrorCode { get; set; }
public string Message { get; set; } = string.Empty;
}
+8
View File
@@ -0,0 +1,8 @@
namespace Azaion.LoaderUI;
public static class Constants
{
public const string CONFIG_JSON_FILE = "loaderconfig.json";
public const string API_URL = "http://localhost:5219"; //"https://api.azaion.com";
public const string AZAION_SUITE_EXE = "Azaion.Suite.exe";
}
+6
View File
@@ -0,0 +1,6 @@
namespace Azaion.LoaderUI;
public class DirectoriesConfig
{
public string SuiteInstallerDirectory {get;set;} = null!;
}
+54 -4
View File
@@ -1,29 +1,79 @@
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Azaion.CommonSecurity.DTO;
using Microsoft.Extensions.Options;
namespace Azaion.LoaderUI;
public partial class Login
{
public Login()
private readonly IAzaionApi _azaionApi;
private readonly DirectoriesConfig _dirConfig;
public Login(IAzaionApi azaionApi, IOptions<DirectoriesConfig> directoriesConfig)
{
_azaionApi = azaionApi;
_dirConfig = directoriesConfig.Value;
InitializeComponent();
}
private void LoginClick(object sender, RoutedEventArgs e)
{
var creds = new ApiCredentials(TbEmail.Text, TbPassword.Password);
if (!creds.IsValid())
return;
LoginBtn.Cursor = Cursors.Wait;
Cursor = Cursors.Wait;
Process.Start("Azaion.Suite.exe", $"-e {TbEmail.Text} -p {TbPassword.Password}");
_azaionApi.Login(creds);
if (GetInstallerVer() > GetLocalVer())
DownloadAndRunInstaller();
Process.Start(Constants.AZAION_SUITE_EXE, $"-e {creds.Email} -p {creds.Password}");
Close();
}
private void DownloadAndRunInstaller()
{
var (installerName, stream) = _azaionApi.DownloadInstaller(_dirConfig.SuiteInstallerDirectory);
var localFileStream = new FileStream(installerName, FileMode.Create, FileAccess.Write);
stream.CopyTo(localFileStream);
localFileStream.Close();
stream.Close();
var processInfo = new ProcessStartInfo(installerName)
{
UseShellExecute = true,
Verb = "runas"
};
Process.Start(processInfo);
}
private Version GetInstallerVer()
{
var installerName = _azaionApi.GetLastInstallerName(_dirConfig.SuiteInstallerDirectory);
var version = installerName
.Replace("AzaionSuite.Iterative.", "")
.Replace(".exe", "");
return new Version(version);
}
private Version GetLocalVer()
{
var localFileInfo = FileVersionInfo.GetVersionInfo(Constants.AZAION_SUITE_EXE);
if (string.IsNullOrWhiteSpace(localFileInfo.ProductVersion))
throw new Exception($"Can't find {Constants.AZAION_SUITE_EXE} and its version");
return new Version(localFileInfo.FileVersion!);
}
private void CloseClick(object sender, RoutedEventArgs e) => Close();
private void MainMouseMove(object sende0r, MouseEventArgs e)
private void MainMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is Button || e.OriginalSource is TextBox)
return;
+6
View File
@@ -0,0 +1,6 @@
namespace Azaion.LoaderUI;
public class LoginResponse
{
public string Token { get; set; } = null!;
}
+6
View File
@@ -0,0 +1,6 @@
{
"DirectoriesConfig":
{
"SuiteInstallerDirectory": ""
}
}