From dd366eaf5b42191f68140a4adc1679326f801492 Mon Sep 17 00:00:00 2001 From: Peter Sykora Date: Tue, 1 May 2018 16:27:38 +0200 Subject: [PATCH] [impl] Logging into AppData folder [chore] Change API appId parameter => productId parameter --- api/LicenseClient.h | 2 +- api/OSUtils.cpp | 33 +++++++++++++++++++++++ api/OSUtils.h | 5 ++++ libLicenseClient.vcxproj | 2 ++ libLicenseClient.vcxproj.filters | 6 +++++ src/LicenseClient.cpp | 45 ++++++++++++++++++-------------- 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 api/OSUtils.cpp create mode 100644 api/OSUtils.h diff --git a/api/LicenseClient.h b/api/LicenseClient.h index e85b012..55520c9 100644 --- a/api/LicenseClient.h +++ b/api/LicenseClient.h @@ -11,7 +11,7 @@ struct ActivationData { std::string activationId; - std::string appId; + std::string productId; SystemParams systemParams; std::set licensedModules; }; diff --git a/api/OSUtils.cpp b/api/OSUtils.cpp new file mode 100644 index 0000000..c7b0d5b --- /dev/null +++ b/api/OSUtils.cpp @@ -0,0 +1,33 @@ +#include "OSUtils.h" + +#include + +#include + +namespace fs = boost::filesystem; + +boost::filesystem::path ensureLogFolder(const std::string& appName) +{ + char szPath[MAX_PATH] = { 0 }; + + if (SUCCEEDED(SHGetFolderPathA(NULL, + CSIDL_APPDATA | CSIDL_FLAG_CREATE, + NULL, + 0, + szPath))) + { + auto logPath = fs::path(szPath) / appName / "Log"; + if (!fs::exists(logPath)) + { + fs::create_directories(logPath); + } + if (!fs::is_directory(logPath)) + { + throw std::runtime_error("Could not access log directory"); + } + + return logPath; + } + + throw std::runtime_error("Could not find application data folder"); +} diff --git a/api/OSUtils.h b/api/OSUtils.h new file mode 100644 index 0000000..527cbc2 --- /dev/null +++ b/api/OSUtils.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +boost::filesystem::path ensureLogFolder(const std::string& appName); diff --git a/libLicenseClient.vcxproj b/libLicenseClient.vcxproj index e8cf53a..87d26e1 100644 --- a/libLicenseClient.vcxproj +++ b/libLicenseClient.vcxproj @@ -103,6 +103,7 @@ + @@ -110,6 +111,7 @@ + diff --git a/libLicenseClient.vcxproj.filters b/libLicenseClient.vcxproj.filters index 6fe4622..6a82fcd 100644 --- a/libLicenseClient.vcxproj.filters +++ b/libLicenseClient.vcxproj.filters @@ -63,6 +63,9 @@ src + + api + @@ -101,5 +104,8 @@ src + + src + \ No newline at end of file diff --git a/src/LicenseClient.cpp b/src/LicenseClient.cpp index 5666c2b..e3a4ad6 100644 --- a/src/LicenseClient.cpp +++ b/src/LicenseClient.cpp @@ -51,7 +51,8 @@ namespace ASN1 = CryptoPP::ASN1; namespace { -static const std::string appId = "coc"; +static const std::string productId = "coc"; +static const std::string serverPath = "http://localhost:3000/api/v1"; static const uint32_t initializationVectorSize = AES::BLOCKSIZE; static const uint32_t macTagSize = 16; @@ -125,13 +126,13 @@ std::string readBinaryFile(const std::string& filename) struct PreactivationRequest { - std::string appId = appId; + std::string productId = productId; SystemParams systemParams; }; struct ActivationRequest { - std::string appId = appId; + std::string productId = productId; SystemParams systemParams; std::string licenseNumber; }; @@ -146,6 +147,7 @@ struct CheckUpdatesRequest { SystemParams systemParams; std::string activationId; + std::string productId = productId; std::map moduleVersions; }; @@ -163,8 +165,8 @@ void serialize(std::ostream& os, const ActivationData& a) os << "{"; os << "\"activationId\":"; serialize(os, a.activationId); - os << ",\"appId\":"; - serialize(os, a.appId); + os << ",\"productId\":"; + serialize(os, a.productId); os << ",\"systemParams\":"; serialize(os, a.systemParams); os << ",\"licensedModules\":"; @@ -175,7 +177,7 @@ void serialize(std::ostream& os, const ActivationData& a) void deserialize(const pt::ptree& tree, ActivationData& a) { deserialize(tree.get_child("activationId"), a.activationId); - deserialize(tree.get_child("appId"), a.appId); + deserialize(tree.get_child("productId"), a.productId); deserialize(tree.get_child("systemParams"), a.systemParams); deserialize(tree.get_child("licensedModules"), a.licensedModules); } @@ -199,8 +201,8 @@ void deserialize(const pt::ptree& tree, SignedData& d) void serialize(std::ostream& os, const PreactivationRequest& a) { os << "{"; - os << "\"appId\":"; - ::serialize(os, a.appId); + os << "\"productId\":"; + ::serialize(os, a.productId); os << ",\"systemParams\":"; ::serialize(os, a.systemParams); os << "}"; @@ -209,8 +211,8 @@ void serialize(std::ostream& os, const PreactivationRequest& a) void serialize(std::ostream& os, const ActivationRequest& a) { os << "{"; - os << "\"appId\":"; - ::serialize(os, a.appId); + os << "\"productId\":"; + ::serialize(os, a.productId); os << ",\"systemParams\":"; ::serialize(os, a.systemParams); os << ",\"licenseNumber\":"; @@ -241,6 +243,8 @@ void serialize(std::ostream& os, const CheckUpdatesRequest& r) ::serialize(os, r.systemParams); os << ",\"activationId\":"; ::serialize(os, r.activationId); + os << ",\"productId\":"; + serialize(os, r.productId); os << ",\"moduleVersions\":"; ::serialize(os, r.moduleVersions); os << "}"; @@ -260,7 +264,10 @@ void deserialize(const pt::ptree& tree, CheckUpdatesResponse& r) { r.licenseFile = {}; } - deserialize(tree.get_child("moduleUpdates"), r.moduleUpdates); + if (r.success) + { + deserialize(tree.get_child("moduleUpdates"), r.moduleUpdates); + } } @@ -291,7 +298,7 @@ void LicenseClient::init() bool LicenseClient::tryPreactivate(HTTPClient &httpClient) { - PreactivationRequest req{ appId, m_systemParams }; + PreactivationRequest req{ productId, m_systemParams }; std::string jsonReq; { @@ -303,7 +310,7 @@ bool LicenseClient::tryPreactivate(HTTPClient &httpClient) std::string jsonRes; { std::ostringstream ss2; - httpClient.postJson("http://localhost:3000/activate0", jsonReq, ss2); + httpClient.postJson(serverPath + "/activate0", jsonReq, ss2); jsonRes = ss2.str(); } @@ -330,11 +337,11 @@ bool LicenseClient::activate(HTTPClient &httpClient, const std::string & license std::ostringstream ss1; - ActivationRequest req{ appId, m_systemParams, licenseNumber }; + ActivationRequest req{ productId, m_systemParams, licenseNumber }; serialize(ss1, req); std::stringstream ss2; - httpClient.postJson("http://localhost:3000/activate", ss1.str() , ss2); + httpClient.postJson(serverPath + "/activate", ss1.str() , ss2); ActivationResponse activationResponse; pt::ptree root; @@ -356,13 +363,13 @@ std::vector LicenseClient::checkForUpdates(HTTPClient & httpClient { if (!isActivated()) { throw std::runtime_error("Not active"); } - CheckUpdatesRequest req{ m_systemParams, m_activationData->activationId, currentVersions }; + CheckUpdatesRequest req{ m_systemParams, m_activationData->activationId, productId, currentVersions }; std::ostringstream ss1; serialize(ss1, req); std::stringstream ss2; - httpClient.postJson("http://localhost:3000/check", ss1.str(), ss2); + httpClient.postJson(serverPath + "/check", ss1.str(), ss2); CheckUpdatesResponse checkUpdatesResponse; pt::ptree root; @@ -371,7 +378,7 @@ std::vector LicenseClient::checkForUpdates(HTTPClient & httpClient if (!checkUpdatesResponse.success) { - throw std::exception("Could not check for updates"); + throw std::runtime_error("Could not check for updates"); } if (checkUpdatesResponse.licenseFile) @@ -581,7 +588,7 @@ bool LicenseClient::validateActivationData(const ActivationData & activationData return false; } - if (activationData.appId != appId) + if (activationData.productId != productId) { return false; }