Skip to content
Snippets Groups Projects
Commit 11f7753f authored by Adrien Béraud's avatar Adrien Béraud
Browse files

account manager: avoid blocking on CSR generation

Change-Id: I2457c29fce6c54295aba9ebe6b059f066a6f80a0
parent 1fc82d4d
Branches
No related tags found
No related merge requests found
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <opendht/http.h> #include <opendht/http.h>
#include <opendht/log.h> #include <opendht/log.h>
#include <opendht/thread_pool.h>
#include "manager.h" #include "manager.h"
...@@ -77,13 +78,17 @@ ServerAccountManager::initAuthentication( ...@@ -77,13 +78,17 @@ ServerAccountManager::initAuthentication(
} }
onChange_ = std::move(onChange); onChange_ = std::move(onChange);
const std::string url = managerHostname_ + PATH_DEVICE; const std::string url = managerHostname_ + PATH_DEVICE;
JAMI_WARN("[Auth] authentication with: %s to %s", ctx->credentials->username.c_str(), url.c_str()); JAMI_WARN("[Auth] authentication with: %s to %s", ctx->credentials->username.c_str(), url.c_str());
auto request = std::make_shared<Request>(*Manager::instance().ioContext(), url, logger_); auto request = std::make_shared<Request>(*Manager::instance().ioContext(), url, logger_);
auto reqid = request->id(); auto reqid = request->id();
request->set_method(restinio::http_method_post()); request->set_method(restinio::http_method_post());
request->set_auth(ctx->credentials->username, ctx->credentials->password); request->set_auth(ctx->credentials->username, ctx->credentials->password);
requests_[reqid] = request;
dht::ThreadPool::computation().run([onAsync = onAsync_, ctx, request, reqid]{
onAsync([ctx, request, reqid, onAsync](AccountManager& accountManager){
auto& this_ = *static_cast<ServerAccountManager*>(&accountManager);
{ {
std::stringstream ss; std::stringstream ss;
auto csr = ctx->request.get()->toString(); auto csr = ctx->request.get()->toString();
...@@ -93,20 +98,20 @@ ServerAccountManager::initAuthentication( ...@@ -93,20 +98,20 @@ ServerAccountManager::initAuthentication(
JAMI_WARN("[Auth] Sending request: %s", csr.c_str()); JAMI_WARN("[Auth] Sending request: %s", csr.c_str());
request->set_body(ss.str()); request->set_body(ss.str());
} }
setHeaderFields(*request); this_.setHeaderFields(*request);
request->add_on_state_change_callback([reqid, ctx, onAsync = onAsync_] request->add_on_state_change_callback([reqid, request, ctx, onAsync]
(Request::State state, const dht::http::Response& response){ (Request::State state, const dht::http::Response& response){
JAMI_DBG("[Auth] Got request callback with state=%d", (int)state); JAMI_DBG("[Auth] Got request callback with state=%d", (int)state);
if (state != Request::State::DONE) if (state != Request::State::DONE)
return; return;
if (response.status_code == 0) { if (response.status_code == 0)
ctx->onFailure(AuthError::SERVER_ERROR, "Invalid server provided"); ctx->onFailure(AuthError::SERVER_ERROR, "Can't connect to server");
return; else if (response.status_code >= 400 && response.status_code < 500)
} else if (response.status_code >= 400 && response.status_code < 500)
ctx->onFailure(AuthError::INVALID_ARGUMENTS, ""); ctx->onFailure(AuthError::INVALID_ARGUMENTS, "");
else if (response.status_code < 200 || response.status_code > 299) else if (response.status_code < 200 || response.status_code > 299)
ctx->onFailure(AuthError::INVALID_ARGUMENTS, ""); ctx->onFailure(AuthError::INVALID_ARGUMENTS, "");
else { else {
do {
try { try {
Json::Value json; Json::Value json;
std::string err; std::string err;
...@@ -115,7 +120,7 @@ ServerAccountManager::initAuthentication( ...@@ -115,7 +120,7 @@ ServerAccountManager::initAuthentication(
if (!reader->parse(response.body.data(), response.body.data() + response.body.size(), &json, &err)){ if (!reader->parse(response.body.data(), response.body.data() + response.body.size(), &json, &err)){
JAMI_ERR("[Auth] Can't parse server response: %s", err.c_str()); JAMI_ERR("[Auth] Can't parse server response: %s", err.c_str());
ctx->onFailure(AuthError::SERVER_ERROR, "Can't parse server response"); ctx->onFailure(AuthError::SERVER_ERROR, "Can't parse server response");
return; break;
} }
JAMI_WARN("[Auth] Got server response: %s", response.body.c_str()); JAMI_WARN("[Auth] Got server response: %s", response.body.c_str());
...@@ -128,7 +133,7 @@ ServerAccountManager::initAuthentication( ...@@ -128,7 +133,7 @@ ServerAccountManager::initAuthentication(
if (not accountCert) { if (not accountCert) {
JAMI_ERR("[Auth] Can't parse certificate: no issuer"); JAMI_ERR("[Auth] Can't parse certificate: no issuer");
ctx->onFailure(AuthError::SERVER_ERROR, "Invalid certificate from server"); ctx->onFailure(AuthError::SERVER_ERROR, "Invalid certificate from server");
return; break;
} }
auto receipt = json["deviceReceipt"].asString(); auto receipt = json["deviceReceipt"].asString();
Json::Value receiptJson; Json::Value receiptJson;
...@@ -136,7 +141,7 @@ ServerAccountManager::initAuthentication( ...@@ -136,7 +141,7 @@ ServerAccountManager::initAuthentication(
if (!receiptReader->parse(receipt.data(), receipt.data() + receipt.size(), &receiptJson, &err)){ if (!receiptReader->parse(receipt.data(), receipt.data() + receipt.size(), &receiptJson, &err)){
JAMI_ERR("[Auth] Can't parse receipt from server: %s", err.c_str()); JAMI_ERR("[Auth] Can't parse receipt from server: %s", err.c_str());
ctx->onFailure(AuthError::SERVER_ERROR, "Can't parse receipt from server"); ctx->onFailure(AuthError::SERVER_ERROR, "Can't parse receipt from server");
return; break;
} }
onAsync([reqid, ctx, onAsync([reqid, ctx,
json=std::move(json), json=std::move(json),
...@@ -183,17 +188,22 @@ ServerAccountManager::initAuthentication( ...@@ -183,17 +188,22 @@ ServerAccountManager::initAuthentication(
ctx->onSuccess(*this_.info_, std::move(config), std::move(receipt), std::move(receiptSignature)); ctx->onSuccess(*this_.info_, std::move(config), std::move(receipt), std::move(receiptSignature));
this_.syncDevices(); this_.syncDevices();
this_.requests_.erase(reqid);
}); });
} }
catch (const std::exception& e) { catch (const std::exception& e) {
JAMI_ERR("Error when loading account: %s", e.what()); JAMI_ERR("Error when loading account: %s", e.what());
ctx->onFailure(AuthError::NETWORK, ""); ctx->onFailure(AuthError::NETWORK, "");
} }
} while (false);
} }
onAsync([reqid](AccountManager& accountManager){
auto& this_ = *static_cast<ServerAccountManager*>(&accountManager);
this_.requests_.erase(reqid);
});
}); });
request->send(); request->send();
requests_[reqid] = std::move(request); });
});
} }
void void
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment