1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-27 06:08:48 -05:00

Revert "Use IHttpClientFactory in more places"

This reverts commit 284501a4932b819b093406e0bcdf76def22b6eea.
This commit is contained in:
Justin Baur 2025-05-07 16:53:31 -04:00
parent 4fe99ab6ba
commit 5fedad91a3
No known key found for this signature in database
7 changed files with 24 additions and 36 deletions

View File

@ -11,17 +11,13 @@ namespace Bit.Admin.Controllers;
public class HomeController : Controller public class HomeController : Controller
{ {
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient = new HttpClient();
private readonly ILogger<HomeController> _logger; private readonly ILogger<HomeController> _logger;
public HomeController( public HomeController(GlobalSettings globalSettings, ILogger<HomeController> logger)
GlobalSettings globalSettings,
ILogger<HomeController> logger,
IHttpClientFactory httpClientFactory)
{ {
_globalSettings = globalSettings; _globalSettings = globalSettings;
_logger = logger; _logger = logger;
_httpClient = httpClientFactory.CreateClient();
} }
[Authorize] [Authorize]

View File

@ -8,16 +8,14 @@ namespace Bit.Admin.Jobs;
public class AliveJob : BaseJob public class AliveJob : BaseJob
{ {
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly HttpClient _httpClient; private HttpClient _httpClient = new HttpClient();
public AliveJob( public AliveJob(
GlobalSettings globalSettings, GlobalSettings globalSettings,
ILogger<AliveJob> logger, ILogger<AliveJob> logger)
IHttpClientFactory httpClientFactory)
: base(logger) : base(logger)
{ {
_globalSettings = globalSettings; _globalSettings = globalSettings;
_httpClient = httpClientFactory.CreateClient();
} }
protected async override Task ExecuteJobAsync(IJobExecutionContext context) protected async override Task ExecuteJobAsync(IJobExecutionContext context)

View File

@ -16,24 +16,27 @@ public class HibpController : Controller
{ {
private const string HibpBreachApi = "https://haveibeenpwned.com/api/v3/breachedaccount/{0}" + private const string HibpBreachApi = "https://haveibeenpwned.com/api/v3/breachedaccount/{0}" +
"?truncateResponse=false&includeUnverified=false"; "?truncateResponse=false&includeUnverified=false";
private readonly HttpClient _httpClient; private static HttpClient _httpClient;
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly ICurrentContext _currentContext; private readonly ICurrentContext _currentContext;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly string _userAgent; private readonly string _userAgent;
static HibpController()
{
_httpClient = new HttpClient();
}
public HibpController( public HibpController(
IUserService userService, IUserService userService,
ICurrentContext currentContext, ICurrentContext currentContext,
GlobalSettings globalSettings, GlobalSettings globalSettings)
IHttpClientFactory httpClientFactory)
{ {
_userService = userService; _userService = userService;
_currentContext = currentContext; _currentContext = currentContext;
_globalSettings = globalSettings; _globalSettings = globalSettings;
_userAgent = _globalSettings.SelfHosted ? "Bitwarden Self-Hosted" : "Bitwarden"; _userAgent = _globalSettings.SelfHosted ? "Bitwarden Self-Hosted" : "Bitwarden";
_httpClient = httpClientFactory.CreateClient();
} }
[HttpGet("breach")] [HttpGet("breach")]

View File

@ -1,4 +1,5 @@
using System.Text.Json.Serialization; using System.Net.Http.Headers;
using System.Text.Json.Serialization;
using Bit.Core.Billing.Enums; using Bit.Core.Billing.Enums;
using Bit.Core.Repositories; using Bit.Core.Repositories;
using Bit.Core.Settings; using Bit.Core.Settings;
@ -24,16 +25,23 @@ public class FreshsalesController : Controller
IOrganizationRepository organizationRepository, IOrganizationRepository organizationRepository,
IOptions<BillingSettings> billingSettings, IOptions<BillingSettings> billingSettings,
ILogger<FreshsalesController> logger, ILogger<FreshsalesController> logger,
GlobalSettings globalSettings, GlobalSettings globalSettings)
IHttpClientFactory httpClientFactory)
{ {
_userRepository = userRepository; _userRepository = userRepository;
_organizationRepository = organizationRepository; _organizationRepository = organizationRepository;
_logger = logger; _logger = logger;
_globalSettings = globalSettings; _globalSettings = globalSettings;
_httpClient = httpClientFactory.CreateClient("Freshsales");
_httpClient = new HttpClient
{
BaseAddress = new Uri("https://bitwarden.freshsales.io/api/")
};
_freshsalesApiKey = billingSettings.Value.FreshsalesApiKey; _freshsalesApiKey = billingSettings.Value.FreshsalesApiKey;
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Token",
$"token={_freshsalesApiKey}");
} }

View File

@ -10,7 +10,6 @@ using Bit.Core.Settings;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Bit.SharedWeb.Utilities; using Bit.SharedWeb.Utilities;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Quartz; using Quartz;
using Stripe; using Stripe;
@ -103,15 +102,6 @@ public class Startup
{ {
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", billingSettings.Onyx.ApiKey); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", billingSettings.Onyx.ApiKey);
}); });
services.AddHttpClient("Freshsales", (sp, client) =>
{
var billingSettings = sp.GetRequiredService<IOptions<BillingSettings>>().Value;
client.BaseAddress = new Uri("https://bitwarden.freshsales.io/api/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Token",
$"token={billingSettings.FreshsalesApiKey}"
);
});
services.AddScoped<IStripeFacade, StripeFacade>(); services.AddScoped<IStripeFacade, StripeFacade>();
services.AddScoped<IStripeEventService, StripeEventService>(); services.AddScoped<IStripeEventService, StripeEventService>();

View File

@ -31,18 +31,12 @@ public class FreshsalesControllerTests
var globalSettings = new GlobalSettings(); var globalSettings = new GlobalSettings();
globalSettings.BaseServiceUri.Admin = "https://test.com"; globalSettings.BaseServiceUri.Admin = "https://test.com";
var httpClientFactory = Substitute.For<IHttpClientFactory>();
httpClientFactory
.CreateClient("Freshsales")
.Returns(new HttpClient());
var sut = new FreshsalesController( var sut = new FreshsalesController(
userRepository, userRepository,
organizationRepository, organizationRepository,
billingSettings, billingSettings,
Substitute.For<ILogger<FreshsalesController>>(), Substitute.For<ILogger<FreshsalesController>>(),
globalSettings, globalSettings
httpClientFactory
); );
return (sut, userRepository, organizationRepository); return (sut, userRepository, organizationRepository);

View File

@ -285,7 +285,6 @@ public class Program
url = $"{installationUrl}/installations/"; url = $"{installationUrl}/installations/";
} }
var response = new HttpClient().GetAsync(url + _context.Install.InstallationId).GetAwaiter().GetResult(); var response = new HttpClient().GetAsync(url + _context.Install.InstallationId).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)