Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotswap Stripe API secrets when they are updated. #512

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Options;
using Passwordless.AdminConsole.Billing.Configuration;
using Stripe;

namespace Passwordless.AdminConsole.Billing.BackgroundServices;

public class StripeConfigurationUpdaterBackgroundService(
IOptionsMonitor<BillingOptions> OptionsMonitor,
ILogger<StripeConfigurationUpdaterBackgroundService> Logger) : BackgroundService
{
private IDisposable? _onChangeListener;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
StripeConfiguration.ApiKey = OptionsMonitor.CurrentValue.ApiKey;
_onChangeListener = OptionsMonitor.OnChange(OnOptionsChanged);
return Task.CompletedTask;
}

private void OnOptionsChanged(BillingOptions options, string? arg2)
{
if (StripeConfiguration.ApiKey != options.ApiKey)
{
StripeConfiguration.ApiKey = options.ApiKey;
Logger.LogInformation("Stripe API key updated.");
}
}

public override void Dispose()
{
_onChangeListener?.Dispose();
base.Dispose();
}
}
6 changes: 4 additions & 2 deletions src/AdminConsole/Billing/BillingBootstrap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Passwordless.AdminConsole.Billing.BackgroundServices;
using Passwordless.AdminConsole.Billing.Configuration;
using Passwordless.AdminConsole.Services;
using Passwordless.Common.Configuration;
using Stripe;

namespace Passwordless.AdminConsole.Billing;
Expand All @@ -16,13 +18,13 @@ public static void AddBilling(this WebApplicationBuilder builder)
builder.Services.AddHostedService<UserCountUpdaterBackgroundService>();

// Todo: Improve this self-hosting story.
if (builder.Configuration.GetValue("SelfHosted", false))
if (builder.Configuration.IsSelfHosted())
{
builder.Services.AddScoped<ISharedBillingService, NoOpBillingService>();
}
else
{
StripeConfiguration.ApiKey = builder.Configuration["Stripe:ApiKey"];
builder.Services.AddHostedService<StripeConfigurationUpdaterBackgroundService>();
builder.Services.AddScoped<ISharedBillingService, SharedStripeBillingService>();
builder.Services.AddHostedService<MeteredBillingBackgroundService>();
}
Expand Down
8 changes: 4 additions & 4 deletions src/AdminConsole/Pages/Billing/webhook.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace Passwordless.AdminConsole.Pages.Billing;
public class Webhook : PageModel
{
private readonly ISharedBillingService _sharedBillingService;
private readonly BillingOptions _billingOptions;
private readonly IOptionsSnapshot<BillingOptions> _billingOptions;

public Webhook(ISharedBillingService sharedBillingService, IOptions<BillingOptions> billingOptions)
public Webhook(ISharedBillingService sharedBillingService, IOptionsSnapshot<BillingOptions> billingOptions)
{
_sharedBillingService = sharedBillingService;
_billingOptions = billingOptions.Value;
_billingOptions = billingOptions;
}

public async Task<IActionResult> OnPost()
Expand All @@ -31,7 +31,7 @@ public async Task<IActionResult> OnPost()
stripeEvent = EventUtility.ConstructEvent(
json,
Request.Headers["Stripe-Signature"],
_billingOptions.WebhookSecret
_billingOptions.Value.WebhookSecret
);
Console.WriteLine($"Webhook notification with type: {stripeEvent.Type} found for {stripeEvent.Id}");
}
Expand Down
Loading