mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 14:46:31 +00:00
rework to have only 1 exe!
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Security;
|
||||
using System.Text;
|
||||
using Azaion.Common.DTO;
|
||||
using Newtonsoft.Json;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
|
||||
namespace Azaion.Common.Services;
|
||||
|
||||
@@ -15,7 +16,9 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
|
||||
private string Email { get; set; } = null!;
|
||||
private SecureString Password { get; set; } = new();
|
||||
|
||||
private string JwtToken { get; set; } = null!;
|
||||
public User User { get; set; } = null!;
|
||||
|
||||
public void EnterCredentials(ApiCredentials credentials)
|
||||
{
|
||||
@@ -35,7 +38,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
|
||||
private async Task<string> Authorize()
|
||||
private async Task Authorize()
|
||||
{
|
||||
if (string.IsNullOrEmpty(Email) || Password.Length == 0)
|
||||
throw new Exception("Email or password is empty! Please do EnterCredentials first!");
|
||||
@@ -59,19 +62,23 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
if (string.IsNullOrEmpty(result?.Token))
|
||||
throw new Exception("JWT Token not found in response");
|
||||
|
||||
return result.Token;
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var token = handler.ReadJwtToken(result.Token);
|
||||
|
||||
User = new User(token.Claims);
|
||||
JwtToken = result.Token;
|
||||
}
|
||||
|
||||
private async Task<HttpResponseMessage> Send(HttpClient client, HttpRequestMessage request)
|
||||
{
|
||||
if (string.IsNullOrEmpty(JwtToken))
|
||||
JwtToken = await Authorize();
|
||||
await Authorize();
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JwtToken);
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
JwtToken = await Authorize();
|
||||
await Authorize();
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JwtToken);
|
||||
response = await client.SendAsync(request);
|
||||
}
|
||||
@@ -88,4 +95,4 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
httpClient.Dispose();
|
||||
Password.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace Azaion.Common.Services;
|
||||
|
||||
public interface IHardwareService
|
||||
{
|
||||
Task<HardwareInfo> GetHardware();
|
||||
HardwareInfo GetHardware();
|
||||
}
|
||||
|
||||
public class HardwareService : IHardwareService
|
||||
@@ -23,11 +23,11 @@ public class HardwareService : IHardwareService
|
||||
"lscpu | grep 'Model name:' | cut -d':' -f2 && " +
|
||||
"lspci | grep VGA | cut -d':' -f3\"";
|
||||
|
||||
public async Task<HardwareInfo> GetHardware()
|
||||
public HardwareInfo GetHardware()
|
||||
{
|
||||
try
|
||||
{
|
||||
var output = await RunCommand(Environment.OSVersion.Platform == PlatformID.Win32NT
|
||||
var output = RunCommand(Environment.OSVersion.Platform == PlatformID.Win32NT
|
||||
? WIN32_GET_HARDWARE_COMMAND
|
||||
: UNIX_GET_HARDWARE_COMMAND);
|
||||
|
||||
@@ -77,7 +77,7 @@ public class HardwareService : IHardwareService
|
||||
return macAddress ?? string.Empty;
|
||||
}
|
||||
|
||||
private async Task<string> RunCommand(string command)
|
||||
private string RunCommand(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -91,8 +91,8 @@ public class HardwareService : IHardwareService
|
||||
process.StartInfo.CreateNoWindow = true;
|
||||
|
||||
process.Start();
|
||||
var result = await process.StandardOutput.ReadToEndAsync();
|
||||
await process.WaitForExitAsync();
|
||||
var result = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
return result.Trim();
|
||||
}
|
||||
|
||||
@@ -46,8 +46,9 @@ public class ResourceLoader(AzaionApiClient api, ApiCredentials credentials) : I
|
||||
public async Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var hardwareService = new HardwareService();
|
||||
var hardwareInfo = await hardwareService.GetHardware();
|
||||
var encryptedStream = await api.GetResource(fileName, credentials.Password, hardwareInfo);
|
||||
var hardwareInfo = hardwareService.GetHardware();
|
||||
|
||||
var encryptedStream = Task.Run(() => api.GetResource(fileName, credentials.Password, hardwareInfo), cancellationToken).Result;
|
||||
|
||||
var key = Security.MakeEncryptionKey(credentials.Email, credentials.Password, hardwareInfo.Hash);
|
||||
var stream = new MemoryStream();
|
||||
|
||||
Reference in New Issue
Block a user