Skip to content
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
Expand Up @@ -2,6 +2,7 @@
using Bit.Api.Billing.Models.Requests.Payment;
using Bit.Api.Billing.Models.Requests.Premium;
using Bit.Core;
using Bit.Core.Billing.Licenses.Queries;
using Bit.Core.Billing.Payment.Commands;
using Bit.Core.Billing.Payment.Queries;
using Bit.Core.Billing.Premium.Commands;
Expand All @@ -21,6 +22,7 @@ public class AccountBillingVNextController(
ICreatePremiumCloudHostedSubscriptionCommand createPremiumCloudHostedSubscriptionCommand,
IGetCreditQuery getCreditQuery,
IGetPaymentMethodQuery getPaymentMethodQuery,
IGetUserLicenseQuery getUserLicenseQuery,
IUpdatePaymentMethodCommand updatePaymentMethodCommand) : BaseBillingController
{
[HttpGet("credit")]
Expand Down Expand Up @@ -77,4 +79,13 @@ public async Task<IResult> CreateSubscriptionAsync(
user, paymentMethod, billingAddress, additionalStorageGb);
return Handle(result);
}

[HttpGet("license")]
[InjectUser]
public async Task<IResult> GetLicenseAsync(
[BindNever] User user)
{
var response = await getUserLicenseQuery.Run(user);
return TypedResults.Ok(response);
}
}
2 changes: 2 additions & 0 deletions src/Core/Billing/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
๏ปฟusing Bit.Core.Billing.Caches;
using Bit.Core.Billing.Caches.Implementations;
using Bit.Core.Billing.Licenses;
using Bit.Core.Billing.Licenses.Extensions;
using Bit.Core.Billing.Organizations.Commands;
using Bit.Core.Billing.Organizations.Queries;
Expand Down Expand Up @@ -28,6 +29,7 @@ public static void AddBillingOperations(this IServiceCollection services)
services.AddTransient<ISetupIntentCache, SetupIntentDistributedCache>();
services.AddTransient<ISubscriberService, SubscriberService>();
services.AddLicenseServices();
services.AddLicenseOperations();
services.AddPricingClient();
services.AddPaymentOperations();
services.AddOrganizationLicenseCommandsQueries();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
๏ปฟusing System.Security.Claims;
using Bit.Core.Billing.Licenses.Extensions;
using Bit.Core.Billing.Models.Business;
using Bit.Core.Models.Api;

namespace Bit.Core.Billing.Licenses.Models.Api.Response;

/// <summary>
/// Response model containing user license information.
/// Separated from subscription data to maintain separation of concerns.
/// </summary>
public class LicenseResponseModel : ResponseModel
{
public LicenseResponseModel(UserLicense license, ClaimsPrincipal? claimsPrincipal)
: base("license")
{
License = license;

// CRITICAL: When a license has a Token (JWT), ALWAYS use the expiration from the token claim
// The token's expiration is cryptographically secured and cannot be tampered with
// The file's Expires property can be manually edited and should NOT be trusted for display
if (claimsPrincipal != null)
{
Expiration = claimsPrincipal.GetValue<DateTime?>(UserLicenseConstants.Expires);
}
else
{
// No token - use the license file expiration (for older licenses without tokens)
Expiration = license.Expires;
}
}

/// <summary>
/// The user's license containing feature entitlements and metadata.
/// </summary>
public UserLicense License { get; set; }

/// <summary>
/// The license expiration date.
/// Extracted from the cryptographically secured JWT token when available,
/// otherwise falls back to the license file's expiration date.
/// </summary>
public DateTime? Expiration { get; set; }
}
23 changes: 23 additions & 0 deletions src/Core/Billing/Licenses/Queries/GetUserLicenseQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
๏ปฟusing Bit.Core.Billing.Licenses.Models.Api.Response;
using Bit.Core.Billing.Services;
using Bit.Core.Entities;
using Bit.Core.Services;

namespace Bit.Core.Billing.Licenses.Queries;

public interface IGetUserLicenseQuery
{
Task<LicenseResponseModel> Run(User user);
}

public class GetUserLicenseQuery(
IUserService userService,
ILicensingService licensingService) : IGetUserLicenseQuery
{
public async Task<LicenseResponseModel> Run(User user)
{
var license = await userService.GenerateLicenseAsync(user);
var claimsPrincipal = licensingService.GetClaimsPrincipalFromLicense(license);
return new LicenseResponseModel(license, claimsPrincipal);
}
}
13 changes: 13 additions & 0 deletions src/Core/Billing/Licenses/Registrations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
๏ปฟusing Bit.Core.Billing.Licenses.Queries;
using Microsoft.Extensions.DependencyInjection;

namespace Bit.Core.Billing.Licenses;

public static class Registrations
{
public static void AddLicenseOperations(this IServiceCollection services)
{
// Queries
services.AddTransient<IGetUserLicenseQuery, GetUserLicenseQuery>();
}
}
14 changes: 14 additions & 0 deletions test/Api.Test/Billing/Controllers/AccountsControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public async Task GetSubscriptionAsync_WhenFeatureFlagEnabled_IncludesDiscount(
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());

user.Gateway = GatewayType.Stripe; // User has payment gateway

Expand Down Expand Up @@ -124,6 +125,7 @@ public async Task GetSubscriptionAsync_WhenFeatureFlagDisabled_ExcludesDiscount(
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(false);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());

user.Gateway = GatewayType.Stripe; // User has payment gateway

Expand Down Expand Up @@ -161,6 +163,7 @@ public async Task GetSubscriptionAsync_WithNonMatchingCouponId_ExcludesDiscount(
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());

user.Gateway = GatewayType.Stripe; // User has payment gateway

Expand Down Expand Up @@ -207,6 +210,7 @@ public async Task GetSubscriptionAsync_WhenNoGateway_ExcludesDiscount(User user,
};
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_userService.GenerateLicenseAsync(user).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());

// Act
var result = await _sut.GetSubscriptionAsync(_globalSettings, _paymentService);
Expand Down Expand Up @@ -243,6 +247,7 @@ public async Task GetSubscriptionAsync_WithInactiveDiscount_ExcludesDiscount(
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());

user.Gateway = GatewayType.Stripe; // User has payment gateway

Expand Down Expand Up @@ -293,6 +298,7 @@ public async Task GetSubscriptionAsync_FullPipeline_ConvertsStripeDiscountToApiR
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());

user.Gateway = GatewayType.Stripe;

Expand Down Expand Up @@ -349,6 +355,7 @@ public async Task GetSubscriptionAsync_FullPipeline_WithFeatureFlagToggle_Contro
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act & Assert - Feature flag ENABLED
Expand Down Expand Up @@ -413,6 +420,7 @@ public async Task GetSubscriptionAsync_IntegrationTest_CompletePipelineFromStrip
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act - Step 4: Call AccountsController.GetSubscriptionAsync
Expand Down Expand Up @@ -507,6 +515,7 @@ public async Task GetSubscriptionAsync_IntegrationTest_MultipleDiscountsInSubscr
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act
Expand Down Expand Up @@ -558,6 +567,7 @@ public async Task GetSubscriptionAsync_IntegrationTest_BothPercentOffAndAmountOf
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act
Expand Down Expand Up @@ -611,6 +621,7 @@ public async Task GetSubscriptionAsync_IntegrationTest_BillingSubscriptionMapsTh
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act
Expand Down Expand Up @@ -658,6 +669,7 @@ public async Task GetSubscriptionAsync_IntegrationTest_BillingUpcomingInvoiceMap
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act
Expand Down Expand Up @@ -726,6 +738,7 @@ public async Task GetSubscriptionAsync_IntegrationTest_CompletePipelineWithAllCo
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;

// Act - Full pipeline: Stripe โ†’ SubscriptionInfo โ†’ SubscriptionResponseModel โ†’ API response
Expand Down Expand Up @@ -791,6 +804,7 @@ public async Task GetSubscriptionAsync_NullGateway_WithDiscountFlagEnabled_Never
};
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_userService.GenerateLicenseAsync(user).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true); // Flag enabled

// Act
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
๏ปฟusing Bit.Api.Billing.Controllers.VNext;
using Bit.Core.Billing.Licenses.Queries;
using Bit.Core.Billing.Payment.Commands;
using Bit.Core.Billing.Payment.Queries;
using Bit.Core.Billing.Premium.Commands;
using Bit.Core.Entities;
using Bit.Test.Common.AutoFixture.Attributes;
using Microsoft.AspNetCore.Http;
using NSubstitute;
using Xunit;

namespace Bit.Api.Test.Billing.Controllers.VNext;

public class AccountBillingVNextControllerTests
{
private readonly ICreateBitPayInvoiceForCreditCommand _createBitPayInvoiceForCreditCommand;
private readonly ICreatePremiumCloudHostedSubscriptionCommand _createPremiumCloudHostedSubscriptionCommand;
private readonly IGetCreditQuery _getCreditQuery;
private readonly IGetPaymentMethodQuery _getPaymentMethodQuery;
private readonly IGetUserLicenseQuery _getUserLicenseQuery;
private readonly IUpdatePaymentMethodCommand _updatePaymentMethodCommand;
private readonly AccountBillingVNextController _sut;

public AccountBillingVNextControllerTests()
{
_createBitPayInvoiceForCreditCommand = Substitute.For<ICreateBitPayInvoiceForCreditCommand>();
_createPremiumCloudHostedSubscriptionCommand = Substitute.For<ICreatePremiumCloudHostedSubscriptionCommand>();
_getCreditQuery = Substitute.For<IGetCreditQuery>();
_getPaymentMethodQuery = Substitute.For<IGetPaymentMethodQuery>();
_getUserLicenseQuery = Substitute.For<IGetUserLicenseQuery>();
_updatePaymentMethodCommand = Substitute.For<IUpdatePaymentMethodCommand>();

_sut = new AccountBillingVNextController(
_createBitPayInvoiceForCreditCommand,
_createPremiumCloudHostedSubscriptionCommand,
_getCreditQuery,
_getPaymentMethodQuery,
_getUserLicenseQuery,
_updatePaymentMethodCommand);
}

[Theory, BitAutoData]
public async Task GetLicenseAsync_ValidUser_ReturnsLicenseResponse(User user,
Core.Billing.Licenses.Models.Api.Response.LicenseResponseModel licenseResponse)
{
// Arrange
_getUserLicenseQuery.Run(user).Returns(licenseResponse);

// Act
var result = await _sut.GetLicenseAsync(user);

// Assert
var okResult = Assert.IsAssignableFrom<IResult>(result);
await _getUserLicenseQuery.Received(1).Run(user);
}

}
Loading