1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-06 18:42:49 -05:00

Updated GetDataBytes to use the new attributes instead of hardcoding the version logic

This commit is contained in:
Conner Turnbull
2025-05-20 15:06:43 -04:00
parent 3c40646d9f
commit f4031fc7f0
2 changed files with 48 additions and 85 deletions

View File

@ -66,34 +66,34 @@ public class UserLicense : BaseLicense
public override byte[] GetDataBytes(bool forHash = false)
{
string data = null;
if (Version == 1)
{
var props = typeof(UserLicense)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p =>
!p.Name.Equals(nameof(Signature)) &&
!p.Name.Equals(nameof(SignatureBytes)) &&
!p.Name.Equals(nameof(LicenseType)) &&
!p.Name.Equals(nameof(Token)) &&
(
!forHash ||
(
!p.Name.Equals(nameof(Hash)) &&
!p.Name.Equals(nameof(Issued)) &&
!p.Name.Equals(nameof(Refresh))
)
))
.OrderBy(p => p.Name)
.Select(p => $"{p.Name}:{Utilities.CoreHelpers.FormatLicenseSignatureValue(p.GetValue(this, null))}")
.Aggregate((c, n) => $"{c}|{n}");
data = $"license:user|{props}";
}
else
if (Version != 1)
{
throw new NotSupportedException($"Version {Version} is not supported.");
}
var props = GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p =>
{
var versionAttr = p.GetCustomAttribute<LicenseVersionAttribute>();
if (versionAttr is null || versionAttr.Version > Version)
{
return false;
}
var ignoreAttr = p.GetCustomAttribute<LicenseIgnoreAttribute>();
if (ignoreAttr is null)
{
return true;
}
return forHash && ignoreAttr.IncludeInHash;
})
.OrderBy(p => p.Name)
.Select(p => $"{p.Name}:{Utilities.CoreHelpers.FormatLicenseSignatureValue(p.GetValue(this, null))}")
.Aggregate((c, n) => $"{c}|{n}");
var data = $"license:user|{props}";
return Encoding.UTF8.GetBytes(data);
}