Skip to content

Commit

Permalink
Updated LicensingService to be a singleton again and moved IFeatureSe…
Browse files Browse the repository at this point in the history
…rvice up a frame in the call stack
  • Loading branch information
cturnbull-bitwarden committed Jan 9, 2025
1 parent e754ae4 commit 9e79688
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ public class CloudGetOrganizationLicenseQuery : ICloudGetOrganizationLicenseQuer
private readonly IPaymentService _paymentService;
private readonly ILicensingService _licensingService;
private readonly IProviderRepository _providerRepository;
private readonly IFeatureService _featureService;

public CloudGetOrganizationLicenseQuery(
IInstallationRepository installationRepository,
IPaymentService paymentService,
ILicensingService licensingService,
IProviderRepository providerRepository)
IProviderRepository providerRepository,
IFeatureService featureService)
{
_installationRepository = installationRepository;
_paymentService = paymentService;
_licensingService = licensingService;
_providerRepository = providerRepository;
_featureService = featureService;
}

public async Task<OrganizationLicense> GetLicenseAsync(Organization organization, Guid installationId,
Expand All @@ -38,11 +41,13 @@ public async Task<OrganizationLicense> GetLicenseAsync(Organization organization
}

var subscriptionInfo = await GetSubscriptionAsync(organization);

return new OrganizationLicense(organization, subscriptionInfo, installationId, _licensingService, version)
var license = new OrganizationLicense(organization, subscriptionInfo, installationId, _licensingService, version);
if (_featureService.IsEnabled(FeatureFlagKeys.SelfHostLicenseRefactor))
{
Token = await _licensingService.CreateOrganizationTokenAsync(organization, installationId, subscriptionInfo)
};
license.Token = await _licensingService.CreateOrganizationTokenAsync(organization, installationId, subscriptionInfo);
}

return license;
}

private async Task<SubscriptionInfo> GetSubscriptionAsync(Organization organization)
Expand Down
13 changes: 0 additions & 13 deletions src/Core/Services/Implementations/LicensingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class LicensingService : ILicensingService
private readonly ILogger<LicensingService> _logger;
private readonly ILicenseClaimsFactory<Organization> _organizationLicenseClaimsFactory;
private readonly ILicenseClaimsFactory<User> _userLicenseClaimsFactory;
private readonly IFeatureService _featureService;

private IDictionary<Guid, DateTime> _userCheckCache = new Dictionary<Guid, DateTime>();

Expand All @@ -42,7 +41,6 @@ public LicensingService(
ILogger<LicensingService> logger,
IGlobalSettings globalSettings,
ILicenseClaimsFactory<Organization> organizationLicenseClaimsFactory,
IFeatureService featureService,
ILicenseClaimsFactory<User> userLicenseClaimsFactory)
{
_userRepository = userRepository;
Expand All @@ -51,7 +49,6 @@ public LicensingService(
_logger = logger;
_globalSettings = globalSettings;
_organizationLicenseClaimsFactory = organizationLicenseClaimsFactory;
_featureService = featureService;
_userLicenseClaimsFactory = userLicenseClaimsFactory;

var certThumbprint = environment.IsDevelopment() ?
Expand Down Expand Up @@ -344,11 +341,6 @@ public ClaimsPrincipal GetClaimsPrincipalFromLicense(ILicense license)

public async Task<string> CreateOrganizationTokenAsync(Organization organization, Guid installationId, SubscriptionInfo subscriptionInfo)
{
if (!_featureService.IsEnabled(FeatureFlagKeys.SelfHostLicenseRefactor))
{
return null;
}

var licenseContext = new LicenseContext
{
InstallationId = installationId,
Expand All @@ -363,11 +355,6 @@ public async Task<string> CreateOrganizationTokenAsync(Organization organization

public async Task<string> CreateUserTokenAsync(User user, SubscriptionInfo subscriptionInfo)
{
if (!_featureService.IsEnabled(FeatureFlagKeys.SelfHostLicenseRefactor))
{
return null;
}

var licenseContext = new LicenseContext { SubscriptionInfo = subscriptionInfo };
var claims = await _userLicenseClaimsFactory.GenerateClaims(user, licenseContext);
var audience = $"user:{user.Id}";
Expand Down
2 changes: 1 addition & 1 deletion src/SharedWeb/Utilities/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static void AddDefaultServices(this IServiceCollection services, GlobalSe
services.AddScoped<IPaymentHistoryService, PaymentHistoryService>();
services.AddSingleton<IStripeSyncService, StripeSyncService>();
services.AddSingleton<IMailService, HandlebarsMailService>();
services.AddScoped<ILicensingService, LicensingService>();
services.AddSingleton<ILicensingService, LicensingService>();
services.AddSingleton<ILookupClient>(_ =>
{
var options = new LookupClientOptions { Timeout = TimeSpan.FromSeconds(15), UseTcpOnly = true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,35 @@ public async Task GetLicenseAsync_CreatesAndReturns(SutProvider<CloudGetOrganiza
sutProvider.GetDependency<IInstallationRepository>().GetByIdAsync(installationId).Returns(installation);
sutProvider.GetDependency<IPaymentService>().GetSubscriptionAsync(organization).Returns(subInfo);
sutProvider.GetDependency<ILicensingService>().SignLicense(Arg.Any<ILicense>()).Returns(licenseSignature);
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.SelfHostLicenseRefactor).Returns(false);

var result = await sutProvider.Sut.GetLicenseAsync(organization, installationId);

Assert.Equal(LicenseType.Organization, result.LicenseType);
Assert.Equal(organization.Id, result.Id);
Assert.Equal(installationId, result.InstallationId);
Assert.Equal(licenseSignature, result.SignatureBytes);
Assert.Null(result.Token);
}

[Theory]
[BitAutoData]
public async Task GetLicenseAsync_WhenFeatureFlagEnabled_CreatesToken(SutProvider<CloudGetOrganizationLicenseQuery> sutProvider,
Organization organization, Guid installationId, Installation installation, SubscriptionInfo subInfo,
byte[] licenseSignature, string token)
{
installation.Enabled = true;
sutProvider.GetDependency<IInstallationRepository>().GetByIdAsync(installationId).Returns(installation);
sutProvider.GetDependency<IPaymentService>().GetSubscriptionAsync(organization).Returns(subInfo);
sutProvider.GetDependency<ILicensingService>().SignLicense(Arg.Any<ILicense>()).Returns(licenseSignature);
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.SelfHostLicenseRefactor).Returns(true);
sutProvider.GetDependency<ILicensingService>()
.CreateOrganizationTokenAsync(organization, installationId, subInfo)
.Returns(token);

var result = await sutProvider.Sut.GetLicenseAsync(organization, installationId);

Assert.Equal(token, result.Token);
}

[Theory]
Expand Down

0 comments on commit 9e79688

Please sign in to comment.