diff --git a/src/lib_license/License.cpp b/src/lib_license/License.cpp index 46cc824c..caa9598e 100644 --- a/src/lib_license/License.cpp +++ b/src/lib_license/License.cpp @@ -1,11 +1,12 @@ #include "License.h" #include +#include #include -#include #include -#include +#include #include +#include //#include "botan_all.h" #include "boost/date_time.hpp" @@ -187,6 +188,16 @@ std::string License::getLicenseInfo() const return info; } +bool License::isNonCommercialLicenseType(const std::string type) const +{ + for ( const std::string nonCommercialLicenseType : NON_COMMERCIAL_LICENSE_TYPES) + { if (type == nonCommercialLicenseType) { + return true; + } + } + return false; +} + int License::getTimeLeft() const { const std::string testText = "Test License - valid till "; @@ -251,7 +262,7 @@ void License::createMessage(const std::string& user, const std::string& version, m_lines.push_back(BEGIN_LICENSE); m_lines.push_back(user); m_lines.push_back(type); - std::string versionstring = "Coati " + getVersion(); + std::string versionstring = "Sourcetrail " + getVersion(); m_lines.push_back(versionstring); std::string pass9 = Botan::generate_passhash9(versionstring, *(m_rng.get())); m_lines.push_back(pass9); @@ -363,10 +374,11 @@ bool License::isValid() const } try // lol, now we try to handle errors? { - // why is this still here? - if(Botan::check_passhash9("Coati "+ getVersion(), getHashLine())) + // remove coati in version 1 + if(!(Botan::check_passhash9("Coati "+ getVersion(), getHashLine()) + || Botan::check_passhash9("Sourcetrail " + getVersion(), getHashLine()))) { - // std::cout << "Hash from Coati "+ getVersion() + " confirmed" << std::endl; + return false; } std::string signatureString = getSignature(); @@ -425,11 +437,26 @@ std::string License::getVersion() const { if(m_version.empty()) { + + std::string line = getVersionLine(); + + //TODO: remove coati in version 1 + const std::array appNames = {{ "Coati ", "Sourcetrail " }}; + + for ( std::string appName : appNames) + { + if (line.substr(0,appName.length()) == appName) + { + return line.substr(appName.length()); + } + } + return "x"; } return m_version; } + bool License::loadPublicKeyFromFile(const std::string& filename) { if (!filename.empty()) diff --git a/src/lib_license/License.h b/src/lib_license/License.h index 916bf1e6..a54450d2 100644 --- a/src/lib_license/License.h +++ b/src/lib_license/License.h @@ -69,6 +69,8 @@ private: void addSignature(const std::string&); std::string getEncodeKey(const std::string applicationLocation) const; + bool isNonCommercialLicenseType(const std::string type) const; + std::string m_version; std::string m_publicKeyFilename; std::shared_ptr m_publicKey; @@ -78,6 +80,8 @@ private: const std::string KEY_FILEENDING = ".pem"; const std::string BEGIN_LICENSE = "-----BEGIN LICENSE-----"; const std::string END_LICENSE = "-----END LICENSE-----"; + + const std::vector NON_COMMERCIAL_LICENSE_TYPES = { { "Private/Academic Single User License" } }; }; #endif // COATI_LICENSE_H diff --git a/src/test/GeneratorTestSuite.h b/src/test/GeneratorTestSuite.h index 8d1b5558..14ad2c98 100644 --- a/src/test/GeneratorTestSuite.h +++ b/src/test/GeneratorTestSuite.h @@ -63,6 +63,7 @@ public: TS_ASSERT(license.isValid()); TS_ASSERT_EQUALS(license.getTimeLeft(), 10); + TS_ASSERT_EQUALS(license.getVersion(), "v2"); license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); license.loadFromString(generator.encodeLicense("User", -10));