Added: Get configuration for OAuth2 service account.
This commit is contained in:
@@ -58,7 +58,6 @@ namespace Apostol {
|
||||
CWebSocketModule::InitMethods();
|
||||
|
||||
InitServerList();
|
||||
Reload();
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -113,13 +112,13 @@ namespace Apostol {
|
||||
if (ServerList.Count() != 0) {
|
||||
CStringPairs::ConstEnumerator em(ServerList);
|
||||
while (em.MoveNext()) {
|
||||
const auto ¤t = em.Current();
|
||||
index = m_Servers.IndexOfName(current.Name());
|
||||
const auto &caCurrent = em.Current();
|
||||
index = m_Servers.IndexOfName(caCurrent.Name());
|
||||
if (index == -1) {
|
||||
index = m_Servers.AddPair(current.Name(), CClientContext(CLocation(current.Value())));
|
||||
index = m_Servers.AddPair(caCurrent.Name(), CClientContext(CLocation(caCurrent.Value())));
|
||||
}
|
||||
auto &Context = m_Servers[index].Value();
|
||||
Context.Name() = current.Name();
|
||||
Context.Name() = caCurrent.Name();
|
||||
Context.PGP().Name = "PUBLIC";
|
||||
Context.PGP().Key = Key;
|
||||
Context.BTCKeys() = Keys;
|
||||
@@ -128,41 +127,116 @@ namespace Apostol {
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
void CWebSocketModule::UpdateOAuth2() {
|
||||
const auto &oauth2 = Config()->IniFile().ReadString(CONFIG_SECTION_NAME, "oauth2", "oauth2/service.json");
|
||||
const auto &provider = CString(SYSTEM_PROVIDER_NAME);
|
||||
const auto &application = CString(SERVICE_APPLICATION_NAME);
|
||||
void CWebSocketModule::FetchOAuth2(CContext &Context) {
|
||||
|
||||
Log()->Debug(APP_LOG_DEBUG_CORE, "Trying to fetch a OAuth2 configuration file for module \"%s\" from: %s", Context.Name().c_str(), Context.URL().Origin().c_str());
|
||||
|
||||
auto OnRequest = [this, &Context](CHTTPClient *Sender, CHTTPRequest *ARequest) {
|
||||
|
||||
ARequest->ContentType = CHTTPRequest::text;
|
||||
|
||||
Apostol::PGP::CleartextSignature(
|
||||
m_pgpPrivateKey,
|
||||
m_pgpPassphrase,
|
||||
BPS_PGP_HASH,
|
||||
Context.Name(),
|
||||
ARequest->Content);
|
||||
|
||||
CHTTPRequest::Prepare(ARequest, "POST", "/api/v1/dm/service");
|
||||
|
||||
const auto& caModuleAddress = m_Module["address"];
|
||||
if (!caModuleAddress.IsEmpty())
|
||||
ARequest->AddHeader("Module-Address", caModuleAddress);
|
||||
|
||||
DebugRequest(ARequest);
|
||||
};
|
||||
|
||||
auto OnExecute = [this, &Context](CTCPConnection *AConnection) {
|
||||
|
||||
auto pConnection = dynamic_cast<CHTTPClientConnection *> (AConnection);
|
||||
|
||||
if (pConnection != nullptr) {
|
||||
auto pReply = pConnection->Reply();
|
||||
|
||||
DebugReply(pReply);
|
||||
|
||||
if (Context.Status() == Context::csPreparing) {
|
||||
if (pReply->Status == CHTTPReply::ok) {
|
||||
const CJSON Json(pReply->Content);
|
||||
Json.SaveToFile(m_OAuth2.c_str());
|
||||
UpdateOAuth2(m_OAuth2);
|
||||
} else {
|
||||
Context.SetStatus(Context::csInitialization);
|
||||
}
|
||||
}
|
||||
|
||||
pConnection->CloseConnection(true);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
auto OnException = [&Context](CTCPConnection *AConnection, const Delphi::Exception::Exception &E) {
|
||||
auto pConnection = dynamic_cast<CHTTPClientConnection *> (AConnection);
|
||||
if (pConnection != nullptr) {
|
||||
auto pClient = dynamic_cast<CHTTPClient *> (pConnection->Client());
|
||||
if (pClient != nullptr) {
|
||||
Log()->Error(APP_LOG_EMERG, 0, "[%s:%d] %s", pClient->Host().c_str(), pClient->Port(), E.what());
|
||||
}
|
||||
DebugReply(pConnection->Reply());
|
||||
}
|
||||
|
||||
Context.SetStatus(Context::csInitialization);
|
||||
};
|
||||
|
||||
Context.SetStatus(Context::csPreparing);
|
||||
|
||||
auto pClient = GetClient(Context.URL().hostname, Context.URL().port == 0 ? BPS_SERVER_PORT : Context.URL().port);
|
||||
|
||||
pClient->OnRequest(OnRequest);
|
||||
pClient->OnExecute(OnExecute);
|
||||
pClient->OnException(OnException);
|
||||
|
||||
pClient->AutoFree(true);
|
||||
pClient->Active(true);
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
void CWebSocketModule::UpdateOAuth2(const CString &FileName) {
|
||||
for (int i = 0; i < m_Servers.Count(); i++) {
|
||||
auto &Context = m_Servers[i].Value();
|
||||
if (!oauth2.empty() && Context.Status() == Context::csInitialization) {
|
||||
LoadOAuth2(oauth2, provider, application, Context.Providers());
|
||||
Context.SetStatus(Context::csInitialized);
|
||||
Context.SetCheckDate(0);
|
||||
if (Context.Status() == Context::csInitialization || Context.Status() == Context::csPreparing) {
|
||||
if (LoadOAuth2(FileName, Context.Providers())) {
|
||||
Context.SetStatus(Context::csInitialized);
|
||||
Context.SetCheckDate(0);
|
||||
} else {
|
||||
if (Context.Status() != Context::csPreparing) {
|
||||
FetchOAuth2(Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
void CWebSocketModule::LoadOAuth2(const CString &FileName, const CString &ProviderName, const CString &ApplicationName, CProviders &Providers) {
|
||||
CString ConfigFile(FileName);
|
||||
bool CWebSocketModule::LoadOAuth2(const CString &FileName, CProviders &Providers) {
|
||||
const auto &caProviderName = CString(SYSTEM_PROVIDER_NAME);
|
||||
const auto &caApplicationName = CString(SERVICE_APPLICATION_NAME);
|
||||
|
||||
if (!path_separator(ConfigFile.front())) {
|
||||
ConfigFile = Config()->Prefix() + ConfigFile;
|
||||
}
|
||||
|
||||
if (FileExists(ConfigFile.c_str())) {
|
||||
if (FileExists(FileName.c_str())) {
|
||||
CJSONObject Json;
|
||||
Json.LoadFromFile(ConfigFile.c_str());
|
||||
Json.LoadFromFile(FileName.c_str());
|
||||
|
||||
int index = Providers.IndexOfName(ProviderName);
|
||||
int index = Providers.IndexOfName(caProviderName);
|
||||
if (index == -1)
|
||||
index = Providers.AddPair(ProviderName, CProvider(ProviderName));
|
||||
index = Providers.AddPair(caProviderName, CProvider(caProviderName));
|
||||
auto& Provider = Providers[index].Value();
|
||||
Provider.Applications().AddPair(ApplicationName, Json);
|
||||
} else {
|
||||
Log()->Error(APP_LOG_WARN, 0, APP_FILE_NOT_FOUND, ConfigFile.c_str());
|
||||
Provider.Applications().AddPair(caApplicationName, Json);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -511,7 +585,7 @@ namespace Apostol {
|
||||
|
||||
if (Response.Payload.HasOwnProperty("data")) {
|
||||
UpdateServerList(Response.Payload["data"].AsString());
|
||||
UpdateOAuth2();
|
||||
UpdateOAuth2(m_OAuth2);
|
||||
}
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
@@ -731,21 +805,12 @@ namespace Apostol {
|
||||
caClearText = pServerRequest->Content;
|
||||
}
|
||||
|
||||
const auto& caPGPPrivateFile = Config()->IniFile().ReadString("pgp", "private", "");
|
||||
const auto& caPGPPassphrase = Config()->IniFile().ReadString("pgp", "passphrase", "");
|
||||
|
||||
if (!FileExists(caPGPPrivateFile.c_str()))
|
||||
throw Delphi::Exception::Exception("PGP: Private key file not opened.");
|
||||
|
||||
CString sPGPPrivate;
|
||||
sPGPPrivate.LoadFromFile(caPGPPrivateFile.c_str());
|
||||
|
||||
if (pgpValue == "off" || pgpValue == "false") {
|
||||
sPayload = caClearText.Text();
|
||||
} else {
|
||||
Apostol::PGP::CleartextSignature(
|
||||
sPGPPrivate,
|
||||
caPGPPassphrase,
|
||||
m_pgpPrivateKey,
|
||||
m_pgpPassphrase,
|
||||
BPS_PGP_HASH,
|
||||
caClearText.Text(),
|
||||
sPayload);
|
||||
@@ -1156,23 +1221,14 @@ namespace Apostol {
|
||||
|
||||
CheckDeal(Deal);
|
||||
|
||||
const auto& caPGPPrivateFile = Config()->IniFile().ReadString("pgp", "private", "");
|
||||
const auto& caPGPPassphrase = Config()->IniFile().ReadString("pgp", "passphrase", "");
|
||||
|
||||
if (!FileExists(caPGPPrivateFile.c_str()))
|
||||
throw Delphi::Exception::Exception("PGP: Private key file not opened.");
|
||||
|
||||
CString sPGPPrivate;
|
||||
sPGPPrivate.LoadFromFile(caPGPPrivateFile.c_str());
|
||||
|
||||
const CString caClearText(YAML::Dump(Node));
|
||||
|
||||
if (pgpValue == "off" || pgpValue == "false") {
|
||||
sPayload = caClearText;
|
||||
} else {
|
||||
Apostol::PGP::CleartextSignature(
|
||||
sPGPPrivate,
|
||||
caPGPPassphrase,
|
||||
m_pgpPrivateKey,
|
||||
m_pgpPassphrase,
|
||||
BPS_PGP_HASH,
|
||||
caClearText,
|
||||
sPayload);
|
||||
@@ -1418,6 +1474,11 @@ namespace Apostol {
|
||||
for (int i = 0; i < m_Servers.Count(); i++) {
|
||||
auto &Context = m_Servers[i].Value();
|
||||
|
||||
if ((Now >= Context.CheckDate()) && (Context.Status() == Context::csInitialization)) {
|
||||
Context.SetCheckDate(Now + (CDateTime) 30 / SecsPerDay); // 30 sec
|
||||
FetchOAuth2(Context);
|
||||
}
|
||||
|
||||
if ((Now >= Context.CheckDate()) && (Context.Status() >= Context::csInitialized)) {
|
||||
Context.SetCheckDate(Now + (CDateTime) 30 / SecsPerDay); // 30 sec
|
||||
|
||||
@@ -1429,15 +1490,16 @@ namespace Apostol {
|
||||
}
|
||||
|
||||
if (Context.Status() == Context::csAuthorized) {
|
||||
if ((Now >= Context.FixedDate())) {
|
||||
if (Now >= Context.FixedDate()) {
|
||||
Context.SetFixedDate(Now + (CDateTime) 30 / SecsPerDay); // 30 sec
|
||||
Context.SetStatus(Context::csInProgress);
|
||||
|
||||
CreateWebSocketClient(Context);
|
||||
}
|
||||
}
|
||||
|
||||
if (Context.Status() == Context::csRunning) {
|
||||
if ((Now >= Context.FixedDate())) {
|
||||
if (Now >= Context.FixedDate()) {
|
||||
Context.SetFixedDate(Now + (CDateTime) 30 / SecsPerDay); // 30 sec
|
||||
|
||||
for (int j = 0; j < Context.ClientManager().Count(); ++j) {
|
||||
@@ -1460,16 +1522,39 @@ namespace Apostol {
|
||||
void CWebSocketModule::Reload() {
|
||||
Config()->IniFile().ReadSectionValues("module", &m_Module);
|
||||
|
||||
m_OAuth2 = Config()->IniFile().ReadString(CONFIG_SECTION_NAME, "oauth2", "oauth2/service.json");
|
||||
if (!path_separator(m_OAuth2.front())) {
|
||||
m_OAuth2 = Config()->Prefix() + m_OAuth2;
|
||||
}
|
||||
|
||||
const auto& caPrivateKey = Config()->IniFile().ReadString("pgp", "private", "module.sec");
|
||||
const auto& caPublicKey = Config()->IniFile().ReadString("pgp", "public", "dm.pub");
|
||||
|
||||
if (FileExists(caPublicKey.c_str())) {
|
||||
CString Key;
|
||||
Key.LoadFromFile(caPublicKey.c_str());
|
||||
m_pgpPassphrase = Config()->IniFile().ReadString("pgp", "passphrase", "");
|
||||
|
||||
UpdateServerList(Key);
|
||||
UpdateOAuth2();
|
||||
if (FileExists(caPrivateKey.c_str())) {
|
||||
m_pgpPrivateKey.LoadFromFile(caPrivateKey.c_str());
|
||||
|
||||
if (FileExists(caPublicKey.c_str())) {
|
||||
CString Key;
|
||||
Key.LoadFromFile(caPublicKey.c_str());
|
||||
|
||||
UpdateServerList(Key);
|
||||
UpdateOAuth2(m_OAuth2);
|
||||
} else {
|
||||
Log()->Error(APP_LOG_WARN, 0, APP_FILE_NOT_FOUND, caPublicKey.c_str());
|
||||
}
|
||||
} else {
|
||||
Log()->Error(APP_LOG_WARN, 0, APP_FILE_NOT_FOUND, caPublicKey.c_str());
|
||||
Log()->Error(APP_LOG_WARN, 0, APP_FILE_NOT_FOUND, caPrivateKey.c_str());
|
||||
}
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
void CWebSocketModule::Initialization(CModuleProcess *AProcess) {
|
||||
CApostolModule::Initialization(AProcess);
|
||||
|
||||
if (Enabled()) {
|
||||
Reload();
|
||||
}
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user