337{
338 wxString str_input = evt.GetString();
339
340 try {
342 wxString strCmd = j["command"];
343
345
346
349 try {
350 json cfg = json::parse(login_cmd);
351 if (cfg.contains("pkce")) {
352 const auto& pkce = cfg["pkce"];
353 if (pkce.contains("loopback_port")) {
354 if (pkce["loopback_port"].is_number_integer()) {
356 } else if (pkce["loopback_port"].is_string()) {
358 }
359 }
360
361 if (
m_loopback_port <= 0 && pkce.contains(
"redirect_uri") && pkce[
"redirect_uri"].is_string()) {
362 const std::string redirect_uri = pkce["redirect_uri"].get<std::string>();
363 const char* prefixes[] = {"localhost:", "127.0.0.1:"};
364 for (const char* prefix : prefixes) {
365 auto start = redirect_uri.find(prefix);
366 if (start == std::string::npos)
367 continue;
368 start += strlen(prefix);
369 auto end = redirect_uri.find(
'/', start);
370 std::string port_str = redirect_uri.substr(start, end - start);
371 try {
373 } catch (...) {
375 }
376 break;
377 }
378 }
379 }
380 } catch (...) {
382 }
383 wxString str_js = wxString::FromUTF8("window.postMessage(") + wxString::FromUTF8(login_cmd.c_str()) +
384 wxString::FromUTF8(", '*')");
386 return;
387 }
388
389 if (strCmd == "autotest_token")
390 {
392 }
393 if (strCmd == "user_ticket_login") {
394 auto* agent = wxGetApp().getAgent();
395 if (!agent || !
m_cloud_agent || !j.contains(
"data") || !j[
"data"].is_object() || !j[
"data"].contains(
"ticket")) {
396 wxMessageBox(
_L(
"Login failed. Please try again."),
_L(
"Login"), wxICON_WARNING);
397 return;
398 }
399
401 std::string ticket = j["data"]["ticket"].get<std::string>();
402
403 unsigned int token_http_code = 0;
404 std::string token_body;
405 int token_result = agent->get_my_token(ticket, &token_http_code, &token_body, provider);
406 if (token_result != 0) {
407 BOOST_LOG_TRIVIAL(warning) << "embedded_login: get_my_token failed, http_code=" << token_http_code;
408 wxMessageBox(
_L(
"Login failed. Please try again."),
_L(
"Login"), wxICON_WARNING);
409 return;
410 }
411
412 std::string access_token;
413 std::string refresh_token;
414 std::string expires_in_str;
415 std::string refresh_expires_in_str;
416 try {
417 json token_j = json::parse(token_body);
418 if (token_j.contains("accessToken"))
419 access_token = token_j["accessToken"].get<std::string>();
420 if (token_j.contains("refreshToken"))
421 refresh_token = token_j["refreshToken"].get<std::string>();
422 if (token_j.contains("expiresIn"))
423 expires_in_str = std::to_string(token_j["expiresIn"].get<double>());
424 if (token_j.contains("refreshExpiresIn"))
425 refresh_expires_in_str = std::to_string(token_j["refreshExpiresIn"].get<double>());
426 } catch (...) {
427 wxMessageBox(
_L(
"Login failed. Please try again."),
_L(
"Login"), wxICON_WARNING);
428 return;
429 }
430
431 if (access_token.empty()) {
432 wxMessageBox(
_L(
"Login failed. Please try again."),
_L(
"Login"), wxICON_WARNING);
433 return;
434 }
435
436 unsigned int profile_http_code = 0;
437 std::string profile_body;
438 int profile_result = agent->get_my_profile(access_token, &profile_http_code, &profile_body, provider);
439 if (profile_result != 0) {
440 BOOST_LOG_TRIVIAL(warning) << "embedded_login: get_my_profile failed, http_code=" << profile_http_code;
441 wxMessageBox(
_L(
"Login failed. Please try again."),
_L(
"Login"), wxICON_WARNING);
442 return;
443 }
444
445 std::string user_id;
446 std::string user_name;
447 std::string user_account;
448 std::string user_avatar;
449 try {
450 json user_j = json::parse(profile_body);
451 if (user_j.contains("uidStr"))
452 user_id = user_j["uidStr"].get<std::string>();
453 if (user_j.contains("name"))
454 user_name = user_j["name"].get<std::string>();
455 if (user_j.contains("avatar"))
456 user_avatar = user_j["avatar"].get<std::string>();
457 if (user_j.contains("account"))
458 user_account = user_j["account"].get<std::string>();
459 } catch (...) {
460 BOOST_LOG_TRIVIAL(warning) << "embedded_login: profile JSON parse failed";
461 }
462
464 login_j["command"] = "user_login";
466 login_j["data"]["refresh_token"] = refresh_token;
467 login_j["data"]["token"] = access_token;
468 login_j["data"]["expires_in"] = expires_in_str;
469 login_j["data"]["refresh_expires_in"] = refresh_expires_in_str;
470 login_j["data"]["user"]["uid"] = user_id;
471 login_j["data"]["user"]["name"] = user_name;
472 login_j["data"]["user"]["account"] = user_account;
473 login_j["data"]["user"]["avatar"] = user_avatar;
474 std::string message_json = login_j.dump();
475
476
477 EndModal(wxID_OK);
478
479
480
481 wxTheApp->CallAfter([message_json, provider]() { wxGetApp().handle_script_message(message_json, provider); });
482 }
483 else if (strCmd == "user_login") {
485 std::string message_json = j.dump();
486
487
488 EndModal(wxID_OK);
489
490
491
493 wxTheApp->CallAfter([message_json, provider]() { wxGetApp().handle_script_message(message_json, provider); });
494 }
495 else if (strCmd == "get_localhost_url") {
497 std::string sequence_id = j["sequence_id"].get<std::string>();
498 CallAfter([this, sequence_id] {
500 ack_j["command"] = "get_localhost_url";
502 ack_j[
"response"][
"base_url"] = std::string(
LOCALHOST_URL) + std::to_string(loopback_port);
503 ack_j["response"]["result"] = "success";
504 ack_j["sequence_id"] = sequence_id;
505 wxString str_js = wxString::Format("window.postMessage(%s)", ack_j.dump());
507 });
508 }
509 else if (strCmd == "thirdparty_login") {
511 std::string jump_url = j["data"]["url"].get<std::string>();
514 CallAfter([jump_url] {
515 wxString url = wxString::FromUTF8(jump_url);
516 wxLaunchDefaultBrowser(url);
517 });
518 }
519 }
520 else if (strCmd == "new_webpage") {
522 std::string jump_url = j["data"]["url"].get<std::string>();
523 CallAfter([jump_url] {
524 wxString url = wxString::FromUTF8(jump_url);
525 wxLaunchDefaultBrowser(url);
526 });
527 }
528 return;
529 }
530 } catch (std::exception &e) {
531 wxMessageBox(e.what(),
_L(
"parse json failed"), wxICON_WARNING);
532 Close();
533 }
534}
#define LOCALHOST_URL
Definition HttpServer.hpp:26
int ensure_loopback_port()
Definition WebUserLoginDialog.cpp:100
std::string m_AutotestToken
Definition WebUserLoginDialog.hpp:80
void RunScript(const wxString &javascript)
Definition WebUserLoginDialog.cpp:536
T * end(Slic3r::Mat< N, M, T > &mat)
Definition Point.hpp:841
std::string rewrite_loopback_url(std::string url, int port)
Definition WebUserLoginDialog.cpp:81
std::string into_u8(const wxString &str)
Definition GUI.cpp:549
bool contains(const std::vector< T > &vector, const Point &point)
Definition Geometry.cpp:55
nlohmann::json json
Definition AssembleList.cpp:42