mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 15:16:31 +00:00
fix keyboard catch, fix loading
refactoring
This commit is contained in:
@@ -17,13 +17,13 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
private SecureString Password { get; set; } = new();
|
||||
private string JwtToken { get; set; } = null!;
|
||||
|
||||
public void Login(string email, string password)
|
||||
public void EnterCredentials(ApiCredentials credentials)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
|
||||
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
|
||||
throw new Exception("Email or password is empty!");
|
||||
|
||||
Email = email;
|
||||
Password = password.ToSecureString();
|
||||
Email = credentials.Email;
|
||||
Password = credentials.Password.ToSecureString();
|
||||
}
|
||||
|
||||
public async Task<Stream> GetResource(string fileName, string password, HardwareInfo hardware)
|
||||
@@ -38,7 +38,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
private async Task<string> Authorize()
|
||||
{
|
||||
if (string.IsNullOrEmpty(Email) || Password.Length == 0)
|
||||
throw new Exception("Email or password is empty! Please do Login first!");
|
||||
throw new Exception("Email or password is empty! Please do EnterCredentials first!");
|
||||
|
||||
var payload = new
|
||||
{
|
||||
@@ -50,7 +50,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, JSON_MEDIA));
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception($"Login failed: {response.StatusCode}");
|
||||
throw new Exception($"EnterCredentials failed: {response.StatusCode}");
|
||||
|
||||
var responseData = await response.Content.ReadAsStringAsync();
|
||||
|
||||
|
||||
@@ -1,20 +1,45 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Azaion.Common.DTO;
|
||||
|
||||
namespace Azaion.Common.Services;
|
||||
|
||||
public interface IResourceLoader
|
||||
{
|
||||
Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default);
|
||||
Assembly LoadAssembly(string asmName);
|
||||
}
|
||||
|
||||
public class ResourceLoader(string email, string password, AzaionApiClient api, IHardwareService hardwareService) : IResourceLoader
|
||||
public class ResourceLoader(AzaionApiClient api, ApiCredentials credentials) : IResourceLoader
|
||||
{
|
||||
private static readonly List<string> EncryptedResources =
|
||||
[
|
||||
"Azaion.Annotator",
|
||||
"Azaion.Dataset"
|
||||
];
|
||||
|
||||
public Assembly LoadAssembly(string resourceName)
|
||||
{
|
||||
var assemblyName = resourceName.Split(',').First();
|
||||
if (EncryptedResources.Contains(assemblyName))
|
||||
{
|
||||
var stream = Load(assemblyName).GetAwaiter().GetResult();
|
||||
return Assembly.Load(stream.ToArray());
|
||||
}
|
||||
|
||||
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.FirstOrDefault(a => a.GetName().Name == assemblyName);
|
||||
|
||||
return loadedAssembly;
|
||||
}
|
||||
|
||||
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, password, hardwareInfo);
|
||||
var encryptedStream = await api.GetResource(fileName, credentials.Password, hardwareInfo);
|
||||
|
||||
var key = Security.MakeEncryptionKey(email, password, hardwareInfo.Hash);
|
||||
var key = Security.MakeEncryptionKey(credentials.Email, credentials.Password, hardwareInfo.Hash);
|
||||
var stream = new MemoryStream();
|
||||
await encryptedStream.DecryptTo(stream, key, cancellationToken);
|
||||
return stream;
|
||||
|
||||
Reference in New Issue
Block a user