This commit is contained in:
2023-01-09 01:43:08 +00:00
parent 47cb4c0045
commit 146d17a865
3 changed files with 336 additions and 342 deletions

View File

@@ -240,7 +240,7 @@ namespace Sockeye.Biz
public static string GenFetchCode() private static string GenFetchCode()
{ {
//sufficient for this purpose //sufficient for this purpose
@@ -304,7 +304,7 @@ namespace Sockeye.Biz
/// This is called by both regular and trial license key routes /// This is called by both regular and trial license key routes
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private static void genKey(License l) internal static void GenerateAndSetV7Key(License l)
{ {
StringBuilder sbKey = new StringBuilder(); StringBuilder sbKey = new StringBuilder();

View File

@@ -493,18 +493,14 @@ namespace Sockeye.Biz
switch(l.PGroup){ switch(l.PGroup){
case ProductGroup.RavenPerpetual: case ProductGroup.RavenPerpetual:
{
}
break;
case ProductGroup.RavenSubscription: case ProductGroup.RavenSubscription:
{ {
RavenKeyFactory.GenerateAndSetRavenKey(l);
} }
break; break;
case ProductGroup.AyaNova7: case ProductGroup.AyaNova7:
{ {
KeyFactory.GenerateAndSetV7Key(l);
} }
break; break;
} }

View File

@@ -6,6 +6,7 @@ using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.OpenSsl; using Org.BouncyCastle.OpenSsl;
using Sockeye.Util; using Sockeye.Util;
using Sockeye.Models;
@@ -41,270 +42,23 @@ namespace Sockeye.Biz
//This feature name means it's a trial key //This feature name means it's a trial key
private const string TRIAL_FEATURE_NAME = "TrialMode"; private const string TRIAL_FEATURE_NAME = "TrialMode";
#region license classes
//DTO object returned on license query
public class LicenseFeature
{
//name of feature / product
public string Feature { get; set; }
//Optional count for items that require it
public long Count { get; set; }
}
//DTO object for parsed key
internal class AyaNovaLicenseKey
{
public AyaNovaLicenseKey()
{
Features = new List<LicenseFeature>();
RegisteredTo = UNLICENSED_TOKEN;
//Id = RegisteredTo;
LicenseFormat = "8";
var vv = Math.Truncate((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
string sId = vv.ToString();
if (sId.Contains(","))
sId = sId.Split('.')[0];
Id = sId;
}
public override string ToString()
{
System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine($"Registered to: {RegisteredTo}");
sb.AppendLine($"Database id: {DbId}");
sb.AppendLine($"Type: {(Perpetual ? "Perpetual" : "Subscription")}");
if (WillExpire)
sb.AppendLine($"Available for use until: {LicenseExpiration.ToLongDateString()}");
if (Perpetual)
sb.AppendLine($"Support and updates available until: {MaintenanceExpiration.ToLongDateString()}");
foreach (LicenseFeature f in Features)
{
if (f.Feature == TRIAL_FEATURE_NAME)
{
sb.AppendLine("Temporary license for evaluation");
continue;
}
//default for items added later not tokenized
if (f.Count > 0)
sb.AppendLine($"{f.Feature}: {f.Count}");
else
sb.AppendLine($"{f.Feature}");
}
return sb.ToString();
}
public bool IsEmpty
{
get
{
//Key is empty if it's not registered to anyone or there are no features in it
return string.IsNullOrWhiteSpace(RegisteredTo) || (Features == null || Features.Count == 0);
}
}
/// <summary>
/// Fetch the license status of the feature in question
/// </summary>
/// <param name="Feature"></param>
/// <returns>LicenseFeature object or null if there is no license</returns>
public LicenseFeature GetLicenseFeature(string Feature)
{
if (IsEmpty)
return null;
string lFeature = Feature.ToLowerInvariant();
foreach (LicenseFeature l in Features)
{
if (l.Feature.ToLowerInvariant() == lFeature)
{
return l;
}
}
return null;
}
/// <summary>
/// Check for the existance of license feature
/// </summary>
/// <param name="Feature"></param>
/// <returns>bool</returns>
public bool HasLicenseFeature(string Feature)
{
if (IsEmpty)
return false;
string lFeature = Feature.ToLowerInvariant();
foreach (LicenseFeature l in Features)
{
if (l.Feature.ToLowerInvariant() == lFeature)
{
return true;
}
}
return false;
}
public bool WillExpire
{
get
{
return LicenseExpiration < DateUtil.EmptyDateValue;
}
}
public bool LicenseExpired
{
get
{
return LicenseExpiration > DateTime.Now;
}
}
public bool MaintenanceExpired
{
get
{
return MaintenanceExpiration > DateTime.Now;
}
}
public bool TrialLicense
{
get
{
return HasLicenseFeature(TRIAL_FEATURE_NAME);
}
}
public string LicenseFormat { get; set; }
public string Id { get; set; }
public string RegisteredTo { get; set; }
public string DbId { get; set; }
public bool Perpetual { get; set; }
public DateTime LicenseExpiration { get; set; }
public DateTime MaintenanceExpiration { get; set; }
public List<LicenseFeature> Features { get; set; }
}
#endregion
public static string GetRavenTrialKey(string dbid, string CompanyName, bool Perpetual)
{
//Build a sample test key, sign it and return it
AyaNovaLicenseKey k = new AyaNovaLicenseKey();
k.LicenseFormat = "8";
k.RegisteredTo = CompanyName;
k.DbId = dbid;
k.Perpetual = Perpetual;
//flag as trial key not regular key
k.Features.Add(new LicenseFeature() { Feature = TRIAL_FEATURE_NAME, Count = 0 });
if (Perpetual)
{
//trial period time limit
//Normal code, uncomment this when done testing expiration dates of perpetual
k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);
//Testing code for 3 minute long evaluation license in case need to test with a key that expires
// #warning FYI ROCKFISH is SET TO GENERATE A VERY SHORT TEST KEY
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddMinutes(3); //TEST VALUE FOR DIAGNOSING LICENSE EXPIRATION ISSUES
//5k inside staff users will cover huge seeding level easily
k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
}
else
{
//SUBSCRIPTION
//trial period time limit
k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);//NOTE: this preserves the current time, should it be set to midnight or something?
//20k customer contacts will cover huge seeding level easily
//5k inside staff users will cover huge seeding level easily
k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
k.Features.Add(new LicenseFeature() { Feature = ACTIVE_CUSTOMER_USERS_FEATURE_NAME, Count = 20000 });
k.Features.Add(new LicenseFeature() { Feature = MAXIMUM_DATA_GB_FEATURE_NAME, Count = 20 });
}
return GenerateRavenKey(k);
}
// //TESTING ONLY this is for development purposes only
// //No external usage
// public static string GetRavenTestKey(string dbid, bool Perpetual)
// {
// //Build a sample test key, sign it and return it
// AyaNovaLicenseKey k = new AyaNovaLicenseKey();
// k.LicenseFormat = "8";
// k.RegisteredTo = "GZ TestCo Inc.";
// k.DbId = dbid;
// k.Perpetual = Perpetual;
// if (Perpetual)
// {
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);
// //5k inside staff users will cover huge seeding level easily
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
// }
// else
// {
// //SUBSCRIPTION
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);
// //20k customer contacts will cover huge seeding level easily
// //5k inside staff users will cover huge seeding level easily
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_CUSTOMER_USERS_FEATURE_NAME, Count = 20000 });
// k.Features.Add(new LicenseFeature() { Feature = MAXIMUM_DATA_GB_FEATURE_NAME, Count = 20 });
// }
// k.Features.Add(new LicenseFeature() { Feature = TRIAL_FEATURE_NAME, Count = 0 });
// return GenerateRavenKey(k);
// }
/// <summary> /// <summary>
/// RAVEN key generator /// RAVEN key generator
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal static string GenerateRavenKey(AyaNovaLicenseKey k) internal static void GenerateAndSetRavenKey(License l)
{ {
if (string.IsNullOrWhiteSpace(l.RegTo))
if (string.IsNullOrWhiteSpace(k.RegisteredTo))
throw new ArgumentException("RegisteredTo is required", "RegisteredTo"); throw new ArgumentException("RegisteredTo is required", "RegisteredTo");
// if (k.DbId == Guid.Empty) if (string.IsNullOrWhiteSpace(l.DbId))
if (string.IsNullOrWhiteSpace(k.DbId)) throw new ArgumentException("DBId is required", "DbId");
throw new ArgumentException("DBId is required", "RegisteredTo");
try //license serial number
{ var vv = Math.Truncate((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
string sId = vv.ToString();
if (sId.Contains(","))
sId = sId.Split('.')[0];
StringBuilder sbKey = new StringBuilder(); StringBuilder sbKey = new StringBuilder();
StringWriter sw = new StringWriter(sbKey); StringWriter sw = new StringWriter(sbKey);
@@ -321,60 +75,70 @@ namespace Sockeye.Biz
w.WriteStartObject();//start of key object w.WriteStartObject();//start of key object
w.WritePropertyName("LicenseFormat"); w.WritePropertyName("LicenseFormat");
w.WriteValue(k.LicenseFormat); w.WriteValue("8");
w.WritePropertyName("Id"); w.WritePropertyName("Id");
w.WriteValue(k.Id); w.WriteValue(sId);
w.WritePropertyName("RegisteredTo"); w.WritePropertyName("RegisteredTo");
w.WriteValue(k.RegisteredTo); w.WriteValue(l.RegTo);
w.WritePropertyName("DBID"); w.WritePropertyName("DBID");
w.WriteValue(k.DbId); w.WriteValue(l.DbId);
w.WritePropertyName("Perpetual"); w.WritePropertyName("Perpetual");
w.WriteValue(k.Perpetual); w.WriteValue(l.PGroup == ProductGroup.RavenPerpetual);
w.WritePropertyName("LicenseExpiration"); w.WritePropertyName("LicenseExpiration");
w.WriteValue(k.LicenseExpiration); w.WriteValue(l.LicenseExpire);
w.WritePropertyName("MaintenanceExpiration"); w.WritePropertyName("MaintenanceExpiration");
w.WriteValue(k.MaintenanceExpiration); w.WriteValue(l.MaintenanceExpire);
//FEATURES //FEATURES
// w.WritePropertyName("Features");
// w.WriteStartObject();
w.WritePropertyName("Features"); w.WritePropertyName("Features");
w.WriteStartArray(); w.WriteStartArray();
foreach (LicenseFeature lf in k.Features) if (l.TrialMode)
{ {
w.WriteStartObject(); w.WriteStartObject();
w.WritePropertyName("Name"); w.WritePropertyName("Name");
w.WriteValue(lf.Feature); w.WriteValue(TRIAL_FEATURE_NAME);
w.WriteEndObject();
if (lf.Count > 0)
{
w.WritePropertyName("Count");
w.WriteValue(lf.Count);
} }
//USERS
w.WriteStartObject();
w.WritePropertyName("Name");
w.WriteValue(ACTIVE_INTERNAL_USERS_FEATURE_NAME);
w.WritePropertyName("Count");
w.WriteValue(l.Users);
w.WriteEndObject();
if (l.PGroup == ProductGroup.RavenSubscription)
{
//CUSTOMER USERS
w.WriteStartObject();
w.WritePropertyName("Name");
w.WriteValue(ACTIVE_CUSTOMER_USERS_FEATURE_NAME);
w.WritePropertyName("Count");
w.WriteValue(l.CustomerUsers);
w.WriteEndObject();
//MAX DATA GB
w.WriteStartObject();
w.WritePropertyName("Name");
w.WriteValue(MAXIMUM_DATA_GB_FEATURE_NAME);
w.WritePropertyName("Count");
w.WriteValue(l.MaxDataGB);
w.WriteEndObject(); w.WriteEndObject();
} }
//end of features array //end of features array
w.WriteEnd(); w.WriteEnd();
//end of features object //End of "Key" property
// w.WriteEndObject();
//end of AyaNova/AyaNovaLite key object
w.WriteEndObject(); w.WriteEndObject();
//close outer 'wrapper' object brace } //close outer 'wrapper' object brace }
@@ -429,7 +193,6 @@ oArP0E2Vbow3JMxq/oeXmHbrLMLQfYyXwFmciLFigOtkd45bfHdrbA==
var sign = signer.GenerateSignature(); var sign = signer.GenerateSignature();
var signature = Convert.ToBase64String(sign); var signature = Convert.ToBase64String(sign);
System.Text.StringBuilder sbOut = new StringBuilder(); System.Text.StringBuilder sbOut = new StringBuilder();
sbOut.AppendLine("[KEY"); sbOut.AppendLine("[KEY");
sbOut.AppendLine(sbKey.ToString()); sbOut.AppendLine(sbKey.ToString());
@@ -438,21 +201,256 @@ oArP0E2Vbow3JMxq/oeXmHbrLMLQfYyXwFmciLFigOtkd45bfHdrbA==
sbOut.AppendLine(signature); sbOut.AppendLine(signature);
sbOut.AppendLine("SIGNATURE]"); sbOut.AppendLine("SIGNATURE]");
l.Key = sbOut.ToString();
return sbOut.ToString();
} }
catch (Exception ex)
{
return (ex.Message); #region old stuff
} #region license classes
}
// //DTO object returned on license query
// public class LicenseFeature
// {
// //name of feature / product
// public string Feature { get; set; }
// //Optional count for items that require it
// public long Count { get; set; }
// }
// //DTO object for parsed key
// internal class AyaNovaLicenseKey
// {
// public AyaNovaLicenseKey()
// {
// Features = new List<LicenseFeature>();
// RegisteredTo = UNLICENSED_TOKEN;
// //Id = RegisteredTo;
// LicenseFormat = "8";
// var vv = Math.Truncate((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
// string sId = vv.ToString();
// if (sId.Contains(","))
// sId = sId.Split('.')[0];
// Id = sId;
// }
// public override string ToString()
// {
// System.Text.StringBuilder sb = new StringBuilder();
// sb.AppendLine($"Registered to: {RegisteredTo}");
// sb.AppendLine($"Database id: {DbId}");
// sb.AppendLine($"Type: {(Perpetual ? "Perpetual" : "Subscription")}");
// if (WillExpire)
// sb.AppendLine($"Available for use until: {LicenseExpiration.ToLongDateString()}");
// if (Perpetual)
// sb.AppendLine($"Support and updates available until: {MaintenanceExpiration.ToLongDateString()}");
// foreach (LicenseFeature f in Features)
// {
// if (f.Feature == TRIAL_FEATURE_NAME)
// {
// sb.AppendLine("Temporary license for evaluation");
// continue;
// }
// //default for items added later not tokenized
// if (f.Count > 0)
// sb.AppendLine($"{f.Feature}: {f.Count}");
// else
// sb.AppendLine($"{f.Feature}");
// }
// return sb.ToString();
// }
// public bool IsEmpty
// {
// get
// {
// //Key is empty if it's not registered to anyone or there are no features in it
// return string.IsNullOrWhiteSpace(RegisteredTo) || (Features == null || Features.Count == 0);
// }
// }
// /// <summary>
// /// Fetch the license status of the feature in question
// /// </summary>
// /// <param name="Feature"></param>
// /// <returns>LicenseFeature object or null if there is no license</returns>
// public LicenseFeature GetLicenseFeature(string Feature)
// {
// if (IsEmpty)
// return null;
// string lFeature = Feature.ToLowerInvariant();
// foreach (LicenseFeature l in Features)
// {
// if (l.Feature.ToLowerInvariant() == lFeature)
// {
// return l;
// }
// }
// return null;
// }
// /// <summary>
// /// Check for the existance of license feature
// /// </summary>
// /// <param name="Feature"></param>
// /// <returns>bool</returns>
// public bool HasLicenseFeature(string Feature)
// {
// if (IsEmpty)
// return false;
// string lFeature = Feature.ToLowerInvariant();
// foreach (LicenseFeature l in Features)
// {
// if (l.Feature.ToLowerInvariant() == lFeature)
// {
// return true;
// }
// }
// return false;
// }
// public bool WillExpire
// {
// get
// {
// return LicenseExpiration < DateUtil.EmptyDateValue;
// }
// }
// public bool LicenseExpired
// {
// get
// {
// return LicenseExpiration > DateTime.Now;
// }
// }
// public bool MaintenanceExpired
// {
// get
// {
// return MaintenanceExpiration > DateTime.Now;
// }
// }
// public bool TrialLicense
// {
// get
// {
// return HasLicenseFeature(TRIAL_FEATURE_NAME);
// }
// }
// public string LicenseFormat { get; set; }
// public string Id { get; set; }
// public string RegisteredTo { get; set; }
// public string DbId { get; set; }
// public bool Perpetual { get; set; }
// public DateTime LicenseExpiration { get; set; }
// public DateTime MaintenanceExpiration { get; set; }
// public List<LicenseFeature> Features { get; set; }
// }
#endregion
// public static string GetRavenTrialKey(License l)//string dbid, string CompanyName, bool Perpetual)
// {
// //Build a sample test key, sign it and return it
// AyaNovaLicenseKey k = new AyaNovaLicenseKey();
// k.LicenseFormat = "8";
// k.RegisteredTo = l.RegTo;
// k.DbId = l.DbId;
// k.Perpetual = l.PGroup == ProductGroup.RavenPerpetual;
// //flag as trial key not regular key
// k.Features.Add(new LicenseFeature() { Feature = TRIAL_FEATURE_NAME, Count = 0 });
// if (k.Perpetual)
// {
// //trial period time limit
// //Normal code, uncomment this when done testing expiration dates of perpetual
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);
// //Testing code for 3 minute long evaluation license in case need to test with a key that expires
// // #warning FYI ROCKFISH is SET TO GENERATE A VERY SHORT TEST KEY
// // k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddMinutes(3); //TEST VALUE FOR DIAGNOSING LICENSE EXPIRATION ISSUES
// //5k inside staff users will cover huge seeding level easily
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
// }
// else
// {
// //SUBSCRIPTION
// //trial period time limit
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);//NOTE: this preserves the current time, should it be set to midnight or something?
// //20k customer contacts will cover huge seeding level easily
// //5k inside staff users will cover huge seeding level easily
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_CUSTOMER_USERS_FEATURE_NAME, Count = 20000 });
// k.Features.Add(new LicenseFeature() { Feature = MAXIMUM_DATA_GB_FEATURE_NAME, Count = 20 });
// }
// return GenerateRavenKey(k);
// }
// //TESTING ONLY this is for development purposes only
// //No external usage
// public static string GetRavenTestKey(string dbid, bool Perpetual)
// {
// //Build a sample test key, sign it and return it
// AyaNovaLicenseKey k = new AyaNovaLicenseKey();
// k.LicenseFormat = "8";
// k.RegisteredTo = "GZ TestCo Inc.";
// k.DbId = dbid;
// k.Perpetual = Perpetual;
// if (Perpetual)
// {
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);
// //5k inside staff users will cover huge seeding level easily
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
// }
// else
// {
// //SUBSCRIPTION
// k.MaintenanceExpiration = k.LicenseExpiration = DateTime.UtcNow.AddDays(TRIAL_PERIOD_DAYS);
// //20k customer contacts will cover huge seeding level easily
// //5k inside staff users will cover huge seeding level easily
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_INTERNAL_USERS_FEATURE_NAME, Count = 5000 });
// k.Features.Add(new LicenseFeature() { Feature = ACTIVE_CUSTOMER_USERS_FEATURE_NAME, Count = 20000 });
// k.Features.Add(new LicenseFeature() { Feature = MAXIMUM_DATA_GB_FEATURE_NAME, Count = 20 });
// }
// k.Features.Add(new LicenseFeature() { Feature = TRIAL_FEATURE_NAME, Count = 0 });
// return GenerateRavenKey(k);
// }
#endregion old stuff
//eoc //eoc
} }