diff --git a/src/Core/Models/Business/OrganizationLicense.cs b/src/Core/Models/Business/OrganizationLicense.cs index 1b9b0077ca..af6e5b30e9 100644 --- a/src/Core/Models/Business/OrganizationLicense.cs +++ b/src/Core/Models/Business/OrganizationLicense.cs @@ -1,78 +1,12 @@ using System.Text.Json.Serialization; -using Bit.Core.AdminConsole.Entities; using Bit.Core.Billing.Enums; using Bit.Core.Billing.Licenses.Attributes; -using Bit.Core.Billing.Licenses.Extensions; -using Bit.Core.Services; namespace Bit.Core.Models.Business; public class OrganizationLicense : BaseLicense { - public OrganizationLicense() - { - } - - public OrganizationLicense(Organization org, SubscriptionInfo subscriptionInfo, Guid installationId, - ILicensingService licenseService, int? version = null) - { - Version = version.GetValueOrDefault(CurrentLicenseFileVersion); // TODO: Remember to change the constant - LicenseType = Enums.LicenseType.Organization; - LicenseKey = org.LicenseKey; - InstallationId = installationId; - Id = org.Id; - Name = org.Name; - BillingEmail = org.BillingEmail; - BusinessName = org.BusinessName; - Enabled = org.Enabled; - Plan = org.Plan; - PlanType = org.PlanType; - Seats = org.Seats; - MaxCollections = org.MaxCollections; - UsePolicies = org.UsePolicies; - UseSso = org.UseSso; - UseKeyConnector = org.UseKeyConnector; - UseScim = org.UseScim; - UseGroups = org.UseGroups; - UseEvents = org.UseEvents; - UseDirectory = org.UseDirectory; - UseTotp = org.UseTotp; - Use2fa = org.Use2fa; - UseApi = org.UseApi; - UseResetPassword = org.UseResetPassword; - MaxStorageGb = org.MaxStorageGb; - SelfHost = org.SelfHost; - UsersGetPremium = org.UsersGetPremium; - UseCustomPermissions = org.UseCustomPermissions; - Issued = DateTime.UtcNow; - UsePasswordManager = org.UsePasswordManager; - UseSecretsManager = org.UseSecretsManager; - SmSeats = org.SmSeats; - SmServiceAccounts = org.SmServiceAccounts; - UseRiskInsights = org.UseRiskInsights; - UseOrganizationDomains = org.UseOrganizationDomains; - - // Deprecated. Left for backwards compatibility with old license versions. - LimitCollectionCreationDeletion = org.LimitCollectionCreation || org.LimitCollectionDeletion; - AllowAdminAccessToAllCollectionItems = org.AllowAdminAccessToAllCollectionItems; - // - - Expires = org.CalculateFreshExpirationDate(subscriptionInfo, Issued); - Refresh = org.CalculateFreshRefreshDate(subscriptionInfo, Expires, Issued); - ExpirationWithoutGracePeriod = org.CalculateFreshExpirationDateWithoutGracePeriod(subscriptionInfo); - Trial = org.IsTrialing(subscriptionInfo); - - UseAdminSponsoredFamilies = org.UseAdminSponsoredFamilies; - Hash = Convert.ToBase64String(this.ComputeHash()); - Signature = Convert.ToBase64String(licenseService.SignLicense(this)); - } - - /// - /// Represents the current version of the license format. Should be updated whenever new fields are added. - /// - /// Intentionally set one version behind to allow self hosted users some time to update before - /// getting out of date license errors - /// + [Obsolete("No longer used in the JWT based license format")] public const int CurrentLicenseFileVersion = 15; [LicenseVersion(1)] @@ -182,8 +116,4 @@ public class OrganizationLicense : BaseLicense { get => Version is >= 1 and <= CurrentLicenseFileVersion + 1; } - - - - } diff --git a/src/Core/OrganizationFeatures/OrganizationLicenses/Cloud/CloudGetOrganizationLicenseQuery.cs b/src/Core/OrganizationFeatures/OrganizationLicenses/Cloud/CloudGetOrganizationLicenseQuery.cs index 44edde1495..4ced39c171 100644 --- a/src/Core/OrganizationFeatures/OrganizationLicenses/Cloud/CloudGetOrganizationLicenseQuery.cs +++ b/src/Core/OrganizationFeatures/OrganizationLicenses/Cloud/CloudGetOrganizationLicenseQuery.cs @@ -1,5 +1,6 @@ using Bit.Core.AdminConsole.Entities; using Bit.Core.AdminConsole.Repositories; +using Bit.Core.Billing.Licenses.Extensions; using Bit.Core.Enums; using Bit.Core.Exceptions; using Bit.Core.Models.Business; @@ -15,20 +16,17 @@ 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, - IFeatureService featureService) + IProviderRepository providerRepository) { _installationRepository = installationRepository; _paymentService = paymentService; _licensingService = licensingService; _providerRepository = providerRepository; - _featureService = featureService; } public async Task GetLicenseAsync(Organization organization, Guid installationId, @@ -41,7 +39,60 @@ public class CloudGetOrganizationLicenseQuery : ICloudGetOrganizationLicenseQuer } var subscriptionInfo = await GetSubscriptionAsync(organization); - var license = new OrganizationLicense(organization, subscriptionInfo, installationId, _licensingService, version); + var issued = DateTime.UtcNow; + + var license = new OrganizationLicense + { + Version = version.GetValueOrDefault(OrganizationLicense.CurrentLicenseFileVersion), + LicenseType = LicenseType.Organization, + LicenseKey = organization.LicenseKey, + InstallationId = installationId, + Id = organization.Id, + Name = organization.Name, + BillingEmail = organization.BillingEmail, + BusinessName = organization.BusinessName, + Enabled = organization.Enabled, + Plan = organization.Plan, + PlanType = organization.PlanType, + Seats = organization.Seats, + MaxCollections = organization.MaxCollections, + UsePolicies = organization.UsePolicies, + UseSso = organization.UseSso, + UseKeyConnector = organization.UseKeyConnector, + UseScim = organization.UseScim, + UseGroups = organization.UseGroups, + UseEvents = organization.UseEvents, + UseDirectory = organization.UseDirectory, + UseTotp = organization.UseTotp, + Use2fa = organization.Use2fa, + UseApi = organization.UseApi, + UseResetPassword = organization.UseResetPassword, + MaxStorageGb = organization.MaxStorageGb, + SelfHost = organization.SelfHost, + UsersGetPremium = organization.UsersGetPremium, + UseCustomPermissions = organization.UseCustomPermissions, + Issued = issued, + UsePasswordManager = organization.UsePasswordManager, + UseSecretsManager = organization.UseSecretsManager, + SmSeats = organization.SmSeats, + SmServiceAccounts = organization.SmServiceAccounts, + UseRiskInsights = organization.UseRiskInsights, + UseOrganizationDomains = organization.UseOrganizationDomains, + + // Deprecated. Left for backwards compatibility with old license versions. + LimitCollectionCreationDeletion = organization.LimitCollectionCreation || organization.LimitCollectionDeletion, + AllowAdminAccessToAllCollectionItems = organization.AllowAdminAccessToAllCollectionItems, + + Expires = organization.CalculateFreshExpirationDate(subscriptionInfo, issued), + Refresh = organization.CalculateFreshRefreshDate(subscriptionInfo, organization.CalculateFreshExpirationDate(subscriptionInfo, issued), issued), + ExpirationWithoutGracePeriod = organization.CalculateFreshExpirationDateWithoutGracePeriod(subscriptionInfo), + Trial = organization.IsTrialing(subscriptionInfo), + UseAdminSponsoredFamilies = organization.UseAdminSponsoredFamilies + }; + + // Hash is included in Signature, and so must be initialized before signing + license.Hash = Convert.ToBase64String(license.ComputeHash()); + license.Signature = Convert.ToBase64String(_licensingService.SignLicense(license)); license.Token = await _licensingService.CreateOrganizationTokenAsync(organization, installationId, subscriptionInfo); return license;