YourMoneroRequests::login refactored

pull/12/head
moneroexamples 7 years ago
parent 00334cb69d
commit 3c8a812378

@ -36,36 +36,19 @@ YourMoneroRequests::YourMoneroRequests(shared_ptr<MySqlAccounts> _acc):
void
YourMoneroRequests::login(const shared_ptr<Session> session, const Bytes & body)
{
json j_request;
json j_response;
string xmr_address;
string view_key;
map<string, string> values_map{{"address" , {}}, {"view_key", {}}};
try
if (!parse_request(body, values_map, j_response))
{
j_request = body_to_json(body);
xmr_address = j_request["address"];
view_key = j_request["view_key"];
}
catch (std::exception& e)
{
cerr << "login: cant parse json: " << e.what() << endl;
j_response["status"] = "error";
j_response["reason"] = "address and/or viewkey not provided";
string response_body = j_response.dump();
auto response_headers = make_headers({{"Content-Length", to_string(response_body.size())}});
session->close(OK, response_body, response_headers);
session_close(session, j_response.dump());
return;
}
const string& xmr_address = values_map["address"];
const string& view_key = values_map["view_key"];
// make hash of the submited viewkey. we only store
// hash of viewkey in database, not acctual viewkey.
string viewkey_hash = make_hash(view_key);
@ -85,7 +68,7 @@ YourMoneroRequests::login(const shared_ptr<Session> session, const Bytes & body)
if (viewkey_hash == acc.viewkey_hash)
{
acc.viewkey = j_request["view_key"];
acc.viewkey = values_map["view_key"];
// so we have an account now. Either existing or
// newly created. Thus, we can start a tread
@ -143,12 +126,7 @@ YourMoneroRequests::login(const shared_ptr<Session> session, const Bytes & body)
}
}
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length", to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
session_close(session, j_response.dump());
}
void
@ -889,6 +867,47 @@ YourMoneroRequests::get_current_blockchain_height()
return CurrentBlockchainStatus::get_current_blockchain_height();
}
void
YourMoneroRequests::session_close(const shared_ptr< Session > session, string response_body)
{
auto response_headers = make_headers({{"Content-Length", to_string(response_body.size())}});
session->close(OK, response_body, response_headers);
}
bool
YourMoneroRequests::parse_request(
const Bytes& body,
map<string, string>& values_map,
json& j_response)
{
json j_request;
try
{
j_request = body_to_json(body);
for (const auto& kv: values_map)
{
values_map[kv.first] = j_request[kv.first];
}
return true;
}
catch (std::exception& e)
{
cerr << "YourMoneroRequests::parse_request: " << e.what() << endl;
j_response["status"] = "error";
j_response["reason"] = "reqest json parsing failed";
return false;
}
}
// define default static variables
string YourMoneroRequests::frontend_url {"http://127.0.0.1:81"};

@ -95,7 +95,9 @@ public:
generic_options_handler( const shared_ptr< Session > session );
static multimap<string, string>
make_headers(const multimap<string, string>& extra_headers = multimap<string, string>());
make_headers(
const multimap<string, string>& extra_headers
= multimap<string, string>());
static void
print_json_log(const string& text, const json& j);
@ -109,6 +111,15 @@ public:
inline uint64_t
get_current_blockchain_height();
private:
inline void
session_close(const shared_ptr< Session > session, string response_body);
bool parse_request(const Bytes& body,
map<string, string>& values_map,
json& j_response);
};

Loading…
Cancel
Save