diff --git a/src/lib_license/License.cpp b/src/lib_license/License.cpp index 02fac8fb..39f58f7f 100644 --- a/src/lib_license/License.cpp +++ b/src/lib_license/License.cpp @@ -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); } diff --git a/src/lib_license/License.h b/src/lib_license/License.h index 07711cad..19904dce 100644 --- a/src/lib_license/License.h +++ b/src/lib_license/License.h @@ -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; diff --git a/src/license_generator/Generator.cpp b/src/license_generator/Generator.cpp index 61723f7e..78d97f69 100644 --- a/src/license_generator/Generator.cpp +++ b/src/license_generator/Generator.cpp @@ -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(); - 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(); } - diff --git a/src/license_generator/Generator.h b/src/license_generator/Generator.h index fe498a6e..21d9e44a 100644 --- a/src/license_generator/Generator.h +++ b/src/license_generator/Generator.h @@ -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 ); diff --git a/src/license_generator/main.cpp b/src/license_generator/main.cpp index 912ebb63..adba79c4 100644 --- a/src/license_generator/main.cpp +++ b/src/license_generator/main.cpp @@ -35,7 +35,7 @@ bool process_command_line(int argc, char** argv) ("version,v", po::value(&version), "Versionnumber (in format 20xx.x) until Sourcetrail valid") ("users,u", po::value(&numberOfUsers), "Number of users") ("licenseType,t", po::value(&type), "License Type of ") - ("testLicense,e", po::value(&days), "Generates a test license for days"); + ("expiration,e", po::value(&days), "Valid for 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")) diff --git a/src/test/GeneratorTestSuite.h b/src/test/GeneratorTestSuite.h index 7d3f063f..80b2ca20 100644 --- a/src/test/GeneratorTestSuite.h +++ b/src/test/GeneratorTestSuite.h @@ -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