mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:16:30 +00:00
move zmq port to config file for C# and python
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
namespace Azaion.CommonSecurity.DTO;
|
||||
|
||||
public class ApiConfig
|
||||
{
|
||||
public string Url { get; set; } = null!;
|
||||
public int RetryCount {get;set;}
|
||||
public double TimeoutSeconds { get; set; }
|
||||
public string ResourcesFolder { get; set; } = "";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Azaion.CommonSecurity.DTO;
|
||||
|
||||
public class PythonConfig
|
||||
{
|
||||
public string ZeroMqHost { get; set; }
|
||||
public int ZeroMqPort { get; set; }
|
||||
public double OneTryTimeoutSeconds { get; set; }
|
||||
public int RetryCount {get;set;}
|
||||
|
||||
public string ResourcesFolder { get; set; } = "";
|
||||
}
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
public class SecureAppConfig
|
||||
{
|
||||
public ApiConfig ApiConfig { get; set; } = null!;
|
||||
public PythonConfig PythonConfig { get; set; } = null!;
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Security.Claims;
|
||||
using MessagePack;
|
||||
|
||||
namespace Azaion.CommonSecurity.DTO;
|
||||
@@ -9,18 +8,4 @@ public class User
|
||||
[Key("i")] public string Id { get; set; } = "";
|
||||
[Key("e")] public string Email { get; set; } = "";
|
||||
[Key("r")]public RoleEnum Role { get; set; }
|
||||
|
||||
//For deserializing
|
||||
public User(){}
|
||||
|
||||
public User(IEnumerable<Claim> claims)
|
||||
{
|
||||
var claimDict = claims.ToDictionary(x => x.Type, x => x.Value);
|
||||
|
||||
Id = claimDict[SecurityConstants.CLAIM_NAME_ID];
|
||||
Email = claimDict[SecurityConstants.CLAIM_EMAIL];
|
||||
if (!Enum.TryParse(claimDict[SecurityConstants.CLAIM_ROLE], out RoleEnum role))
|
||||
role = RoleEnum.None;
|
||||
Role = role;
|
||||
}
|
||||
}
|
||||
@@ -6,23 +6,13 @@ public class SecurityConstants
|
||||
|
||||
public const string DUMMY_DIR = "dummy";
|
||||
|
||||
#region ApiConfig
|
||||
|
||||
public const string DEFAULT_API_URL = "https://api.azaion.com/";
|
||||
public const int DEFAULT_API_RETRY_COUNT = 3;
|
||||
public const int DEFAULT_API_TIMEOUT_SECONDS = 40;
|
||||
|
||||
public const string CLAIM_NAME_ID = "nameid";
|
||||
public const string CLAIM_EMAIL = "unique_name";
|
||||
public const string CLAIM_ROLE = "role";
|
||||
|
||||
#endregion ApiConfig
|
||||
|
||||
#region SocketClient
|
||||
public const string ZMQ_HOST = "127.0.0.1";
|
||||
public const int ZMQ_PORT = 5127;
|
||||
|
||||
#endregion SocketClient
|
||||
|
||||
#region PythonConfig
|
||||
public const string AZAION_INFERENCE_PATH = "azaion-inference.exe";
|
||||
|
||||
public const string DEFAULT_ZMQ_HOST = "127.0.0.1";
|
||||
public const int DEFAULT_ZMQ_PORT = 5127;
|
||||
public const int DEFAULT_RETRY_COUNT = 25;
|
||||
public const int DEFAULT_TIMEOUT_SECONDS = 5;
|
||||
|
||||
#endregion PythonConfig
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
using Azaion.CommonSecurity.DTO;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Azaion.CommonSecurity.Services;
|
||||
|
||||
public class AzaionApiClient(HttpClient httpClient) : IDisposable
|
||||
{
|
||||
const string JSON_MEDIA = "application/json";
|
||||
|
||||
private static ApiConfig _apiConfig = null!;
|
||||
|
||||
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 static AzaionApiClient Create(ApiCredentials credentials, ApiConfig apiConfig)
|
||||
{
|
||||
_apiConfig = apiConfig;
|
||||
var api = new AzaionApiClient(new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(_apiConfig.Url),
|
||||
Timeout = TimeSpan.FromSeconds(_apiConfig.TimeoutSeconds)
|
||||
});
|
||||
|
||||
api.EnterCredentials(credentials);
|
||||
return api;
|
||||
}
|
||||
|
||||
public void EnterCredentials(ApiCredentials credentials)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
|
||||
throw new Exception("Email or password is empty!");
|
||||
|
||||
Email = credentials.Email;
|
||||
Password = credentials.Password.ToSecureString();
|
||||
}
|
||||
|
||||
public async Task<Stream> GetResource(string fileName, string password, HardwareInfo hardware, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = await Send(httpClient, new HttpRequestMessage(HttpMethod.Post, $"/resources/get/{_apiConfig.ResourcesFolder}")
|
||||
{
|
||||
Content = new StringContent(JsonConvert.SerializeObject(new { fileName, password, hardware }), Encoding.UTF8, JSON_MEDIA)
|
||||
}, cancellationToken);
|
||||
return await response.Content.ReadAsStreamAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private async Task Authorize()
|
||||
{
|
||||
if (string.IsNullOrEmpty(Email) || Password.Length == 0)
|
||||
throw new Exception("Email or password is empty! Please do EnterCredentials first!");
|
||||
|
||||
var payload = new
|
||||
{
|
||||
email = Email,
|
||||
password = Password.ToRealString()
|
||||
};
|
||||
var response = await httpClient.PostAsync(
|
||||
"login",
|
||||
new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, JSON_MEDIA));
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception($"EnterCredentials failed: {response.StatusCode}");
|
||||
|
||||
var responseData = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var result = JsonConvert.DeserializeObject<LoginResponse>(responseData);
|
||||
|
||||
if (string.IsNullOrEmpty(result?.Token))
|
||||
throw new Exception("JWT Token not found in response");
|
||||
|
||||
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, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrEmpty(JwtToken))
|
||||
await Authorize();
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JwtToken);
|
||||
var response = await client.SendAsync(request, cancellationToken);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await Authorize();
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JwtToken);
|
||||
response = await client.SendAsync(request, cancellationToken);
|
||||
}
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
return response;
|
||||
|
||||
var result = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
throw new Exception($"Failed: {response.StatusCode}! Result: {result}");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
httpClient.Dispose();
|
||||
Password.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ public interface IAuthProvider
|
||||
User CurrentUser { get; }
|
||||
}
|
||||
|
||||
|
||||
public class PythonResourceLoader : IResourceLoader, IAuthProvider
|
||||
{
|
||||
private readonly DealerSocket _dealer = new();
|
||||
@@ -27,11 +26,11 @@ public class PythonResourceLoader : IResourceLoader, IAuthProvider
|
||||
|
||||
public User CurrentUser { get; set; } = null!;
|
||||
|
||||
public PythonResourceLoader()
|
||||
public PythonResourceLoader(PythonConfig config)
|
||||
{
|
||||
StartPython();
|
||||
//StartPython();
|
||||
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
||||
_dealer.Connect($"tcp://{SecurityConstants.ZMQ_HOST}:{SecurityConstants.ZMQ_PORT}");
|
||||
_dealer.Connect($"tcp://{config.ZeroMqHost}:{config.ZeroMqPort}");
|
||||
}
|
||||
|
||||
private void StartPython()
|
||||
|
||||
Reference in New Issue
Block a user