-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-27731] Updated organization licenses to save the correct values from the token #6546
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
Changes from 8 commits
a0ae1ee
fe0b5d0
331c987
6d33da8
2dc1349
216ddcb
1977151
35a9845
90a35de
4bb7046
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| ๏ปฟusing System.Text.Json; | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Billing.Enums; | ||
| using Bit.Core.Billing.Licenses; | ||
| using Bit.Core.Billing.Licenses.Extensions; | ||
| using Bit.Core.Billing.Organizations.Models; | ||
| using Bit.Core.Billing.Services; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Exceptions; | ||
| using Bit.Core.Models.Data.Organizations; | ||
| using Bit.Core.Services; | ||
|
|
@@ -46,6 +48,57 @@ public async Task UpdateLicenseAsync(SelfHostedOrganizationDetails selfHostedOrg | |
| } | ||
|
|
||
| var claimsPrincipal = _licensingService.GetClaimsPrincipalFromLicense(license); | ||
|
|
||
| // If the license has a Token (claims-based), extract all properties from claims BEFORE validation | ||
| // This ensures that CanUseLicense validation has access to the correct values from claims | ||
| // Otherwise, fall back to using the properties already on the license object (backward compatibility) | ||
| if (claimsPrincipal != null) | ||
| { | ||
| license.Name = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.Name); | ||
| license.BillingEmail = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.BillingEmail); | ||
| license.BusinessName = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.BusinessName); | ||
| license.PlanType = claimsPrincipal.GetValue<PlanType>(OrganizationLicenseConstants.PlanType); | ||
| license.Seats = claimsPrincipal.GetValue<int?>(OrganizationLicenseConstants.Seats); | ||
| license.MaxCollections = claimsPrincipal.GetValue<short?>(OrganizationLicenseConstants.MaxCollections); | ||
| license.UsePolicies = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsePolicies); | ||
| license.UseSso = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseSso); | ||
| license.UseKeyConnector = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseKeyConnector); | ||
| license.UseScim = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseScim); | ||
| license.UseGroups = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseGroups); | ||
| license.UseDirectory = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseDirectory); | ||
| license.UseEvents = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseEvents); | ||
| license.UseTotp = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseTotp); | ||
| license.Use2fa = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.Use2fa); | ||
| license.UseApi = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseApi); | ||
| license.UseResetPassword = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseResetPassword); | ||
| license.Plan = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.Plan); | ||
| license.SelfHost = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.SelfHost); | ||
| license.UsersGetPremium = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsersGetPremium); | ||
| license.UseCustomPermissions = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseCustomPermissions); | ||
| license.Enabled = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.Enabled); | ||
| license.Expires = claimsPrincipal.GetValue<DateTime?>(OrganizationLicenseConstants.Expires); | ||
| license.LicenseKey = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.LicenseKey); | ||
| license.UsePasswordManager = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsePasswordManager); | ||
| license.UseSecretsManager = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseSecretsManager); | ||
| license.SmSeats = claimsPrincipal.GetValue<int?>(OrganizationLicenseConstants.SmSeats); | ||
| license.SmServiceAccounts = claimsPrincipal.GetValue<int?>(OrganizationLicenseConstants.SmServiceAccounts); | ||
| license.UseRiskInsights = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseRiskInsights); | ||
| license.UseOrganizationDomains = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseOrganizationDomains); | ||
| license.UseAdminSponsoredFamilies = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseAdminSponsoredFamilies); | ||
| license.UseAutomaticUserConfirmation = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseAutomaticUserConfirmation); | ||
| license.UseDisableSmAdsForUsers = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseDisableSmAdsForUsers); | ||
| license.UsePhishingBlocker = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsePhishingBlocker); | ||
| license.MaxStorageGb = claimsPrincipal.GetValue<short?>(OrganizationLicenseConstants.MaxStorageGb); | ||
| license.InstallationId = claimsPrincipal.GetValue<Guid>(OrganizationLicenseConstants.InstallationId); | ||
| license.LicenseType = claimsPrincipal.GetValue<LicenseType>(OrganizationLicenseConstants.LicenseType); | ||
| license.Issued = claimsPrincipal.GetValue<DateTime>(OrganizationLicenseConstants.Issued); | ||
| license.Refresh = claimsPrincipal.GetValue<DateTime?>(OrganizationLicenseConstants.Refresh); | ||
| license.ExpirationWithoutGracePeriod = claimsPrincipal.GetValue<DateTime?>(OrganizationLicenseConstants.ExpirationWithoutGracePeriod); | ||
| license.Trial = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.Trial); | ||
| license.LimitCollectionCreationDeletion = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.LimitCollectionCreationDeletion); | ||
| license.AllowAdminAccessToAllCollectionItems = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.AllowAdminAccessToAllCollectionItems); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code extracts 29 properties, but two constants appear to be missing from OrganizationLicenseConstants:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, it wasn't. Those two seem to be new additions from when I opened this PR. I'll get them added, good catch
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch @cyprain-okeke , I just pushed a commit addressing this. I also added a set of unit tests to guide future developers through the process of adding a property to the organization license |
||
|
|
||
| var canUse = license.CanUse(_globalSettings, _licensingService, claimsPrincipal, out var exception) && | ||
| selfHostedOrganization.CanUseLicense(license, out exception); | ||
|
|
||
|
|
@@ -54,12 +107,6 @@ public async Task UpdateLicenseAsync(SelfHostedOrganizationDetails selfHostedOrg | |
| throw new BadRequestException(exception); | ||
| } | ||
|
|
||
| var useAutomaticUserConfirmation = claimsPrincipal? | ||
| .GetValue<bool>(OrganizationLicenseConstants.UseAutomaticUserConfirmation) ?? false; | ||
|
|
||
| selfHostedOrganization.UseAutomaticUserConfirmation = useAutomaticUserConfirmation; | ||
| license.UseAutomaticUserConfirmation = useAutomaticUserConfirmation; | ||
|
|
||
| await WriteLicenseFileAsync(selfHostedOrganization, license); | ||
| await UpdateOrganizationAsync(selfHostedOrganization, license); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System.Reflection; | ||
| using Bit.Core.Billing.Licenses; | ||
| using Bit.Core.Billing.Organizations.Models; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Core.Test.Billing.Licenses; | ||
|
|
||
| public class LicenseConstantsTests | ||
| { | ||
| [Fact] | ||
| public void OrganizationLicenseConstants_HasConstantForEveryLicenseProperty() | ||
| { | ||
| // This test ensures that when a new property is added to OrganizationLicense, | ||
| // a corresponding constant is added to OrganizationLicenseConstants. | ||
| // This is the first step in the license synchronization pipeline: | ||
| // Property โ Constant โ Claim โ Extraction โ Application | ||
|
|
||
| // 1. Get all public properties from OrganizationLicense | ||
| var licenseProperties = typeof(OrganizationLicense) | ||
| .GetProperties(BindingFlags.Public | BindingFlags.Instance) | ||
| .Select(p => p.Name) | ||
| .ToHashSet(); | ||
|
|
||
| // 2. Get all constants from OrganizationLicenseConstants | ||
| var constants = typeof(OrganizationLicenseConstants) | ||
| .GetFields(BindingFlags.Public | BindingFlags.Static) | ||
| .Where(f => f.IsLiteral && !f.IsInitOnly) | ||
| .Select(f => f.GetValue(null) as string) | ||
| .ToHashSet(); | ||
|
|
||
| // 3. Define properties that don't need constants (internal/computed/non-claims properties) | ||
| var excludedProperties = new HashSet<string> | ||
| { | ||
| "SignatureBytes", // Computed from Signature property | ||
| "ValidLicenseVersion", // Internal property, not serialized | ||
| "CurrentLicenseFileVersion", // Constant field, not an instance property | ||
| "Hash", // Signature-related, not in claims system | ||
| "Signature", // Signature-related, not in claims system | ||
| "Token", // The JWT itself, not a claim within the token | ||
| "Version" // Not in claims system (only in deprecated property-based licenses) | ||
| }; | ||
|
|
||
| // 4. Find license properties without corresponding constants | ||
| var propertiesWithoutConstants = licenseProperties | ||
| .Except(constants) | ||
| .Except(excludedProperties) | ||
| .OrderBy(p => p) | ||
| .ToList(); | ||
|
|
||
| // 5. Build error message with guidance | ||
| var errorMessage = ""; | ||
| if (propertiesWithoutConstants.Any()) | ||
| { | ||
| errorMessage = $"The following OrganizationLicense properties don't have constants in OrganizationLicenseConstants:\n"; | ||
| errorMessage += string.Join("\n", propertiesWithoutConstants.Select(p => $" - {p}")); | ||
| errorMessage += "\n\nPlease add the following constants to OrganizationLicenseConstants:\n"; | ||
| foreach (var prop in propertiesWithoutConstants) | ||
| { | ||
| errorMessage += $" public const string {prop} = nameof({prop});\n"; | ||
| } | ||
| } | ||
|
|
||
| // 6. Assert - if this fails, the error message guides the developer to add the constant | ||
| Assert.True( | ||
| !propertiesWithoutConstants.Any(), | ||
| $"\n{errorMessage}"); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.