Skip to content

Commit

Permalink
[PM-16700] Handling nulls in UserLicenseClaimsFactory (#5217)
Browse files Browse the repository at this point in the history
* Handling nulls in UserLicenseClaimsFactory

* Only setting Token if the flag is enabled

(cherry picked from commit f74b94b)
  • Loading branch information
cturnbull-bitwarden committed Jan 3, 2025
1 parent 7c0a499 commit 101b919
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,42 @@ public Task<List<Claim>> GenerateClaims(User entity, LicenseContext licenseConte
{
var subscriptionInfo = licenseContext.SubscriptionInfo;

var expires = subscriptionInfo.UpcomingInvoice?.Date?.AddDays(7) ?? entity.PremiumExpirationDate?.AddDays(7);
var refresh = subscriptionInfo.UpcomingInvoice?.Date ?? entity.PremiumExpirationDate;
var trial = (subscriptionInfo.Subscription?.TrialEndDate.HasValue ?? false) &&
var expires = subscriptionInfo?.UpcomingInvoice?.Date?.AddDays(7) ?? entity.PremiumExpirationDate?.AddDays(7);
var refresh = subscriptionInfo?.UpcomingInvoice?.Date ?? entity.PremiumExpirationDate;
var trial = (subscriptionInfo?.Subscription?.TrialEndDate.HasValue ?? false) &&
subscriptionInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow;

var claims = new List<Claim>
{
new(nameof(UserLicenseConstants.LicenseType), LicenseType.User.ToString()),
new(nameof(UserLicenseConstants.LicenseKey), entity.LicenseKey),
new(nameof(UserLicenseConstants.Id), entity.Id.ToString()),
new(nameof(UserLicenseConstants.Name), entity.Name),
new(nameof(UserLicenseConstants.Email), entity.Email),
new(nameof(UserLicenseConstants.Premium), entity.Premium.ToString()),
new(nameof(UserLicenseConstants.MaxStorageGb), entity.MaxStorageGb.ToString()),
new(nameof(UserLicenseConstants.Issued), DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)),
new(nameof(UserLicenseConstants.Expires), expires.ToString()),
new(nameof(UserLicenseConstants.Refresh), refresh.ToString()),
new(nameof(UserLicenseConstants.Trial), trial.ToString()),
};

if (entity.LicenseKey is not null)
{
claims.Add(new(nameof(UserLicenseConstants.LicenseKey), entity.LicenseKey));
}

if (entity.MaxStorageGb is not null)
{
claims.Add(new(nameof(UserLicenseConstants.MaxStorageGb), entity.MaxStorageGb.ToString()));
}

if (expires is not null)
{
claims.Add(new(nameof(UserLicenseConstants.Expires), expires.ToString()));
}

if (refresh is not null)
{
claims.Add(new(nameof(UserLicenseConstants.Refresh), refresh.ToString()));
}

return Task.FromResult(claims);
}
}
3 changes: 3 additions & 0 deletions src/Core/Services/Implementations/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,10 @@ public async Task<UserLicense> GenerateLicenseAsync(
? new UserLicense(user, _licenseService)
: new UserLicense(user, subscriptionInfo, _licenseService);

if (_featureService.IsEnabled(FeatureFlagKeys.SelfHostLicenseRefactor))
{
userLicense.Token = await _licenseService.CreateUserTokenAsync(user, subscriptionInfo);
}

return userLicense;
}
Expand Down

0 comments on commit 101b919

Please sign in to comment.