Skip to content

Commit 284501a

Browse files
committed
Use IHttpClientFactory in more places
1 parent 7a99f5d commit 284501a

File tree

7 files changed

+44
-25
lines changed

7 files changed

+44
-25
lines changed

src/Admin/Controllers/HomeController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ namespace Bit.Admin.Controllers;
1111
public class HomeController : Controller
1212
{
1313
private readonly GlobalSettings _globalSettings;
14-
private readonly HttpClient _httpClient = new HttpClient();
14+
private readonly HttpClient _httpClient;
1515
private readonly ILogger<HomeController> _logger;
1616

17-
public HomeController(GlobalSettings globalSettings, ILogger<HomeController> logger)
17+
public HomeController(
18+
GlobalSettings globalSettings,
19+
ILogger<HomeController> logger,
20+
IHttpClientFactory httpClientFactory)
1821
{
1922
_globalSettings = globalSettings;
2023
_logger = logger;
24+
_httpClient = httpClientFactory.CreateClient();
2125
}
2226

2327
[Authorize]

src/Admin/Jobs/AliveJob.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ namespace Bit.Admin.Jobs;
88
public class AliveJob : BaseJob
99
{
1010
private readonly GlobalSettings _globalSettings;
11-
private HttpClient _httpClient = new HttpClient();
11+
private readonly HttpClient _httpClient;
1212

1313
public AliveJob(
1414
GlobalSettings globalSettings,
15-
ILogger<AliveJob> logger)
15+
ILogger<AliveJob> logger,
16+
IHttpClientFactory httpClientFactory)
1617
: base(logger)
1718
{
1819
_globalSettings = globalSettings;
20+
_httpClient = httpClientFactory.CreateClient();
1921
}
2022

2123
protected async override Task ExecuteJobAsync(IJobExecutionContext context)

src/Api/Tools/Controllers/HibpController.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,24 @@ public class HibpController : Controller
1616
{
1717
private const string HibpBreachApi = "https://haveibeenpwned.com/api/v3/breachedaccount/{0}" +
1818
"?truncateResponse=false&includeUnverified=false";
19-
private static HttpClient _httpClient;
19+
private readonly HttpClient _httpClient;
2020

2121
private readonly IUserService _userService;
2222
private readonly ICurrentContext _currentContext;
2323
private readonly GlobalSettings _globalSettings;
2424
private readonly string _userAgent;
2525

26-
static HibpController()
27-
{
28-
_httpClient = new HttpClient();
29-
}
30-
3126
public HibpController(
3227
IUserService userService,
3328
ICurrentContext currentContext,
34-
GlobalSettings globalSettings)
29+
GlobalSettings globalSettings,
30+
IHttpClientFactory httpClientFactory)
3531
{
3632
_userService = userService;
3733
_currentContext = currentContext;
3834
_globalSettings = globalSettings;
3935
_userAgent = _globalSettings.SelfHosted ? "Bitwarden Self-Hosted" : "Bitwarden";
36+
_httpClient = httpClientFactory.CreateClient();
4037
}
4138

4239
[HttpGet("breach")]

src/Billing/Controllers/FreshsalesController.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Net.Http.Headers;
2-
using System.Text.Json.Serialization;
1+
using System.Text.Json.Serialization;
32
using Bit.Core.Billing.Enums;
43
using Bit.Core.Repositories;
54
using Bit.Core.Settings;
@@ -25,23 +24,16 @@ public FreshsalesController(IUserRepository userRepository,
2524
IOrganizationRepository organizationRepository,
2625
IOptions<BillingSettings> billingSettings,
2726
ILogger<FreshsalesController> logger,
28-
GlobalSettings globalSettings)
27+
GlobalSettings globalSettings,
28+
IHttpClientFactory httpClientFactory)
2929
{
3030
_userRepository = userRepository;
3131
_organizationRepository = organizationRepository;
3232
_logger = logger;
3333
_globalSettings = globalSettings;
34-
35-
_httpClient = new HttpClient
36-
{
37-
BaseAddress = new Uri("https://bitwarden.freshsales.io/api/")
38-
};
34+
_httpClient = httpClientFactory.CreateClient("Freshsales");
3935

4036
_freshsalesApiKey = billingSettings.Value.FreshsalesApiKey;
41-
42-
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
43-
"Token",
44-
$"token={_freshsalesApiKey}");
4537
}
4638

4739

src/Billing/Startup.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Bit.Core.Utilities;
1111
using Bit.SharedWeb.Utilities;
1212
using Microsoft.Extensions.DependencyInjection.Extensions;
13+
using Microsoft.Extensions.Options;
1314
using Quartz;
1415
using Stripe;
1516

@@ -102,6 +103,15 @@ public void ConfigureServices(IServiceCollection services)
102103
{
103104
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", billingSettings.Onyx.ApiKey);
104105
});
106+
services.AddHttpClient("Freshsales", (sp, client) =>
107+
{
108+
var billingSettings = sp.GetRequiredService<IOptions<BillingSettings>>().Value;
109+
client.BaseAddress = new Uri("https://bitwarden.freshsales.io/api/");
110+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
111+
"Token",
112+
$"token={billingSettings.FreshsalesApiKey}"
113+
);
114+
});
105115

106116
services.AddScoped<IStripeFacade, StripeFacade>();
107117
services.AddScoped<IStripeEventService, StripeEventService>();

test/Billing.Test/Controllers/FreshsalesControllerTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ private static (FreshsalesController, IUserRepository, IOrganizationRepository)
3131
var globalSettings = new GlobalSettings();
3232
globalSettings.BaseServiceUri.Admin = "https://test.com";
3333

34+
var httpClientFactory = Substitute.For<IHttpClientFactory>();
35+
httpClientFactory
36+
.CreateClient("Freshsales")
37+
.Returns(new HttpClient());
38+
3439
var sut = new FreshsalesController(
3540
userRepository,
3641
organizationRepository,
3742
billingSettings,
3843
Substitute.For<ILogger<FreshsalesController>>(),
39-
globalSettings
44+
globalSettings,
45+
httpClientFactory
4046
);
4147

4248
return (sut, userRepository, organizationRepository);

util/Setup/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net.Http.Json;
33
using Bit.Migrator;
44
using Bit.Setup.Enums;
5+
using Microsoft.Extensions.DependencyInjection;
56

67
namespace Bit.Setup;
78

@@ -285,7 +286,14 @@ private static bool ValidateInstallation()
285286
url = $"{installationUrl}/installations/";
286287
}
287288

288-
var response = new HttpClient().GetAsync(url + _context.Install.InstallationId).GetAwaiter().GetResult();
289+
// We need to get an HttpClient that has been configured with custom trust certificates.
290+
var httpClient = new ServiceCollection()
291+
.AddX509ChainCustomization()
292+
.BuildServiceProvider()
293+
.GetRequiredService<IHttpClientFactory>()
294+
.CreateClient();
295+
296+
var response = httpClient.GetAsync(url + _context.Install.InstallationId).GetAwaiter().GetResult();
289297

290298
if (!response.IsSuccessStatusCode)
291299
{

0 commit comments

Comments
 (0)