Skip to content

Commit b586e21

Browse files
[PM-17177] Added additional validation to ensure license claim values aren't null (#5280)
* Added additional validation to ensure license claim values aren't null * Added extra not null validation for any property with a type that can possibly be null (cherry picked from commit 677265b)
1 parent 5461ae6 commit b586e21

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

src/Core/Billing/Licenses/Services/Implementations/OrganizationLicenseClaimsFactory.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,9 @@ public Task<List<Claim>> GenerateClaims(Organization entity, LicenseContext lice
2222
var claims = new List<Claim>
2323
{
2424
new(nameof(OrganizationLicenseConstants.LicenseType), LicenseType.Organization.ToString()),
25-
new Claim(nameof(OrganizationLicenseConstants.LicenseKey), entity.LicenseKey),
26-
new(nameof(OrganizationLicenseConstants.InstallationId), licenseContext.InstallationId.ToString()),
2725
new(nameof(OrganizationLicenseConstants.Id), entity.Id.ToString()),
28-
new(nameof(OrganizationLicenseConstants.Name), entity.Name),
29-
new(nameof(OrganizationLicenseConstants.BillingEmail), entity.BillingEmail),
3026
new(nameof(OrganizationLicenseConstants.Enabled), entity.Enabled.ToString()),
31-
new(nameof(OrganizationLicenseConstants.Plan), entity.Plan),
3227
new(nameof(OrganizationLicenseConstants.PlanType), entity.PlanType.ToString()),
33-
new(nameof(OrganizationLicenseConstants.Seats), entity.Seats.ToString()),
34-
new(nameof(OrganizationLicenseConstants.MaxCollections), entity.MaxCollections.ToString()),
3528
new(nameof(OrganizationLicenseConstants.UsePolicies), entity.UsePolicies.ToString()),
3629
new(nameof(OrganizationLicenseConstants.UseSso), entity.UseSso.ToString()),
3730
new(nameof(OrganizationLicenseConstants.UseKeyConnector), entity.UseKeyConnector.ToString()),
@@ -43,32 +36,79 @@ public Task<List<Claim>> GenerateClaims(Organization entity, LicenseContext lice
4336
new(nameof(OrganizationLicenseConstants.Use2fa), entity.Use2fa.ToString()),
4437
new(nameof(OrganizationLicenseConstants.UseApi), entity.UseApi.ToString()),
4538
new(nameof(OrganizationLicenseConstants.UseResetPassword), entity.UseResetPassword.ToString()),
46-
new(nameof(OrganizationLicenseConstants.MaxStorageGb), entity.MaxStorageGb.ToString()),
4739
new(nameof(OrganizationLicenseConstants.SelfHost), entity.SelfHost.ToString()),
4840
new(nameof(OrganizationLicenseConstants.UsersGetPremium), entity.UsersGetPremium.ToString()),
4941
new(nameof(OrganizationLicenseConstants.UseCustomPermissions), entity.UseCustomPermissions.ToString()),
50-
new(nameof(OrganizationLicenseConstants.Issued), DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)),
5142
new(nameof(OrganizationLicenseConstants.UsePasswordManager), entity.UsePasswordManager.ToString()),
5243
new(nameof(OrganizationLicenseConstants.UseSecretsManager), entity.UseSecretsManager.ToString()),
53-
new(nameof(OrganizationLicenseConstants.SmSeats), entity.SmSeats.ToString()),
54-
new(nameof(OrganizationLicenseConstants.SmServiceAccounts), entity.SmServiceAccounts.ToString()),
5544
// LimitCollectionCreationDeletion was split and removed from the
5645
// license. Left here with an assignment from the new values for
5746
// backwards compatibility.
5847
new(nameof(OrganizationLicenseConstants.LimitCollectionCreationDeletion),
5948
(entity.LimitCollectionCreation || entity.LimitCollectionDeletion).ToString()),
6049
new(nameof(OrganizationLicenseConstants.AllowAdminAccessToAllCollectionItems), entity.AllowAdminAccessToAllCollectionItems.ToString()),
50+
new(nameof(OrganizationLicenseConstants.Issued), DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)),
6151
new(nameof(OrganizationLicenseConstants.Expires), expires.ToString(CultureInfo.InvariantCulture)),
6252
new(nameof(OrganizationLicenseConstants.Refresh), refresh.ToString(CultureInfo.InvariantCulture)),
6353
new(nameof(OrganizationLicenseConstants.ExpirationWithoutGracePeriod), expirationWithoutGracePeriod.ToString(CultureInfo.InvariantCulture)),
6454
new(nameof(OrganizationLicenseConstants.Trial), trial.ToString()),
6555
};
6656

57+
if (entity.Name is not null)
58+
{
59+
claims.Add(new(nameof(OrganizationLicenseConstants.Name), entity.Name));
60+
}
61+
62+
if (entity.BillingEmail is not null)
63+
{
64+
claims.Add(new(nameof(OrganizationLicenseConstants.BillingEmail), entity.BillingEmail));
65+
}
66+
67+
if (entity.Plan is not null)
68+
{
69+
claims.Add(new(nameof(OrganizationLicenseConstants.Plan), entity.Plan));
70+
}
71+
6772
if (entity.BusinessName is not null)
6873
{
6974
claims.Add(new Claim(nameof(OrganizationLicenseConstants.BusinessName), entity.BusinessName));
7075
}
7176

77+
if (entity.LicenseKey is not null)
78+
{
79+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.LicenseKey), entity.LicenseKey));
80+
}
81+
82+
if (licenseContext.InstallationId.HasValue)
83+
{
84+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.InstallationId), licenseContext.InstallationId.ToString()));
85+
}
86+
87+
if (entity.Seats.HasValue)
88+
{
89+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.Seats), entity.Seats.ToString()));
90+
}
91+
92+
if (entity.MaxCollections.HasValue)
93+
{
94+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.MaxCollections), entity.MaxCollections.ToString()));
95+
}
96+
97+
if (entity.MaxStorageGb.HasValue)
98+
{
99+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.MaxStorageGb), entity.MaxStorageGb.ToString()));
100+
}
101+
102+
if (entity.SmSeats.HasValue)
103+
{
104+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.SmSeats), entity.SmSeats.ToString()));
105+
}
106+
107+
if (entity.SmServiceAccounts.HasValue)
108+
{
109+
claims.Add(new Claim(nameof(OrganizationLicenseConstants.SmServiceAccounts), entity.SmServiceAccounts.ToString()));
110+
}
111+
72112
return Task.FromResult(claims);
73113
}
74114

src/Core/Billing/Licenses/Services/Implementations/UserLicenseClaimsFactory.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,39 @@ public Task<List<Claim>> GenerateClaims(User entity, LicenseContext licenseConte
2121
{
2222
new(nameof(UserLicenseConstants.LicenseType), LicenseType.User.ToString()),
2323
new(nameof(UserLicenseConstants.Id), entity.Id.ToString()),
24-
new(nameof(UserLicenseConstants.Name), entity.Name),
25-
new(nameof(UserLicenseConstants.Email), entity.Email),
2624
new(nameof(UserLicenseConstants.Premium), entity.Premium.ToString()),
2725
new(nameof(UserLicenseConstants.Issued), DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)),
2826
new(nameof(UserLicenseConstants.Trial), trial.ToString()),
2927
};
3028

29+
if (entity.Email is not null)
30+
{
31+
claims.Add(new(nameof(UserLicenseConstants.Email), entity.Email));
32+
}
33+
34+
if (entity.Name is not null)
35+
{
36+
claims.Add(new(nameof(UserLicenseConstants.Name), entity.Name));
37+
}
38+
3139
if (entity.LicenseKey is not null)
3240
{
3341
claims.Add(new(nameof(UserLicenseConstants.LicenseKey), entity.LicenseKey));
3442
}
3543

36-
if (entity.MaxStorageGb is not null)
44+
if (entity.MaxStorageGb.HasValue)
3745
{
3846
claims.Add(new(nameof(UserLicenseConstants.MaxStorageGb), entity.MaxStorageGb.ToString()));
3947
}
4048

41-
if (expires is not null)
49+
if (expires.HasValue)
4250
{
43-
claims.Add(new(nameof(UserLicenseConstants.Expires), expires.ToString()));
51+
claims.Add(new(nameof(UserLicenseConstants.Expires), expires.Value.ToString(CultureInfo.InvariantCulture)));
4452
}
4553

46-
if (refresh is not null)
54+
if (refresh.HasValue)
4755
{
48-
claims.Add(new(nameof(UserLicenseConstants.Refresh), refresh.ToString()));
56+
claims.Add(new(nameof(UserLicenseConstants.Refresh), refresh.Value.ToString(CultureInfo.InvariantCulture)));
4957
}
5058

5159
return Task.FromResult(claims);

0 commit comments

Comments
 (0)