1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

more premium licensing

This commit is contained in:
Kyle Spearrin
2017-08-11 22:55:25 -04:00
parent 73029f76d2
commit 9c254a7325
12 changed files with 126 additions and 59 deletions

View File

@ -2,24 +2,25 @@
using System.Linq;
using System.Collections.Generic;
using Bit.Core.Models.Business;
using Stripe;
using Bit.Core.Models.Table;
using Bit.Core.Enums;
using Bit.Core.Services;
namespace Bit.Core.Models.Api
{
public class BillingResponseModel : ResponseModel
{
public BillingResponseModel(IStorable storable, BillingInfo billing)
public BillingResponseModel(User user, BillingInfo billing, ILicensingService licenseService)
: base("billing")
{
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null;
Charges = billing.Charges.Select(c => new BillingCharge(c));
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoice(billing.UpcomingInvoice) : null;
StorageName = storable.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(storable.Storage.Value) : null;
StorageGb = storable.Storage.HasValue ? Math.Round(storable.Storage.Value / 1073741824D, 2) : 0; // 1 GB
MaxStorageGb = storable.MaxStorageGb;
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
MaxStorageGb = user.MaxStorageGb;
License = new UserLicense(user, billing, licenseService);
}
public string StorageName { get; set; }
@ -29,6 +30,7 @@ namespace Bit.Core.Models.Api
public BillingSubscription Subscription { get; set; }
public BillingInvoice UpcomingInvoice { get; set; }
public IEnumerable<BillingCharge> Charges { get; set; }
public UserLicense License { get; set; }
}
public class BillingSource

View File

@ -8,10 +8,11 @@ namespace Bit.Core.Models.Business
string LicenseKey { get; set; }
int Version { get; set; }
DateTime Issued { get; set; }
DateTime Expires { get; set; }
DateTime? Expires { get; set; }
bool Trial { get; set; }
string Signature { get; set; }
byte[] GetSignatureData();
bool VerifySignature(X509Certificate2 certificate);
byte[] Sign(X509Certificate2 certificate);
}
}

View File

@ -43,7 +43,7 @@ namespace Bit.Core.Models.Business
public bool SelfHost { get; set; }
public int Version { get; set; }
public DateTime Issued { get; set; }
public DateTime Expires { get; set; }
public DateTime? Expires { get; set; }
public bool Trial { get; set; }
public string Signature { get; set; }
public byte[] SignatureBytes => Convert.FromBase64String(Signature);
@ -55,8 +55,8 @@ namespace Bit.Core.Models.Business
{
data = string.Format("organization:{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_{9}_{10}_{11}_{12}_{13}",
Version,
Utilities.CoreHelpers.ToEpocMilliseconds(Issued),
Utilities.CoreHelpers.ToEpocMilliseconds(Expires),
Utilities.CoreHelpers.ToEpocSeconds(Issued),
Expires.HasValue ? Utilities.CoreHelpers.ToEpocSeconds(Expires.Value).ToString() : null,
LicenseKey,
Id,
Enabled,
@ -76,7 +76,7 @@ namespace Bit.Core.Models.Business
return Encoding.UTF8.GetBytes(data);
}
public bool VerifyData(Organization organization)
{
if(Issued > DateTime.UtcNow)
@ -115,5 +115,10 @@ namespace Bit.Core.Models.Business
return rsa.VerifyData(GetSignatureData(), SignatureBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
public byte[] Sign(X509Certificate2 certificate)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,4 +1,6 @@
using Bit.Core.Models.Table;
using Bit.Core.Services;
using Newtonsoft.Json;
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
@ -11,12 +13,19 @@ namespace Bit.Core.Models.Business
public UserLicense()
{ }
public UserLicense(User user)
public UserLicense(User user, BillingInfo billingInfo, ILicensingService licenseService)
{
LicenseKey = "";
LicenseKey = user.LicenseKey;
Id = user.Id;
Email = user.Email;
Version = 1;
Premium = user.Premium;
MaxStorageGb = user.MaxStorageGb;
Issued = DateTime.UtcNow;
Expires = billingInfo?.UpcomingInvoice?.Date;
Trial = (billingInfo?.Subscription?.TrialEndDate.HasValue ?? false) &&
billingInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow;
Signature = Convert.ToBase64String(licenseService.SignLicense(this));
}
public string LicenseKey { get; set; }
@ -26,9 +35,10 @@ namespace Bit.Core.Models.Business
public short? MaxStorageGb { get; set; }
public int Version { get; set; }
public DateTime Issued { get; set; }
public DateTime Expires { get; set; }
public DateTime? Expires { get; set; }
public bool Trial { get; set; }
public string Signature { get; set; }
[JsonIgnore]
public byte[] SignatureBytes => Convert.FromBase64String(Signature);
public byte[] GetSignatureData()
@ -36,11 +46,12 @@ namespace Bit.Core.Models.Business
string data = null;
if(Version == 1)
{
data = string.Format("user:{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}",
data = string.Format("user:{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}",
Version,
Utilities.CoreHelpers.ToEpocMilliseconds(Issued),
Utilities.CoreHelpers.ToEpocMilliseconds(Expires),
Utilities.CoreHelpers.ToEpocSeconds(Issued),
Expires.HasValue ? Utilities.CoreHelpers.ToEpocSeconds(Expires.Value).ToString() : null,
LicenseKey,
Trial,
Id,
Email,
Premium,
@ -86,5 +97,18 @@ namespace Bit.Core.Models.Business
return rsa.VerifyData(GetSignatureData(), SignatureBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
public byte[] Sign(X509Certificate2 certificate)
{
if(!certificate.HasPrivateKey)
{
throw new InvalidOperationException("You don't have the private key!");
}
using(var rsa = certificate.GetRSAPrivateKey())
{
return rsa.SignData(GetSignatureData(), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
}
}