-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/auth builder #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ with this program. If not, see <https://www.gnu.org/licenses/> | |
#include <util/platform.h> | ||
#include <nlohmann/json.hpp> | ||
#include <QDir> | ||
#include <sstream> | ||
|
||
using json = nlohmann::json; | ||
|
||
|
@@ -64,13 +65,18 @@ MarketplaceApi::MarketplaceApi() | |
|
||
void MarketplaceApi::logOut() | ||
{ | ||
std::string url = _authUrl + "/auth/realms/mp/protocol/openid-connect/logout"; | ||
std::string url = getAuthUrl(logoutEndpointSegments, {}); | ||
|
||
auto ec = GetElgatoCloud(); | ||
auto refreshToken = ec->GetRefreshToken(); | ||
auto accessToken = ec->GetAccessToken(); | ||
if (refreshToken != "") { | ||
std::string postBody = "{\"client_id\": \"elgatolink\", \"refresh_token\": \"" + refreshToken + "\" }"; | ||
auto resp = fetch_string_from_post(url, postBody, accessToken); | ||
std::map<std::string, std::string> params = { | ||
{ID_KEY, ID}, | ||
{REFRESH_KEY, refreshToken} | ||
}; | ||
std::string postData = postBody(params); | ||
auto resp = fetch_string_from_post(url, postData, accessToken); | ||
} | ||
_loggedIn = false; | ||
_hasAvatar = false; | ||
|
@@ -92,6 +98,43 @@ MarketplaceApi *MarketplaceApi::getInstance() | |
return _api; | ||
} | ||
|
||
std::string MarketplaceApi::getAuthUrl( | ||
std::vector<std::string> const& segments, | ||
std::map<std::string, std::string> const& queryParams | ||
) | ||
{ | ||
std::string endpoint = segments.size() > 0 ? std::accumulate(std::next(segments.begin()), segments.end(), segments[0], | ||
[&](std::string a, std::string b) { return a + "/" + b; }) : ""; | ||
|
||
auto qString = queryString(queryParams); | ||
|
||
if (!qString.empty()) { | ||
qString = "?" + qString; | ||
} | ||
|
||
std::string url = _authUrl + "/" + endpoint + qString; | ||
return url; | ||
} | ||
|
||
std::string MarketplaceApi::getGatewayUrl( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there future logic that is planned which will make this function materially different from getAuthUrl? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially yes, as the calls to the gateway API have various standard parameters such as limits/offsets. My thought is to standardize this in the future for easier/more clear calls to the various API endpoints versus the calls done to the OAuth process |
||
std::vector<std::string> const& segments, | ||
std::map<std::string, std::string> const& queryParams | ||
) | ||
{ | ||
std::string endpoint = segments.size() > 0 ? std::accumulate(std::next(segments.begin()), segments.end(), segments[0], | ||
[&](std::string a, std::string b) { return a + "/" + b; }) : ""; | ||
|
||
auto qString = queryString(queryParams); | ||
|
||
if (!qString.empty()) { | ||
qString = "?" + qString; | ||
} | ||
|
||
std::string url = _gatewayUrl + "/" + endpoint + qString; | ||
return url; | ||
} | ||
|
||
|
||
void MarketplaceApi::setUserDetails(nlohmann::json &data) | ||
{ | ||
_hasAvatar = false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is technically not "bad" code, but I prefer when algorithms like this are spun out into a well named helper function; especially when it's a pattern used in multiple places for a relatively well understood high level operation like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noted this, and will revisit.