Skip to content

Commit

Permalink
UI: Add OAuth token invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
derrod committed Jun 30, 2023
1 parent fe96911 commit e505bb8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
61 changes: 61 additions & 0 deletions UI/auth-oauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,67 @@ try {
return false;
}

bool OAuth::InvalidateToken(const char *url)
{
return InvalidateTokenInternal(url, "", true);
}

bool OAuth::InvalidateToken(const char *url, const std::string &client_id)
{
return InvalidateTokenInternal(url, client_id);
}

bool OAuth::InvalidateTokenInternal(const char *base_url,
const std::string &client_id,
const bool token_as_parameter)
try {
std::string url(base_url);
std::string output;
std::string error;
std::string desc;
std::string post_data;

if (token.empty()) {
return true;
}

/* Google wants the token as a parameter, but still wants us to POST... */
if (token_as_parameter) {
url += "?token=" + token;
} else {
post_data += "token=" + token;
}

/* Only required for Twitch as far as I can tell */
if (!client_id.empty()) {
post_data += "&client_id=" + client_id;
}

bool success = false;

auto func = [&]() {
success = GetRemoteFile(url.c_str(), output, error, nullptr,
"application/x-www-form-urlencoded",
"POST", post_data.c_str(),
std::vector<std::string>(), nullptr, 5,
false);
};

ExecThreadedWithoutBlocking(func, QTStr("Auth.Revoking.Title"),
QTStr("Auth.Revoking.Text").arg(service()));
if (!success)
throw ErrorInfo("Failed to revoke token", error);

/* We don't really care about the result here, just assume it either
* succeeded or didn't matter. */
return true;

} catch (ErrorInfo &info) {
blog(LOG_WARNING, "%s: %s: %s", __FUNCTION__, info.message.c_str(),
info.error.c_str());
return false;
}

void OAuthStreamKey::OnStreamConfig()
{
if (key_.empty())
Expand Down
6 changes: 6 additions & 0 deletions UI/auth-oauth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class OAuth : public Auth {
const std::string &secret,
const std::string &redirect_uri, int scope_ver,
const std::string &auth_code, bool retry);
bool InvalidateToken(const char *url);
bool InvalidateToken(const char *url, const std::string &client_id);

static const char *GetKeychainLabel()
{
Expand All @@ -82,6 +84,10 @@ class OAuth : public Auth {
const std::string &secret,
const std::string &redirect_uri, int scope_ver,
const std::string &auth_code, bool retry);

bool InvalidateTokenInternal(const char *base_url,
const std::string &client_id,
bool token_as_parameter = false);
};

class OAuthStreamKey : public OAuth {
Expand Down
2 changes: 2 additions & 0 deletions UI/data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ ExtraBrowsers.DockName="Dock Name"
# Auth
Auth.Authing.Title="Authenticating..."
Auth.Authing.Text="Authenticating with %1, please wait..."
Auth.Revoking.Title="Logging out..."
Auth.Revoking.Text="Logging out of %1, please wait..."
Auth.AuthFailure.Title="Authentication Failure"
Auth.AuthFailure.Text="Failed to authenticate with %1:\n\n%2: %3"
Auth.InvalidScope.Title="Authentication Required"
Expand Down

0 comments on commit e505bb8

Please sign in to comment.