Skip to content

Commit

Permalink
Adding stuff from NEST-516
Browse files Browse the repository at this point in the history
  • Loading branch information
DemeSzabolcs committed Mar 5, 2024
1 parent 2acd980 commit b59d4bb
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public static class FeatureNames
public const string AddSiteOwnerPermissionToRole = Maintenance + "." + nameof(AddSiteOwnerPermissionToRole);
public const string RemoveUsers = Maintenance + "." + nameof(RemoveUsers);
public const string ChangeUserSensitiveContent = Maintenance + "." + nameof(ChangeUserSensitiveContent);
public const string ResetStripeApiCredentials = Maintenance + "." + nameof(ResetStripeApiCredentials);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="9.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore.Commerce\src\Modules\OrchardCore.Commerce.Payment.Stripe\OrchardCore.Commerce.Payment.Stripe.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.ResetStripeApiCredentials;

public class ResetStripeApiCredentialsMaintenanceOptions
{
public bool IsEnabled { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Lombiq.Hosting.Tenants.Maintenance.Extensions;
using Lombiq.Hosting.Tenants.Maintenance.Models;
using Lombiq.Hosting.Tenants.Maintenance.Services;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using OrchardCore.Commerce.Payment.Stripe.Models;
using OrchardCore.Commerce.Payment.Stripe.Services;
using OrchardCore.Entities;
using OrchardCore.Environment.Shell;
using OrchardCore.Settings;
using System.Linq;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.ResetStripeApiCredentials;

public class ResetStripeApiCredentialsMaintenanceProvider : MaintenanceProviderBase
{
private readonly IOptions<ResetStripeApiCredentialsMaintenanceOptions> _options;
private readonly ISiteService _siteService;
private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly IShellFeaturesManager _shellFeaturesManager;

public ResetStripeApiCredentialsMaintenanceProvider(
IOptions<ResetStripeApiCredentialsMaintenanceOptions> options,
ISiteService siteService,
IDataProtectionProvider dataProtectionProvider,
IShellFeaturesManager shellFeaturesManager)
{
_options = options;
_siteService = siteService;
_dataProtectionProvider = dataProtectionProvider;
_shellFeaturesManager = shellFeaturesManager;
}

public override async Task<bool> ShouldExecuteAsync(MaintenanceTaskExecutionContext context) =>
_options.Value.IsEnabled &&
!context.WasLatestExecutionSuccessful() &&
(await _shellFeaturesManager.GetEnabledFeaturesAsync()).Any(feature =>
feature.Id == "OrchardCore.Commerce.Payment.Stripe");

public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context)
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
var settings = siteSettings.As<StripeApiSettings>();

if (!settings.SecretKey.IsNullOrEmpty() ||
!settings.PublishableKey.IsNullOrEmpty() ||
!settings.WebhookSigningSecret.IsNullOrEmpty())
{
siteSettings.Alter<StripeApiSettings>(nameof(StripeApiSettings), settings =>
{
// These are publicly available test keys.
settings.PublishableKey =
"pk_test_51H59owJmQoVhz82aWAoi9M5s8PC6sSAqFI7KfAD2NRKun5riDIOM0dvu2caM25a5f5JbYLMc5Umxw8Dl7dBIDNwM00yVbSX8uS";

var protector = _dataProtectionProvider.CreateProtector(nameof(StripeApiSettingsConfiguration));
settings.SecretKey = protector
.Protect("sk_test_51H59owJmQoVhz82aOUNOuCVbK0u1zjyRFKkFp9EfrqzWaUWqQni3oSxljsdTIu2YZ9XvlbeGjZRU7B7ye2EjJQE000Dm2DtMWD");

settings.WebhookSigningSecret = string.Empty;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Lombiq.Hosting.Tenants.Maintenance.Constants;
using Lombiq.Hosting.Tenants.Maintenance.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Modules;

namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.ResetStripeApiCredentials;

[Feature(FeatureNames.ResetStripeApiCredentials)]
public class Startup : StartupBase
{
private readonly IShellConfiguration _shellConfiguration;

public Startup(IShellConfiguration shellConfiguration) =>
_shellConfiguration = shellConfiguration;

public override void ConfigureServices(IServiceCollection services)
{
var options = new ResetStripeApiCredentialsMaintenanceOptions();
var configSection = _shellConfiguration.GetSection("Lombiq_Hosting_Tenants_Maintenance:ResetStripeApiCredentials");
configSection.Bind(options);
services.Configure<ResetStripeApiCredentialsMaintenanceOptions>(configSection);

services.AddScoped<IMaintenanceProvider, ResetStripeApiCredentialsMaintenanceProvider>();
}
}
8 changes: 8 additions & 0 deletions Lombiq.Hosting.Tenants.Maintenance/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@
DefaultTenantOnly = true,
Dependencies = [Maintenance]
)]

[assembly: Feature(
Id = ResetStripeApiCredentials,
Name = "Lombiq Hosting - Tenants Maintenance Reset Stripe API Credentials",
Description = "Replaces the Stripe Publishable Key and Secret Key to the publicly available test keys, if they are empty.",
Category = "Maintenance",
Dependencies = new[] { Maintenance }
)]

0 comments on commit b59d4bb

Please sign in to comment.