mirror of
https://github.com/bitwarden/server.git
synced 2025-05-22 20:11:04 -05:00
Extracted the core logic of GetDataBytes to be shared between both files
This commit is contained in:
parent
7132ae5973
commit
5e87a23d89
@ -1,7 +1,11 @@
|
|||||||
using System.Security.Claims;
|
using System.Reflection;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
using Bit.Core.AdminConsole.Entities;
|
using Bit.Core.AdminConsole.Entities;
|
||||||
using Bit.Core.Billing.Enums;
|
using Bit.Core.Billing.Enums;
|
||||||
|
using Bit.Core.Billing.Licenses.Attributes;
|
||||||
using Bit.Core.Models.Business;
|
using Bit.Core.Models.Business;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
namespace Bit.Core.Billing.Licenses.Extensions;
|
namespace Bit.Core.Billing.Licenses.Extensions;
|
||||||
|
|
||||||
@ -76,6 +80,34 @@ public static class LicenseExtensions
|
|||||||
return expirationDate;
|
return expirationDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] GetDataBytesWithAttributes(this ILicense license, bool forHash = false)
|
||||||
|
{
|
||||||
|
var props = license.GetType()
|
||||||
|
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||||
|
.Where(p =>
|
||||||
|
{
|
||||||
|
var versionAttr = p.GetCustomAttribute<LicenseVersionAttribute>();
|
||||||
|
if (versionAttr is null || versionAttr.Version > license.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}:{CoreHelpers.FormatLicenseSignatureValue(p.GetValue(license, null))}")
|
||||||
|
.Aggregate((c, n) => $"{c}|{n}");
|
||||||
|
|
||||||
|
var data = $"license:{license.LicenseType.ToString().ToLowerInvariant()}|{props}";
|
||||||
|
return Encoding.UTF8.GetBytes(data);
|
||||||
|
}
|
||||||
|
|
||||||
public static T GetValue<T>(this ClaimsPrincipal principal, string claimType)
|
public static T GetValue<T>(this ClaimsPrincipal principal, string claimType)
|
||||||
{
|
{
|
||||||
var claim = principal.FindFirst(claimType);
|
var claim = principal.FindFirst(claimType);
|
||||||
|
@ -43,7 +43,7 @@ public abstract class BaseLicense : ILicense
|
|||||||
public string Signature { get; set; }
|
public string Signature { get; set; }
|
||||||
|
|
||||||
[LicenseIgnore]
|
[LicenseIgnore]
|
||||||
public LicenseType? LicenseType { get; set; }
|
public LicenseType LicenseType { get; set; }
|
||||||
|
|
||||||
[LicenseIgnore]
|
[LicenseIgnore]
|
||||||
public string Token { get; set; }
|
public string Token { get; set; }
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
|
||||||
namespace Bit.Core.Models.Business;
|
namespace Bit.Core.Models.Business;
|
||||||
|
|
||||||
public interface ILicense
|
public interface ILicense
|
||||||
{
|
{
|
||||||
|
LicenseType LicenseType { get; set; }
|
||||||
string LicenseKey { get; set; }
|
string LicenseKey { get; set; }
|
||||||
int Version { get; set; }
|
int Version { get; set; }
|
||||||
DateTime Issued { get; set; }
|
DateTime Issued { get; set; }
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Security.Claims;
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Bit.Core.AdminConsole.Entities;
|
using Bit.Core.AdminConsole.Entities;
|
||||||
using Bit.Core.Billing.Enums;
|
using Bit.Core.Billing.Enums;
|
||||||
@ -258,30 +257,7 @@ public class OrganizationLicense : BaseLicense
|
|||||||
throw new NotSupportedException($"Version {Version} is not supported.");
|
throw new NotSupportedException($"Version {Version} is not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var props = GetType()
|
return this.GetDataBytesWithAttributes(forHash);
|
||||||
.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:organization|{props}";
|
|
||||||
return Encoding.UTF8.GetBytes(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CanUse(
|
public bool CanUse(
|
||||||
@ -397,7 +373,7 @@ public class OrganizationLicense : BaseLicense
|
|||||||
errorMessages.AppendLine("The license does not allow for on-premise hosting of organizations.");
|
errorMessages.AppendLine("The license does not allow for on-premise hosting of organizations.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LicenseType != null && LicenseType != Enums.LicenseType.Organization)
|
if (LicenseType != LicenseType.Organization)
|
||||||
{
|
{
|
||||||
errorMessages.AppendLine("Premium licenses cannot be applied to an organization. " +
|
errorMessages.AppendLine("Premium licenses cannot be applied to an organization. " +
|
||||||
"Upload this license from your personal account settings page.");
|
"Upload this license from your personal account settings page.");
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Security.Claims;
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Bit.Core.Billing.Licenses.Attributes;
|
using Bit.Core.Billing.Licenses.Attributes;
|
||||||
using Bit.Core.Billing.Licenses.Extensions;
|
using Bit.Core.Billing.Licenses.Extensions;
|
||||||
@ -71,30 +70,7 @@ public class UserLicense : BaseLicense
|
|||||||
throw new NotSupportedException($"Version {Version} is not supported.");
|
throw new NotSupportedException($"Version {Version} is not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var props = GetType()
|
return this.GetDataBytesWithAttributes(forHash);
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CanUse(User user, ClaimsPrincipal claimsPrincipal, out string exception)
|
public bool CanUse(User user, ClaimsPrincipal claimsPrincipal, out string exception)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user