OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::OAuthJob Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Jobs/OAuthJob.hpp>

Inheritance diagram for Slic3r::GUI::OAuthJob:
Collaboration diagram for Slic3r::GUI::OAuthJob:

Public Types

enum  JobPrepareState { PREPARE_STATE_DEFAULT = 0 , PREPARE_STATE_MENU = 1 }

Public Member Functions

 OAuthJob (const OAuthData &input)
void process (Ctl &ctl) override
void finalize (bool canceled, std::exception_ptr &e) override
void set_event_handle (wxWindow *hanle)

Static Public Member Functions

static void parse_token_response (const std::string &body, bool error, OAuthResult &result)

Private Attributes

HttpServer local_authorization_server
OAuthData _data
wxWindow * m_event_handle {nullptr}

Detailed Description

Member Enumeration Documentation

◆ JobPrepareState

Enumerator
PREPARE_STATE_DEFAULT 
PREPARE_STATE_MENU 
22 {
25 };
@ PREPARE_STATE_MENU
Definition Job.hpp:24
@ PREPARE_STATE_DEFAULT
Definition Job.hpp:23

Constructor & Destructor Documentation

◆ OAuthJob()

Slic3r::GUI::OAuthJob::OAuthJob ( const OAuthData & input)
explicit
23: local_authorization_server(input.params.callback_port), _data(input) {}
HttpServer local_authorization_server
Definition OAuthJob.hpp:53
OAuthData _data
Definition OAuthJob.hpp:54

References _data, and local_authorization_server.

Member Function Documentation

◆ finalize()

void Slic3r::GUI::OAuthJob::finalize ( bool canceled,
std::exception_ptr & e )
overridevirtual

Reimplemented from Slic3r::GUI::Job.

124{
125 // Make sure it's stopped
127
128 wxCommandEvent event(EVT_OAUTH_COMPLETE_MESSAGE);
129 event.SetEventObject(m_event_handle);
130 wxPostEvent(m_event_handle, event);
131}
wxWindow * m_event_handle
Definition OAuthJob.hpp:55

References local_authorization_server, and m_event_handle.

◆ parse_token_response()

void Slic3r::GUI::OAuthJob::parse_token_response ( const std::string & body,
bool error,
OAuthResult & result )
static
26{
27 const auto j = nlohmann::json::parse(body, nullptr, false, true);
28 if (j.is_discarded()) {
29 BOOST_LOG_TRIVIAL(warning) << "Invalid or no JSON data on token response: " << body;
30 result.error_message = _u8L("Unknown error");
31 } else if (error) {
32 if (j.contains("error_description")) {
33 j.at("error_description").get_to(result.error_message);
34 } else {
35 result.error_message = _u8L("Unknown error");
36 }
37 } else {
38 j.at("access_token").get_to(result.access_token);
39 j.at("refresh_token").get_to(result.refresh_token);
40 result.success = true;
41 }
42}
#define _u8L(s)
Definition I18N.hpp:11

References _u8L, Slic3r::GUI::OAuthResult::access_token, Slic3r::GUI::OAuthResult::error_message, Slic3r::GUI::OAuthResult::refresh_token, and Slic3r::GUI::OAuthResult::success.

Referenced by Slic3r::SimplyPrint::do_api_call(), and process().

Here is the caller graph for this function:

◆ process()

void Slic3r::GUI::OAuthJob::process ( Ctl & ctl)
overridevirtual

Implements Slic3r::GUI::Job.

46{
47 // Prepare auth process
48 std::shared_ptr<ThreadSafeQueueSPSC<OAuthResult>> queue = std::make_shared<ThreadSafeQueueSPSC<OAuthResult>>();
49
50 // Setup auth server to receive OAuth code from callback url
51 local_authorization_server.set_request_handler([this, queue](const std::string& url) -> std::shared_ptr<HttpServer::Response> {
52 if (boost::contains(url, "/callback")) {
53 const auto code = url_get_param(url, "code");
54 const auto state = url_get_param(url, "state");
55
56 const auto handle_auth_fail = [this, queue](const std::string& message) -> std::shared_ptr<HttpServer::ResponseRedirect> {
57 queue->push(OAuthResult{false, message});
58 return std::make_shared<HttpServer::ResponseRedirect>(this->_data.params.auth_fail_redirect_url);
59 };
60
61 if (state != _data.params.state) {
62 BOOST_LOG_TRIVIAL(warning) << "The provided state was not correct. Got " << state << " and expected " << _data.params.state;
63 return handle_auth_fail(_u8L("The provided state is not correct."));
64 }
65
66 if (code.empty()) {
67 const auto error_code = url_get_param(url, "error_code");
68 if (error_code == "user_denied") {
69 BOOST_LOG_TRIVIAL(debug) << "User did not give the required permission when authorizing this application";
70 return handle_auth_fail(_u8L("Please give the required permissions when authorizing this application."));
71 }
72
73 BOOST_LOG_TRIVIAL(warning) << "Unexpected error when logging in. Error_code: " << error_code << ", State: " << state;
74 return handle_auth_fail(_u8L("Something unexpected happened when trying to log in, please try again."));
75 }
76
77
78 OAuthResult r;
79 // Request the access token from the authorization server.
80 auto http = Http::post(_data.params.token_url);
81 http.timeout_connect(5)
82 .timeout_max(5)
83 .form_add("client_id", _data.params.client_id)
84 .form_add("redirect_uri", _data.params.callback_url)
85 .form_add("grant_type", "authorization_code")
86 .form_add("code", code)
87 .form_add("code_verifier", _data.params.verification_code)
88 .form_add("scope", _data.params.scope)
89 .on_complete([&](std::string body, unsigned status) { parse_token_response(body, false, r); })
90 .on_error([&](std::string body, std::string error, unsigned status) { parse_token_response(body, true, r); })
91 .perform_sync();
92
93 queue->push(r);
94 return std::make_shared<HttpServer::ResponseRedirect>(r.success ? _data.params.auth_success_redirect_url :
95 _data.params.auth_fail_redirect_url);
96 } else {
97 queue->push(OAuthResult{false});
98 return std::make_shared<HttpServer::ResponseNotFound>();
99 }
100 });
101
102 // Run the local server
104
105 // Wait until we received the result
106 bool received = false;
107 while (!ctl.was_canceled() && !received ) {
108 queue->consume_one(BlockingWait{1000}, [this, &received](const OAuthResult& result) {
109 *_data.result = result;
110 received = true;
111 });
112 }
113
114 // Handle timeout
115 if (!received && ctl.was_canceled()) {
116 _data.result->error_message = _u8L("User canceled.");
117 } else {
118 // Wait a while to ensure the response has sent
119 std::this_thread::sleep_for(std::chrono::milliseconds(1500));
120 }
121}
static void parse_token_response(const std::string &body, bool error, OAuthResult &result)
Definition OAuthJob.cpp:25
static Http post(std::string url)
Definition Http.cpp:891
std::string url_get_param(const std::string &url, const std::string &key)
Definition HttpServer.cpp:35
@ http
Definition IPrinterAgent.hpp:64

References _data, _u8L, Slic3r::http, local_authorization_server, parse_token_response(), Slic3r::Http::post(), Slic3r::GUI::OAuthResult::success, Slic3r::GUI::url_get_param(), and Slic3r::GUI::Job::Ctl::was_canceled().

Here is the call graph for this function:

◆ set_event_handle()

void Slic3r::GUI::OAuthJob::set_event_handle ( wxWindow * hanle)
inline
63{ m_event_handle = hanle; }

References m_event_handle.

Member Data Documentation

◆ _data

OAuthData Slic3r::GUI::OAuthJob::_data
private

Referenced by OAuthJob(), and process().

◆ local_authorization_server

HttpServer Slic3r::GUI::OAuthJob::local_authorization_server
private

Referenced by OAuthJob(), finalize(), and process().

◆ m_event_handle

wxWindow* Slic3r::GUI::OAuthJob::m_event_handle {nullptr}
private
55{nullptr};

Referenced by finalize(), and set_event_handle().


The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Jobs/OAuthJob.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Jobs/OAuthJob.cpp