From 2394752d02c73611d6670b29810d8bd7e38f31c8 Mon Sep 17 00:00:00 2001 From: lamanoff Date: Thu, 27 Feb 2025 10:22:23 +0500 Subject: [PATCH 1/5] DEV-344 Add base --- .../Configuration/ServiceConfiguration.cs | 15 +- .../Services/MultiFactorApiClient.cs | 128 +++++++++++------- 2 files changed, 86 insertions(+), 57 deletions(-) diff --git a/MultiFactor.Ldap.Adapter/Configuration/ServiceConfiguration.cs b/MultiFactor.Ldap.Adapter/Configuration/ServiceConfiguration.cs index 1ce1186..89cbd77 100644 --- a/MultiFactor.Ldap.Adapter/Configuration/ServiceConfiguration.cs +++ b/MultiFactor.Ldap.Adapter/Configuration/ServiceConfiguration.cs @@ -1,5 +1,5 @@ //Copyright(c) 2021 MultiFactor -//Please see licence at +//Please see licence at //https://github.com/MultifactorLab/MultiFactor.Ldap.Adapter/blob/main/LICENSE.md using MultiFactor.Ldap.Adapter.Core; @@ -60,7 +60,7 @@ public ClientConfiguration GetClient(IPAddress ip) /// /// Multifactor API URL /// - public string ApiUrl { get; set; } + public string[] ApiUrls { get; set; } /// /// HTTP Proxy for API @@ -115,9 +115,14 @@ public static ServiceConfiguration Load(ILogger logger) throw new Exception("Configuration error: 'logging-level' element not found"); } + var apiUrls = apiUrlSetting + .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) + .Select(x => x.Trim()) + .Distinct() + .ToArray(); var configuration = new ServiceConfiguration { - ApiUrl = apiUrlSetting, + ApiUrls = apiUrls, ApiProxy = apiProxySetting, ApiTimeout = apiTimeout, LogLevel = logLevelSetting, @@ -231,7 +236,7 @@ private static ClientConfiguration Load(string name, AppSettingsSection appSetti LdapServer = ldapServerSetting, MultifactorApiKey = multifactorApiKeySetting, MultifactorApiSecret = multifactorApiSecretSetting, - TransformLdapIdentity = string.IsNullOrEmpty(transformLdapIdentity) + TransformLdapIdentity = string.IsNullOrEmpty(transformLdapIdentity) ? LdapIdentityFormat.None : (LdapIdentityFormat)Enum.Parse(typeof(LdapIdentityFormat), transformLdapIdentity, true) }; @@ -307,7 +312,7 @@ private static ClientConfiguration Load(string name, AppSettingsSection appSetti { throw new Exception($"Configuration error: Can't parse '{Constants.Configuration.AuthenticationCacheLifetime}' value"); } - + if (TimeSpan.TryParse(ldapBindTimeout, out var bindTimeout)) { if (bindTimeout > TimeSpan.Zero) diff --git a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs index 8788fac..b6b8404 100644 --- a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs +++ b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs @@ -1,5 +1,5 @@ //Copyright(c) 2021 MultiFactor -//Please see licence at +//Please see licence at //https://github.com/MultifactorLab/MultiFactor.Ldap.Adapter/blob/main/LICENSE.md @@ -12,6 +12,9 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; +using System.Linq; +using Polly; +using Polly.Timeout; namespace MultiFactor.Ldap.Adapter.Services { @@ -64,13 +67,12 @@ public async Task Authenticate(ConnectedClientInfo connectedClient) return true; } - var url = _configuration.ApiUrl + "/access/requests/la"; var payload = new { Identity = connectedClient.Username, }; - var response = await SendRequest(connectedClient.ClientConfiguration, url, payload); + var response = await SendRequest(connectedClient.ClientConfiguration, _configuration.ApiUrls, payload); if (response == null) { @@ -88,80 +90,102 @@ public async Task Authenticate(ConnectedClientInfo connectedClient) { var reason = response?.ReplyMessage; var phone = response?.Phone; - _logger.Warning("Second factor verification for user '{user:l}' failed with reason='{reason:l}'. User phone {phone:l}", + _logger.Warning("Second factor verification for user '{user:l}' failed with reason='{reason:l}'. User phone {phone:l}", connectedClient.Username, reason, phone); } return response.Granted; } - private async Task SendRequest(ClientConfiguration clientConfig, string url, object payload) + private async Task SendRequest(ClientConfiguration clientConfig, string[] baseUrls, object payload) { - try - { - //make sure we can communicate securely - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; - ServicePointManager.DefaultConnectionLimit = 100; + //make sure we can communicate securely + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + ServicePointManager.DefaultConnectionLimit = 100; - var json = JsonSerializer.Serialize(payload, _serialazerOptions); + var json = JsonSerializer.Serialize(payload, _serialazerOptions); - _logger.Debug($"Sending request to API: {json}"); + _logger.Debug("Sending request to API: {Body}.", json); - //basic authorization - var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(clientConfig.MultifactorApiKey + ":" + clientConfig.MultifactorApiSecret)); - var httpClient = _httpClientFactory.CreateClient(nameof(MultiFactorApiClient)); + //basic authorization + var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientConfig.MultifactorApiKey}:{clientConfig.MultifactorApiSecret}")); + var httpClient = _httpClientFactory.CreateClient(nameof(MultiFactorApiClient)); - StringContent jsonContent = new StringContent(json, Encoding.UTF8, "application/json"); - HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, url) - { - Content = jsonContent - }; - message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth); - var res = await httpClient.SendAsync(message); - - if ((int)res.StatusCode == 429) + foreach (var url in baseUrls.Select(baseUrl => $"{baseUrl}/access/requests/la")) + { + try { - _logger.Warning("Got unsuccessful response from API: {@response}", res.ReasonPhrase); - return new MultiFactorAccessRequest() { Status = "Denied", ReplyMessage = "Too many requests"}; - } - - var jsonResponse = await res.Content.ReadAsStringAsync(); - var response = JsonSerializer.Deserialize>(jsonResponse, _serialazerOptions); + var res = await TrySendRequest(httpClient, json, url, auth); + if (res == null) + continue; - _logger.Debug("Received response from API: {@response}", response); + if ((int)res.StatusCode == 429) + { + _logger.Warning("Got unsuccessful response from API '{ApiUrl:l}': {@response}", url, res.ReasonPhrase); + return new MultiFactorAccessRequest { Status = "Denied", ReplyMessage = "Too many requests" }; + } - if (!response.Success) - { - _logger.Warning("Got unsuccessful response from API: {@response}", response); - } + var jsonResponse = await res.Content.ReadAsStringAsync(); + var response = JsonSerializer.Deserialize>(jsonResponse, _serialazerOptions); - return response.Model; - } - catch (TaskCanceledException tce) - { - _logger.Error(tce, $"Multifactor API host unreachable {url}: timeout!"); + _logger.Debug("Received response from API '{ApiUrl:l}': {@response}", url, response); + + if (!response.Success) + { + _logger.Warning("Got unsuccessful response from API: {@response}", response); + } - if (clientConfig.BypassSecondFactorWhenApiUnreachable) + return response.Model; + + } + catch (Exception ex) { - _logger.Warning("Bypass second factor"); - return MultiFactorAccessRequest.Bypass; + _logger.Error(ex, "Multifactor API host '{ApiUrl:l}' unreachable: {Message:l}", url, ex.Message); } - - return null; } - catch (Exception ex) + + _logger.Error("Multifactor API Cloud unreachable"); + + if (clientConfig.BypassSecondFactorWhenApiUnreachable) { - _logger.Error(ex, $"Multifactor API host unreachable {url}: {ex.Message}"); + _logger.Warning("Bypass second factor"); + return MultiFactorAccessRequest.Bypass; + } + return null; + } - if (clientConfig.BypassSecondFactorWhenApiUnreachable) + private async Task TrySendRequest(HttpClient httpClient, string json, string url, string auth) + { + var policy = Policy + .Handle() + .Or() + .WaitAndRetryAsync(1, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); + try + { + return await policy.ExecuteAsync(() => { - _logger.Warning("Bypass second factor"); - return MultiFactorAccessRequest.Bypass; - } - + _logger.Information("Sending request to API '{ApiUrl:l}'.", url); + var message = PrepareHttpRequestMessage(json, url, auth); + return httpClient.SendAsync(message); + }); + } + catch (Exception exception) + { + _logger.Warning("Failed to send request to API '{ApiUrl:l}': {Message:l}", url, exception.Message); return null; } } + + private static HttpRequestMessage PrepareHttpRequestMessage(string json, string url, string auth) + { + var jsonContent = new StringContent(json, Encoding.UTF8, "application/json"); + var message = new HttpRequestMessage(HttpMethod.Post, url) + { + Content = jsonContent + }; + message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth); + return message; + } } public class MultiFactorApiResponse From 627589667392441069b8c796746bddd845119e3d Mon Sep 17 00:00:00 2001 From: Lamanov Maksim Date: Thu, 27 Feb 2025 10:33:07 +0500 Subject: [PATCH 2/5] DEV-344 Add deps --- .../MultiFactor.Ldap.Adapter.csproj | 13 +++++++++++++ MultiFactor.Ldap.Adapter/packages.config | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj b/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj index 84cc8d1..46d2f37 100644 --- a/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj +++ b/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj @@ -90,6 +90,9 @@ ..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.1\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll + + ..\packages\Microsoft.Bcl.TimeProvider.8.0.0\lib\net462\Microsoft.Bcl.TimeProvider.dll + ..\packages\Microsoft.Extensions.Configuration.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.Configuration.Abstractions.dll @@ -120,9 +123,16 @@ ..\packages\Microsoft.Net.Http.Headers.2.2.0\lib\netstandard2.0\Microsoft.Net.Http.Headers.dll + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + ..\packages\Polly.8.5.2\lib\net462\Polly.dll + + + ..\packages\Polly.Core.8.5.2\lib\net462\Polly.Core.dll + ..\packages\Serilog.2.10.0\lib\net46\Serilog.dll @@ -145,6 +155,9 @@ ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + ..\packages\System.ComponentModel.Annotations.4.5.0\lib\net461\System.ComponentModel.Annotations.dll + diff --git a/MultiFactor.Ldap.Adapter/packages.config b/MultiFactor.Ldap.Adapter/packages.config index f15a554..b4570d6 100644 --- a/MultiFactor.Ldap.Adapter/packages.config +++ b/MultiFactor.Ldap.Adapter/packages.config @@ -7,6 +7,7 @@ + @@ -18,6 +19,8 @@ + + @@ -25,6 +28,7 @@ + From b1b2a6e44e8e943e1e58c697aa0d551970945bb2 Mon Sep 17 00:00:00 2001 From: Lamanov Maksim Date: Thu, 27 Feb 2025 10:43:02 +0500 Subject: [PATCH 3/5] DEV-344 Rewrite with Microsoft.Extensions.Http.Resilience --- .../Extensions/ServiceCollectionExtensions.cs | 13 ++- .../MultiFactor.Ldap.Adapter.csproj | 107 ++++++++++++++---- .../Services/MultiFactorApiClient.cs | 21 ++-- MultiFactor.Ldap.Adapter/packages.config | 43 +++++-- 4 files changed, 136 insertions(+), 48 deletions(-) diff --git a/MultiFactor.Ldap.Adapter/Extensions/ServiceCollectionExtensions.cs b/MultiFactor.Ldap.Adapter/Extensions/ServiceCollectionExtensions.cs index ea56902..fe4cf6e 100644 --- a/MultiFactor.Ldap.Adapter/Extensions/ServiceCollectionExtensions.cs +++ b/MultiFactor.Ldap.Adapter/Extensions/ServiceCollectionExtensions.cs @@ -12,6 +12,8 @@ using System.Net; using System.Net.Http; using System.Security.Cryptography.X509Certificates; +using Microsoft.Extensions.Http.Resilience; +using Polly; namespace MultiFactor.Ldap.Adapter.Extensions { @@ -48,7 +50,16 @@ public static void AddHttpClientWithProxy(this IServiceCollection services) return handler; }) - .AddHttpMessageHandler(); + .AddHttpMessageHandler() + .AddResilienceHandler("mf-api-pipeline", x => + { + x.AddRetry(new HttpRetryStrategyOptions + { + MaxRetryAttempts = 2, + Delay = TimeSpan.FromSeconds(1), + BackoffType = DelayBackoffType.Exponential + }); + }); } public static void ConfigureApplicationServices(this IServiceCollection services, LoggingLevelSwitch levelSwitch, string syslogInfoMessage) diff --git a/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj b/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj index 46d2f37..9df9f5f 100644 --- a/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj +++ b/MultiFactor.Ldap.Adapter/MultiFactor.Ldap.Adapter.csproj @@ -90,36 +90,90 @@ ..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.1\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll - - ..\packages\Microsoft.Bcl.TimeProvider.8.0.0\lib\net462\Microsoft.Bcl.TimeProvider.dll + + ..\packages\Microsoft.Bcl.HashCode.1.1.1\lib\net461\Microsoft.Bcl.HashCode.dll + + + ..\packages\Microsoft.Bcl.TimeProvider.8.0.1\lib\net462\Microsoft.Bcl.TimeProvider.dll + + + ..\packages\Microsoft.Extensions.AmbientMetadata.Application.9.2.0\lib\net462\Microsoft.Extensions.AmbientMetadata.Application.dll + + + ..\packages\Microsoft.Extensions.Compliance.Abstractions.9.2.0\lib\netstandard2.0\Microsoft.Extensions.Compliance.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Configuration.8.0.0\lib\net462\Microsoft.Extensions.Configuration.dll ..\packages\Microsoft.Extensions.Configuration.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.Configuration.Abstractions.dll - - ..\packages\Microsoft.Extensions.DependencyInjection.8.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.dll + + ..\packages\Microsoft.Extensions.Configuration.Binder.8.0.2\lib\net462\Microsoft.Extensions.Configuration.Binder.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.8.0.1\lib\net462\Microsoft.Extensions.DependencyInjection.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.8.0.2\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.AutoActivation.9.2.0\lib\net462\Microsoft.Extensions.DependencyInjection.AutoActivation.dll + + + ..\packages\Microsoft.Extensions.Diagnostics.8.0.1\lib\net462\Microsoft.Extensions.Diagnostics.dll + + + ..\packages\Microsoft.Extensions.Diagnostics.Abstractions.8.0.1\lib\net462\Microsoft.Extensions.Diagnostics.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Diagnostics.ExceptionSummarization.9.2.0\lib\net462\Microsoft.Extensions.Diagnostics.ExceptionSummarization.dll + + + ..\packages\Microsoft.Extensions.FileProviders.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.FileProviders.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Hosting.Abstractions.8.0.1\lib\net462\Microsoft.Extensions.Hosting.Abstractions.dll - - ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + ..\packages\Microsoft.Extensions.Http.8.0.1\lib\net462\Microsoft.Extensions.Http.dll - - ..\packages\Microsoft.Extensions.Http.8.0.0\lib\net462\Microsoft.Extensions.Http.dll + + ..\packages\Microsoft.Extensions.Http.Diagnostics.9.2.0\lib\net462\Microsoft.Extensions.Http.Diagnostics.dll - - ..\packages\Microsoft.Extensions.Logging.8.0.0\lib\net462\Microsoft.Extensions.Logging.dll + + ..\packages\Microsoft.Extensions.Http.Resilience.9.2.0\lib\net462\Microsoft.Extensions.Http.Resilience.dll - - ..\packages\Microsoft.Extensions.Logging.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll + + ..\packages\Microsoft.Extensions.Logging.8.0.1\lib\net462\Microsoft.Extensions.Logging.dll - - ..\packages\Microsoft.Extensions.ObjectPool.2.2.0\lib\netstandard2.0\Microsoft.Extensions.ObjectPool.dll + + ..\packages\Microsoft.Extensions.Logging.Abstractions.8.0.3\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll - - ..\packages\Microsoft.Extensions.Options.8.0.0\lib\net462\Microsoft.Extensions.Options.dll + + ..\packages\Microsoft.Extensions.Logging.Configuration.8.0.1\lib\net462\Microsoft.Extensions.Logging.Configuration.dll + + + ..\packages\Microsoft.Extensions.ObjectPool.8.0.13\lib\net462\Microsoft.Extensions.ObjectPool.dll + + + ..\packages\Microsoft.Extensions.Options.8.0.2\lib\net462\Microsoft.Extensions.Options.dll + + + ..\packages\Microsoft.Extensions.Options.ConfigurationExtensions.8.0.0\lib\net462\Microsoft.Extensions.Options.ConfigurationExtensions.dll ..\packages\Microsoft.Extensions.Primitives.8.0.0\lib\net462\Microsoft.Extensions.Primitives.dll + + ..\packages\Microsoft.Extensions.Resilience.9.2.0\lib\net462\Microsoft.Extensions.Resilience.dll + + + ..\packages\Microsoft.Extensions.Telemetry.9.2.0\lib\net462\Microsoft.Extensions.Telemetry.dll + + + ..\packages\Microsoft.Extensions.Telemetry.Abstractions.9.2.0\lib\net462\Microsoft.Extensions.Telemetry.Abstractions.dll + ..\packages\Microsoft.Net.Http.Headers.2.2.0\lib\netstandard2.0\Microsoft.Net.Http.Headers.dll @@ -127,11 +181,14 @@ ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll - - ..\packages\Polly.8.5.2\lib\net462\Polly.dll - - ..\packages\Polly.Core.8.5.2\lib\net462\Polly.Core.dll + ..\packages\Polly.Core.8.4.2\lib\net462\Polly.Core.dll + + + ..\packages\Polly.Extensions.8.4.2\lib\net462\Polly.Extensions.dll + + + ..\packages\Polly.RateLimiting.8.4.2\lib\net462\Polly.RateLimiting.dll ..\packages\Serilog.2.10.0\lib\net46\Serilog.dll @@ -155,6 +212,9 @@ ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + ..\packages\System.Collections.Immutable.8.0.0\lib\net462\System.Collections.Immutable.dll + ..\packages\System.ComponentModel.Annotations.4.5.0\lib\net461\System.ComponentModel.Annotations.dll @@ -162,8 +222,8 @@ - - ..\packages\System.Diagnostics.DiagnosticSource.8.0.0\lib\net462\System.Diagnostics.DiagnosticSource.dll + + ..\packages\System.Diagnostics.DiagnosticSource.8.0.1\lib\net462\System.Diagnostics.DiagnosticSource.dll ..\packages\System.IO.Pipelines.9.0.1\lib\net462\System.IO.Pipelines.dll @@ -191,6 +251,9 @@ ..\packages\System.Text.Json.9.0.1\lib\net462\System.Text.Json.dll + + ..\packages\System.Threading.RateLimiting.8.0.0\lib\net462\System.Threading.RateLimiting.dll + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll diff --git a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs index b6b8404..fe2afb5 100644 --- a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs +++ b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs @@ -113,9 +113,11 @@ private async Task SendRequest(ClientConfiguration cli foreach (var url in baseUrls.Select(baseUrl => $"{baseUrl}/access/requests/la")) { + _logger.Information("Sending request to API '{ApiUrl:l}'.", url); try { - var res = await TrySendRequest(httpClient, json, url, auth); + var message = PrepareHttpRequestMessage(json, url, auth); + var res = await TrySendRequest(httpClient, message); if (res == null) continue; @@ -154,24 +156,15 @@ private async Task SendRequest(ClientConfiguration cli return null; } - private async Task TrySendRequest(HttpClient httpClient, string json, string url, string auth) + private async Task TrySendRequest(HttpClient httpClient, HttpRequestMessage message) { - var policy = Policy - .Handle() - .Or() - .WaitAndRetryAsync(1, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); try { - return await policy.ExecuteAsync(() => - { - _logger.Information("Sending request to API '{ApiUrl:l}'.", url); - var message = PrepareHttpRequestMessage(json, url, auth); - return httpClient.SendAsync(message); - }); + return await httpClient.SendAsync(message); } - catch (Exception exception) + catch (HttpRequestException exception) { - _logger.Warning("Failed to send request to API '{ApiUrl:l}': {Message:l}", url, exception.Message); + _logger.Warning("Failed to send request to API '{ApiUrl:l}': {Message:l}", message.RequestUri, exception.Message); return null; } } diff --git a/MultiFactor.Ldap.Adapter/packages.config b/MultiFactor.Ldap.Adapter/packages.config index b4570d6..31150d1 100644 --- a/MultiFactor.Ldap.Adapter/packages.config +++ b/MultiFactor.Ldap.Adapter/packages.config @@ -7,20 +7,40 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - @@ -28,8 +48,8 @@ + - @@ -37,6 +57,7 @@ + \ No newline at end of file From d67070845740c0db76bf889f9e054d7f2313bcb2 Mon Sep 17 00:00:00 2001 From: Lamanov Maksim Date: Fri, 28 Feb 2025 13:51:16 +0500 Subject: [PATCH 4/5] DEV-344 Fix naming --- MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs index fe2afb5..a469a62 100644 --- a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs +++ b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs @@ -116,8 +116,8 @@ private async Task SendRequest(ClientConfiguration cli _logger.Information("Sending request to API '{ApiUrl:l}'.", url); try { - var message = PrepareHttpRequestMessage(json, url, auth); - var res = await TrySendRequest(httpClient, message); + var message = CreateHttpRequestMessage(json, url, auth); + var res = await TrySendRequestAsync(httpClient, message); if (res == null) continue; @@ -156,7 +156,7 @@ private async Task SendRequest(ClientConfiguration cli return null; } - private async Task TrySendRequest(HttpClient httpClient, HttpRequestMessage message) + private async Task TrySendRequestAsync(HttpClient httpClient, HttpRequestMessage message) { try { @@ -169,7 +169,7 @@ private async Task TrySendRequest(HttpClient httpClient, Ht } } - private static HttpRequestMessage PrepareHttpRequestMessage(string json, string url, string auth) + private static HttpRequestMessage CreateHttpRequestMessage(string json, string url, string auth) { var jsonContent = new StringContent(json, Encoding.UTF8, "application/json"); var message = new HttpRequestMessage(HttpMethod.Post, url) From 710c9a415a67bb41b2f39a8a8aa90488cdc5e356 Mon Sep 17 00:00:00 2001 From: lamanoff Date: Thu, 3 Apr 2025 13:32:34 +0500 Subject: [PATCH 5/5] DEV-344 Fix non-failed status codes --- MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs index a469a62..38f9dc7 100644 --- a/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs +++ b/MultiFactor.Ldap.Adapter/Services/MultiFactorApiClient.cs @@ -135,6 +135,7 @@ private async Task SendRequest(ClientConfiguration cli if (!response.Success) { _logger.Warning("Got unsuccessful response from API: {@response}", response); + throw new HttpRequestException($"Got unsuccessful response from API. Status code: {res.StatusCode}."); } return response.Model;