BitBetter/src/licenseGen/Program.cs

450 lines
20 KiB
C#
Raw Permalink Normal View History

2018-11-22 04:42:23 +01:00
using System;
2017-10-19 18:51:05 +02:00
using System.IO;
using System.Linq;
2017-10-29 20:13:38 +01:00
using System.Runtime.Loader;
2017-10-19 18:51:05 +02:00
using System.Security.Cryptography.X509Certificates;
2017-10-29 20:13:38 +01:00
using Microsoft.Extensions.CommandLineUtils;
using Newtonsoft.Json;
2017-10-19 18:51:05 +02:00
namespace bitwardenSelfLicensor
{
class Program
{
2017-10-22 19:08:50 +02:00
static int Main(string[] args)
2017-10-19 18:51:05 +02:00
{
2017-10-29 20:13:38 +01:00
var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication();
2017-10-29 20:44:54 +01:00
var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue);
2017-10-29 20:13:38 +01:00
var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue);
2017-10-30 01:58:34 +01:00
bool certExists()
{
2017-10-29 20:44:54 +01:00
return File.Exists(cert.Value());
}
2017-10-30 01:58:34 +01:00
bool coreExists()
{
2017-10-29 20:44:54 +01:00
return File.Exists(coreDll.Value());
}
bool verifyTopOptions()
{
return !string.IsNullOrWhiteSpace(cert.Value()) &&
!string.IsNullOrWhiteSpace(coreDll.Value()) &&
certExists() && coreExists();
}
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
app.Command("interactive", config =>
{
string buff="", licensetype="", name="", email="", businessname="";
short storage = 0;
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
bool valid_guid = false, valid_installid = false;
Guid guid = new Guid(), installid = new Guid();
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
config.OnExecute(() =>
{
if (!verifyTopOptions())
{
if (!coreExists()) config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
if (!certExists()) config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
config.ShowHelp();
return 1;
}
WriteLine("Interactive license mode...");
while (licensetype == "")
{
WriteLine("What would you like to generate, a [u]ser license or an [o]rg license?");
buff = Console.ReadLine();
if(buff == "u")
{
licensetype = "user";
WriteLineOver("Okay, we will generate a user license.");
while (valid_guid == false)
{
WriteLine("Please provide the user's guid — refer to the Readme for details on how to retrieve this. [GUID]:");
buff = Console.ReadLine();
if (Guid.TryParse(buff, out guid))valid_guid = true;
else WriteLineOver("The user-guid provided does not appear to be valid.");
}
}
else if (buff == "o")
{
licensetype = "org";
WriteLineOver("Okay, we will generate an organization license.");
while (valid_installid == false)
{
WriteLine("Please provide your Bitwarden Install-ID — refer to the Readme for details on how to retrieve this. [Install-ID]:");
buff = Console.ReadLine();
if (Guid.TryParse(buff, out installid)) valid_installid = true;
else WriteLineOver("The install-id provided does not appear to be valid.");
}
while (businessname == "")
{
WriteLineOver("Please enter a business name, default is BitBetter. [Business Name]:");
buff = Console.ReadLine();
if (buff == "") businessname = "BitBetter";
else if (checkBusinessName(buff)) businessname = buff;
}
}
else
{
WriteLineOver("Unrecognized option \'" + buff + "\'. ");
}
}
while (name == "")
{
WriteLineOver("Please provide the username this license will be registered to. [username]:");
buff = Console.ReadLine();
if ( checkUsername(buff) ) name = buff;
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
}
while (email == "")
{
WriteLineOver("Please provide the email address for the user " + name + ". [email]");
buff = Console.ReadLine();
if ( checkEmail(buff) ) email = buff;
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
}
while (storage == 0)
{
WriteLineOver("Extra storage space for the user " + name + ". (max.: " + short.MaxValue + "). Defaults to maximum value. [storage]");
buff = Console.ReadLine();
if (string.IsNullOrWhiteSpace(buff))
{
storage = short.MaxValue;
}
else
{
if (checkStorage(buff)) storage = short.Parse(buff);
}
}
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
if (licensetype == "user")
{
WriteLineOver("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", User-GUID: \"" + guid + "\"? Y/n");
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
buff = Console.ReadLine();
if ( buff == "" || buff == "y" || buff == "Y" )
{
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, guid, null);
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
}
else
{
WriteLineOver("Exiting...");
return 0;
}
}
else if (licensetype == "org")
{
WriteLineOver("Confirm creation of \"organization\" license for business name: \"" + businessname + "\", username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", Install-ID: \"" + installid + "\"? Y/n");
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
buff = Console.ReadLine();
if ( buff == "" || buff == "y" || buff == "Y" )
{
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, installid, businessname, null);
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
}
else
{
WriteLineOver("Exiting...");
return 0;
}
}
return 0;
});
});
2017-10-29 20:13:38 +01:00
app.Command("user", config =>
{
var name = config.Argument("Name", "your name");
var email = config.Argument("Email", "your email");
2017-10-30 01:58:34 +01:00
var userIdArg = config.Argument("User ID", "your user id");
var storage = config.Argument("Storage", "extra storage space in GB. Maximum is " + short.MaxValue + " (optional, default = max)");
2017-10-29 20:13:38 +01:00
var key = config.Argument("Key", "your key id (optional)");
var help = config.HelpOption("--help | -h | -?");
config.OnExecute(() =>
{
2017-10-29 20:44:54 +01:00
if (!verifyTopOptions())
2017-10-29 20:13:38 +01:00
{
2017-10-30 01:58:34 +01:00
if (!coreExists())
2017-10-29 20:44:54 +01:00
{
config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
}
2017-10-30 01:58:34 +01:00
if (!certExists())
{
2017-10-29 20:44:54 +01:00
config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
}
2017-10-30 01:58:34 +01:00
2017-10-29 20:44:54 +01:00
config.ShowHelp();
2017-10-29 20:13:38 +01:00
return 1;
}
else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value))
{
2017-10-29 20:44:54 +01:00
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'");
2017-10-29 20:13:38 +01:00
config.ShowHelp("user");
return 1;
}
2017-10-30 01:58:34 +01:00
if (string.IsNullOrWhiteSpace(userIdArg.Value) || !Guid.TryParse(userIdArg.Value, out Guid userId))
{
config.Error.WriteLine($"User ID not provided");
config.ShowHelp("user");
return 1;
}
short storageShort = 0;
if (!string.IsNullOrWhiteSpace(storage.Value))
{
var parsedStorage = double.Parse(storage.Value);
if (parsedStorage > short.MaxValue || parsedStorage < 0)
{
config.Error.WriteLine("The storage value provided is outside the accepted range of [0-" + short.MaxValue + "]");
config.ShowHelp("org");
return 1;
}
storageShort = (short) parsedStorage;
}
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, storageShort, userId, key.Value);
2017-10-29 20:13:38 +01:00
return 0;
});
});
app.Command("org", config =>
{
var name = config.Argument("Name", "your name");
var email = config.Argument("Email", "your email");
var installId = config.Argument("InstallId", "your installation id (GUID)");
var storage = config.Argument("Storage", "extra storage space in GB. Maximum is " + short.MaxValue + " (optional, default = max)");
var businessName = config.Argument("BusinessName", "name for the organization (optional)");
2017-10-29 20:13:38 +01:00
var key = config.Argument("Key", "your key id (optional)");
var help = config.HelpOption("--help | -h | -?");
config.OnExecute(() =>
{
2017-10-29 20:44:54 +01:00
if (!verifyTopOptions())
2017-10-29 20:13:38 +01:00
{
2017-10-30 01:58:34 +01:00
if (!coreExists())
2017-10-29 20:44:54 +01:00
{
config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
}
2017-10-30 01:58:34 +01:00
if (!certExists())
{
2017-10-29 20:44:54 +01:00
config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
}
config.ShowHelp();
2017-10-29 20:13:38 +01:00
return 1;
}
else if (string.IsNullOrWhiteSpace(name.Value) ||
string.IsNullOrWhiteSpace(email.Value) ||
string.IsNullOrWhiteSpace(installId.Value))
{
2017-10-29 20:44:54 +01:00
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'");
2017-10-29 20:13:38 +01:00
config.ShowHelp("org");
return 1;
}
if (!Guid.TryParse(installId.Value, out Guid installationId))
{
config.Error.WriteLine("Unable to parse your installation id as a GUID");
2017-10-29 20:44:54 +01:00
config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}");
2017-10-29 20:13:38 +01:00
config.ShowHelp("org");
2017-10-29 20:44:54 +01:00
return 1;
2017-10-29 20:13:38 +01:00
}
short storageShort = 0;
if (!string.IsNullOrWhiteSpace(storage.Value))
{
var parsedStorage = double.Parse(storage.Value);
if (parsedStorage > short.MaxValue || parsedStorage < 0)
{
config.Error.WriteLine("The storage value provided is outside the accepted range of [0-" + short.MaxValue + "]");
config.ShowHelp("org");
return 1;
}
storageShort = (short) parsedStorage;
}
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, storageShort, installationId, businessName.Value, key.Value);
2017-10-29 20:13:38 +01:00
return 0;
});
});
app.OnExecute(() =>
{
app.ShowHelp();
return 10;
});
app.HelpOption("-? | -h | --help");
try
{
return app.Execute(args);
2017-10-19 18:51:05 +02:00
}
2017-10-29 20:13:38 +01:00
catch (Exception e)
{
Console.Error.WriteLine("Oops: {0}", e);
return 100;
2017-10-19 18:51:05 +02:00
}
2017-10-29 20:13:38 +01:00
}
2017-10-19 18:51:05 +02:00
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
// checkUsername Checks that the username is a valid username
static bool checkUsername(string s)
{
if ( string.IsNullOrWhiteSpace(s) ) {
WriteLineOver("The username provided doesn't appear to be valid.\n");
return false;
}
return true; // TODO: Actually validate
}
// checkBusinessName Checks that the Business Name is a valid username
static bool checkBusinessName(string s)
{
if ( string.IsNullOrWhiteSpace(s) ) {
WriteLineOver("The Business Name provided doesn't appear to be valid.\n");
return false;
}
return true; // TODO: Actually validate
}
// checkEmail Checks that the email address is a valid email address
static bool checkEmail(string s)
{
if ( string.IsNullOrWhiteSpace(s) ) {
WriteLineOver("The email provided doesn't appear to be valid.\n");
return false;
}
return true; // TODO: Actually validate
}
// checkStorage Checks that the storage is in a valid range
static bool checkStorage(string s)
{
if (string.IsNullOrWhiteSpace(s))
{
WriteLineOver("The storage provided doesn't appear to be valid.\n");
return false;
}
if (double.Parse(s) > short.MaxValue || double.Parse(s) < 0)
{
WriteLineOver("The storage value provided is outside the accepted range of [0-" + short.MaxValue + "].\n");
return false;
}
return true;
}
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
// WriteLineOver Writes a new line to console over last line.
static void WriteLineOver(string s)
{
Console.SetCursorPosition(0, Console.CursorTop -1);
Console.WriteLine(s);
}
// WriteLine This wrapper is just here so that console writes all look similar.
static void WriteLine(string s)
{
Console.WriteLine(s);
}
static void GenerateUserLicense(X509Certificate2 cert, string corePath, string userName, string email, short storage, Guid userId, string key)
2017-10-29 20:13:38 +01:00
{
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
var type = core.GetType("Bit.Core.Models.Business.UserLicense");
var licenseTypeEnum = core.GetType("Bit.Core.Enums.LicenseType");
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
var license = Activator.CreateInstance(type);
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
void set(string name, object value)
{
type.GetProperty(name).SetValue(license, value);
}
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
set("LicenseKey", string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
2017-10-30 01:58:34 +01:00
set("Id", userId);
2017-10-29 20:13:38 +01:00
set("Name", userName);
set("Email", email);
set("Premium", true);
set("MaxStorageGb", storage == 0 ? short.MaxValue : storage);
2017-10-29 20:13:38 +01:00
set("Version", 1);
set("Issued", DateTime.UtcNow);
2018-11-22 03:49:40 +01:00
set("Refresh", DateTime.UtcNow.AddYears(100).AddMonths(-1));
set("Expires", DateTime.UtcNow.AddYears(100));
2017-10-29 20:13:38 +01:00
set("Trial", false);
set("LicenseType", Enum.Parse(licenseTypeEnum, "User"));
2017-10-29 20:13:38 +01:00
set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0])));
set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert })));
2017-10-29 20:44:54 +01:00
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
2017-10-29 20:13:38 +01:00
}
2017-10-19 18:51:05 +02:00
static void GenerateOrgLicense(X509Certificate2 cert, string corePath, string userName, string email, short storage, Guid instalId, string businessName, string key)
2017-10-29 20:13:38 +01:00
{
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
var type = core.GetType("Bit.Core.Models.Business.OrganizationLicense");
var licenseTypeEnum = core.GetType("Bit.Core.Enums.LicenseType");
var planTypeEnum = core.GetType("Bit.Core.Enums.PlanType");
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
var license = Activator.CreateInstance(type);
2017-10-19 18:51:05 +02:00
2017-10-29 20:13:38 +01:00
void set(string name, object value)
{
type.GetProperty(name).SetValue(license, value);
2017-10-19 18:51:05 +02:00
}
2017-10-29 20:13:38 +01:00
set("LicenseKey", string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
set("InstallationId", instalId);
set("Id", Guid.NewGuid());
set("Name", userName);
set("BillingEmail", email);
License Generator Interactive Mode (#23) * Added a Key Generating script To make the keygen process a bit easier I've added a `generate-keys.sh` script that can be found in the `.keys` directory. It will generate the key & cert and bundle them into the required pkcs#12 file. I've updated the readme to include instructions on the script. * Generate bitbetter/identiry container with modified Core.dll Added the generation of a second modified container, bitbetter/identity, which contains the modified dll. Fixes #12. This works on my testing environment but has not gone through extensive testing. I'd recommend a review and cleanup of this commit before it is merged into the develop or master branches. * Updated Docs I've taken the steps written out by @online-stuff and consolidated/organized them into the README. This closes #13. In a future update it might be worth adding a docs/ directory and breaking the readme into several docs that link to one another. * Updated build.sh Build now checks for and creates missing .keys directories. * Added subj to allow for non-interactive use. * Generate keys on build. * Circle-ci needs to gen keys to test build * Generate keys if they don't exist. Don't overwrite if keys already exist. * Generate keys online in the .keys directory * Updated README.md * Added initial interactive options * Functional implementation of licensegen interactive mode. * Bumped Newtonson.Json version Never versions of the dotnet-sdk have issues with older Newtonsoft versions. 12.0.1 seems to satisfy the widest variety of sdk versions. * Removing old readme * Removed Duplicate Section * Fixed typo This fixes and closes issue #24.
2019-06-05 20:19:39 +02:00
set("BusinessName", string.IsNullOrWhiteSpace(businessName) ? "BitBetter" : businessName);
2017-10-29 20:13:38 +01:00
set("Enabled", true);
2018-11-22 04:42:23 +01:00
set("Plan", "Custom");
set("PlanType", Enum.Parse(planTypeEnum, "Custom"));
set("Seats", (int)short.MaxValue);
2017-10-29 20:13:38 +01:00
set("MaxCollections", short.MaxValue);
set("UsePolicies", true);
set("UseSso", true);
set("UseKeyConnector", true);
set("UseScim", true);
2017-10-29 20:13:38 +01:00
set("UseGroups", true);
2018-11-22 04:42:23 +01:00
set("UseEvents", true);
2017-10-29 20:13:38 +01:00
set("UseDirectory", true);
set("UseTotp", true);
2018-11-22 04:42:23 +01:00
set("Use2fa", true);
set("UseApi", true);
set("UseResetPassword", true);
set("UseCustomPermissions", true);
set("MaxStorageGb", storage == 0 ? short.MaxValue : storage);
2018-11-22 04:42:23 +01:00
set("SelfHost", true);
set("UsersGetPremium", true);
set("Version", 10);
2017-10-29 20:13:38 +01:00
set("Issued", DateTime.UtcNow);
2018-11-22 03:49:40 +01:00
set("Refresh", DateTime.UtcNow.AddYears(100).AddMonths(-1));
set("Expires", DateTime.UtcNow.AddYears(100));
2017-10-29 20:13:38 +01:00
set("Trial", false);
set("LicenseType", Enum.Parse(licenseTypeEnum, "Organization"));
2017-10-29 20:13:38 +01:00
set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0])));
set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert })));
2017-10-29 20:44:54 +01:00
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
2017-10-19 18:51:05 +02:00
}
}
}