[impl] Logging into AppData folder

[chore] Change API appId parameter => productId parameter
This commit is contained in:
Peter Sykora 2018-05-01 16:27:38 +02:00
parent 6eef61fb94
commit dd366eaf5b
6 changed files with 73 additions and 20 deletions

View File

@ -11,7 +11,7 @@
struct ActivationData struct ActivationData
{ {
std::string activationId; std::string activationId;
std::string appId; std::string productId;
SystemParams systemParams; SystemParams systemParams;
std::set<std::string> licensedModules; std::set<std::string> licensedModules;
}; };

33
api/OSUtils.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "OSUtils.h"
#include <ShlObj.h>
#include <boost/filesystem.hpp>
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");
}

5
api/OSUtils.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <boost/filesystem/path.hpp>
boost::filesystem::path ensureLogFolder(const std::string& appName);

View File

@ -103,6 +103,7 @@
<ClInclude Include="api\ModuleManager.h" /> <ClInclude Include="api\ModuleManager.h" />
<ClInclude Include="api\ModuleUpdate.h" /> <ClInclude Include="api\ModuleUpdate.h" />
<ClInclude Include="api\ModuleVersion.h" /> <ClInclude Include="api\ModuleVersion.h" />
<ClInclude Include="api\OSUtils.h" />
<ClInclude Include="api\SystemParams.h" /> <ClInclude Include="api\SystemParams.h" />
<ClInclude Include="api\SystemParamsProvider.h" /> <ClInclude Include="api\SystemParamsProvider.h" />
<ClInclude Include="api\SystemParamsProvider_win.h" /> <ClInclude Include="api\SystemParamsProvider_win.h" />
@ -110,6 +111,7 @@
<ClInclude Include="src\SafeFileUtils.h" /> <ClInclude Include="src\SafeFileUtils.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="api\OSUtils.cpp" />
<ClCompile Include="src\CachedDownloader.cpp" /> <ClCompile Include="src\CachedDownloader.cpp" />
<ClCompile Include="src\CryptoUtils.cpp" /> <ClCompile Include="src\CryptoUtils.cpp" />
<ClCompile Include="src\HashUtils.cpp" /> <ClCompile Include="src\HashUtils.cpp" />

View File

@ -63,6 +63,9 @@
<ClInclude Include="src\SafeFileUtils.h"> <ClInclude Include="src\SafeFileUtils.h">
<Filter>src</Filter> <Filter>src</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="api\OSUtils.h">
<Filter>api</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\HashUtils.cpp"> <ClCompile Include="src\HashUtils.cpp">
@ -101,5 +104,8 @@
<ClCompile Include="src\SafeFileUtils.cpp"> <ClCompile Include="src\SafeFileUtils.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="api\OSUtils.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -51,7 +51,8 @@ namespace ASN1 = CryptoPP::ASN1;
namespace 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 initializationVectorSize = AES::BLOCKSIZE;
static const uint32_t macTagSize = 16; static const uint32_t macTagSize = 16;
@ -125,13 +126,13 @@ std::string readBinaryFile(const std::string& filename)
struct PreactivationRequest struct PreactivationRequest
{ {
std::string appId = appId; std::string productId = productId;
SystemParams systemParams; SystemParams systemParams;
}; };
struct ActivationRequest struct ActivationRequest
{ {
std::string appId = appId; std::string productId = productId;
SystemParams systemParams; SystemParams systemParams;
std::string licenseNumber; std::string licenseNumber;
}; };
@ -146,6 +147,7 @@ struct CheckUpdatesRequest
{ {
SystemParams systemParams; SystemParams systemParams;
std::string activationId; std::string activationId;
std::string productId = productId;
std::map<std::string, ModuleVersion> moduleVersions; std::map<std::string, ModuleVersion> moduleVersions;
}; };
@ -163,8 +165,8 @@ void serialize(std::ostream& os, const ActivationData& a)
os << "{"; os << "{";
os << "\"activationId\":"; os << "\"activationId\":";
serialize(os, a.activationId); serialize(os, a.activationId);
os << ",\"appId\":"; os << ",\"productId\":";
serialize(os, a.appId); serialize(os, a.productId);
os << ",\"systemParams\":"; os << ",\"systemParams\":";
serialize(os, a.systemParams); serialize(os, a.systemParams);
os << ",\"licensedModules\":"; os << ",\"licensedModules\":";
@ -175,7 +177,7 @@ void serialize(std::ostream& os, const ActivationData& a)
void deserialize(const pt::ptree& tree, ActivationData& a) void deserialize(const pt::ptree& tree, ActivationData& a)
{ {
deserialize(tree.get_child("activationId"), a.activationId); 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("systemParams"), a.systemParams);
deserialize(tree.get_child("licensedModules"), a.licensedModules); 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) void serialize(std::ostream& os, const PreactivationRequest& a)
{ {
os << "{"; os << "{";
os << "\"appId\":"; os << "\"productId\":";
::serialize(os, a.appId); ::serialize(os, a.productId);
os << ",\"systemParams\":"; os << ",\"systemParams\":";
::serialize(os, a.systemParams); ::serialize(os, a.systemParams);
os << "}"; os << "}";
@ -209,8 +211,8 @@ void serialize(std::ostream& os, const PreactivationRequest& a)
void serialize(std::ostream& os, const ActivationRequest& a) void serialize(std::ostream& os, const ActivationRequest& a)
{ {
os << "{"; os << "{";
os << "\"appId\":"; os << "\"productId\":";
::serialize(os, a.appId); ::serialize(os, a.productId);
os << ",\"systemParams\":"; os << ",\"systemParams\":";
::serialize(os, a.systemParams); ::serialize(os, a.systemParams);
os << ",\"licenseNumber\":"; os << ",\"licenseNumber\":";
@ -241,6 +243,8 @@ void serialize(std::ostream& os, const CheckUpdatesRequest& r)
::serialize(os, r.systemParams); ::serialize(os, r.systemParams);
os << ",\"activationId\":"; os << ",\"activationId\":";
::serialize(os, r.activationId); ::serialize(os, r.activationId);
os << ",\"productId\":";
serialize(os, r.productId);
os << ",\"moduleVersions\":"; os << ",\"moduleVersions\":";
::serialize(os, r.moduleVersions); ::serialize(os, r.moduleVersions);
os << "}"; os << "}";
@ -260,8 +264,11 @@ void deserialize(const pt::ptree& tree, CheckUpdatesResponse& r)
{ {
r.licenseFile = {}; r.licenseFile = {};
} }
if (r.success)
{
deserialize(tree.get_child("moduleUpdates"), r.moduleUpdates); deserialize(tree.get_child("moduleUpdates"), r.moduleUpdates);
} }
}
std::optional<std::string> validateLicenseKey(const std::string& licenseKey) std::optional<std::string> validateLicenseKey(const std::string& licenseKey)
@ -291,7 +298,7 @@ void LicenseClient::init()
bool LicenseClient::tryPreactivate(HTTPClient &httpClient) bool LicenseClient::tryPreactivate(HTTPClient &httpClient)
{ {
PreactivationRequest req{ appId, m_systemParams }; PreactivationRequest req{ productId, m_systemParams };
std::string jsonReq; std::string jsonReq;
{ {
@ -303,7 +310,7 @@ bool LicenseClient::tryPreactivate(HTTPClient &httpClient)
std::string jsonRes; std::string jsonRes;
{ {
std::ostringstream ss2; std::ostringstream ss2;
httpClient.postJson("http://localhost:3000/activate0", jsonReq, ss2); httpClient.postJson(serverPath + "/activate0", jsonReq, ss2);
jsonRes = ss2.str(); jsonRes = ss2.str();
} }
@ -330,11 +337,11 @@ bool LicenseClient::activate(HTTPClient &httpClient, const std::string & license
std::ostringstream ss1; std::ostringstream ss1;
ActivationRequest req{ appId, m_systemParams, licenseNumber }; ActivationRequest req{ productId, m_systemParams, licenseNumber };
serialize(ss1, req); serialize(ss1, req);
std::stringstream ss2; std::stringstream ss2;
httpClient.postJson("http://localhost:3000/activate", ss1.str() , ss2); httpClient.postJson(serverPath + "/activate", ss1.str() , ss2);
ActivationResponse activationResponse; ActivationResponse activationResponse;
pt::ptree root; pt::ptree root;
@ -356,13 +363,13 @@ std::vector<ModuleUpdate> LicenseClient::checkForUpdates(HTTPClient & httpClient
{ {
if (!isActivated()) { throw std::runtime_error("Not active"); } 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; std::ostringstream ss1;
serialize(ss1, req); serialize(ss1, req);
std::stringstream ss2; std::stringstream ss2;
httpClient.postJson("http://localhost:3000/check", ss1.str(), ss2); httpClient.postJson(serverPath + "/check", ss1.str(), ss2);
CheckUpdatesResponse checkUpdatesResponse; CheckUpdatesResponse checkUpdatesResponse;
pt::ptree root; pt::ptree root;
@ -371,7 +378,7 @@ std::vector<ModuleUpdate> LicenseClient::checkForUpdates(HTTPClient & httpClient
if (!checkUpdatesResponse.success) if (!checkUpdatesResponse.success)
{ {
throw std::exception("Could not check for updates"); throw std::runtime_error("Could not check for updates");
} }
if (checkUpdatesResponse.licenseFile) if (checkUpdatesResponse.licenseFile)
@ -581,7 +588,7 @@ bool LicenseClient::validateActivationData(const ActivationData & activationData
return false; return false;
} }
if (activationData.appId != appId) if (activationData.productId != productId)
{ {
return false; return false;
} }