license: Allow any license type to specify expiration by days

This commit is contained in:
Eberhard Graether
2018-05-23 06:26:01 +02:00
parent 59f32e9942
commit 382f17ffa2
6 changed files with 53 additions and 45 deletions
+11 -9
View File
@@ -62,6 +62,7 @@ void License::createHeader(
const std::string& user,
const std::string& type,
const std::string& expiration,
bool expiresAtDate,
size_t numberOfUsers
)
{
@@ -71,6 +72,7 @@ void License::createHeader(
}
m_user = user;
m_expire = expiration;
m_expiresAtDate = expiresAtDate;
m_type = type;
m_numberOfUsers = numberOfUsers;
}
@@ -145,9 +147,11 @@ bool License::extractData(const std::string& data, LICENSE_LINE line)
return !m_user.empty();
case EXPIRE_LINE:
m_expire = removeCaption(data, LicenseConstants::VALID_UP_TO_STRING);
m_expiresAtDate = false;
if (m_expire.empty())
{
m_expire = removeCaption(data, LicenseConstants::VALID_UNTIL_STRING);
m_expiresAtDate = true;
}
return !m_expire.empty();
case TYPE_LINE:
@@ -187,18 +191,16 @@ std::string License::getType() const
std::string License::getExpireLine() const
{
return (m_type == LicenseConstants::TEST_LICENSE_STRING ?
LicenseConstants::VALID_UNTIL_STRING :
LicenseConstants::VALID_UP_TO_STRING)
+ m_expire;
return (m_expiresAtDate ?
LicenseConstants::VALID_UNTIL_STRING :
LicenseConstants::VALID_UP_TO_STRING) + m_expire;
}
std::string License::getExpireLineUI() const
{
return toLowerCase(m_type == LicenseConstants::TEST_LICENSE_STRING ?
LicenseConstants::VALID_UNTIL_STRING :
LicenseConstants::VALID_UP_TO_STRING)
+ m_expire;
return toLowerCase(m_expiresAtDate ?
LicenseConstants::VALID_UNTIL_STRING :
LicenseConstants::VALID_UP_TO_STRING) + m_expire;
}
std::string License::getLicenseInfo() const
@@ -425,7 +427,7 @@ bool License::isValid() const
bool License::isExpired() const
{
if (getType() == LicenseConstants::TEST_LICENSE_STRING)
if (m_expiresAtDate)
{
return (getTimeLeft() == -1);
}
+2
View File
@@ -69,6 +69,7 @@ public:
const std::string& user,
const std::string& type,
const std::string& expiration,
bool expiresAtDate,
size_t numberOfUsers = 0
);
std::string getExpireLine() const;
@@ -120,6 +121,7 @@ private:
size_t m_numberOfUsers;
bool m_createdWithSeats;
std::string m_expire;
bool m_expiresAtDate;
std::string m_hashLine;
std::string m_signature;
+26 -25
View File
@@ -57,24 +57,12 @@ std::string Generator::getPublicKeyFilename()
return m_publicKeyFile;
}
std::string Generator::encodeLicense(const std::string& user, const int days)
{
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
boost::gregorian::days daysToTry(days);
boost::gregorian::date expireDate = today + daysToTry;
createLicense(user, LicenseConstants::TEST_LICENSE_STRING, boost::gregorian::to_simple_string(expireDate), 0);
return m_license->getLicenseString();
}
std::string Generator::encodeLicense(
const std::string& user,
const std::string& licenseType,
size_t numberOfUsers,
const std::string& version
)
{
){
m_license = nullptr;
if (user.size() <= 0)
{
@@ -93,15 +81,29 @@ std::string Generator::encodeLicense(
Version tempVersion = Version::fromString(version);
if (tempVersion.isValid())
{
createLicense(user, licenseType, tempVersion.toShortString(), numberOfUsers);
createLicense(user, licenseType, tempVersion.toShortString(), false, numberOfUsers);
}
}
if (!m_license)
{
createLicense(user, licenseType, getExpireVersion(), numberOfUsers);
createLicense(user, licenseType, getExpireVersion(), false, numberOfUsers);
}
return m_license->getLicenseString();
}
std::string Generator::encodeLicense(
const std::string& user,
const std::string& licenseType,
size_t numberOfUsers,
size_t days
){
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
boost::gregorian::days daysToTry(days);
boost::gregorian::date expireDate = today + daysToTry;
createLicense(user, licenseType, boost::gregorian::to_simple_string(expireDate), true, numberOfUsers);
return m_license->getLicenseString();
}
@@ -175,7 +177,6 @@ void Generator::writeKeysToFiles()
return;
}
std::cout << "public key filename: " << publicKeyFilename << std::endl;
std::ofstream pub(publicKeyFilename);
pub << getPublicKeyPEMFileAsString();
@@ -240,12 +241,13 @@ void Generator::createLicense(
const std::string& user,
const std::string& type,
const std::string& expiration,
bool expiresAtDate,
size_t numberOfUsers
)
{
){
m_license = std::make_unique<License>();
m_license->createHeader(user, type, expiration, numberOfUsers);
m_license->createHeader(
user, type.size() ? type : LicenseConstants::TEST_LICENSE_STRING, expiration, expiresAtDate, numberOfUsers);
Botan::AutoSeeded_RNG rng;
std::string pass9 = Botan::generate_passhash9(m_license->getExpireLine(), m_rng);
@@ -273,10 +275,9 @@ int Generator::mapMonthToVersion(int month)
std::string Generator::getExpireVersion(int versions)
{
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
int monthNumber = today.month().as_number();
Version version(today.year(), mapMonthToVersion(monthNumber));
version += versions;
return version.toShortString();
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
int monthNumber = today.month().as_number();
Version version(today.year(), mapMonthToVersion(monthNumber));
version += versions;
return version.toShortString();
}
+7 -3
View File
@@ -21,13 +21,16 @@ public:
std::string encodeLicense(
const std::string& user,
const std::string& licenseType,
size_t numberOfUsers = 0,
const std::string& version = ""
size_t numberOfUsers,
const std::string& version
);
std::string encodeLicense(
const std::string& user,
const int days
const std::string& licenseType,
size_t numberOfUsers,
size_t days
);
void printLicenseAndWriteItToFile();
bool verifyLicense(const std::string& filename = "license.txt");
void generateKeys();
@@ -45,6 +48,7 @@ public:
const std::string& user,
const std::string& type,
const std::string& expiration,
bool expiresAtDate,
size_t numberOfUsers
);
+4 -5
View File
@@ -35,7 +35,7 @@ bool process_command_line(int argc, char** argv)
("version,v", po::value<std::string>(&version), "Versionnumber (in format 20xx.x) until Sourcetrail valid")
("users,u", po::value<int>(&numberOfUsers), "Number of users")
("licenseType,t", po::value<std::string>(&type), "License Type of ")
("testLicense,e", po::value<int>(&days), "Generates a test license for <value> days");
("expiration,e", po::value<int>(&days), "Valid for <value> days");
po::options_description hidden_description("Hidden Options");
hidden_description.add_options()
@@ -115,16 +115,15 @@ bool process_command_line(int argc, char** argv)
if (vm.count("generate"))
{
if (vm.count("testLicense"))
if (days > 0)
{
keygen.encodeLicense(user, days);
keygen.printLicenseAndWriteItToFile();
keygen.encodeLicense(user, type, numberOfUsers, days);
}
else
{
keygen.encodeLicense(user, type, numberOfUsers, version);
keygen.printLicenseAndWriteItToFile();
}
keygen.printLicenseAndWriteItToFile();
}
if (vm.count("check"))
+3 -3
View File
@@ -46,7 +46,7 @@ public:
generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString());
License license;
license.loadFromString(generator.encodeLicense("TestUser", "VolumeLicense", 20));
license.loadFromString(generator.encodeLicense("TestUser", "VolumeLicense", 20, ""));
TS_ASSERT_EQUALS(license.getNumberOfUsers(), 20);
}
@@ -59,13 +59,13 @@ public:
License license;
license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString());
license.loadFromString(generator.encodeLicense("User", 10));
license.loadFromString(generator.encodeLicense("User", "", 0, 10));
TS_ASSERT(license.isValid());
TS_ASSERT_EQUALS(license.getTimeLeft(), 10);
license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString());
license.loadFromString(generator.encodeLicense("User", -10));
license.loadFromString(generator.encodeLicense("User", "", 0, -10));
TS_ASSERT(!license.isValid());
// -1 and not -10 since it means it is expired