#include "auth-youtube.hpp" #include #include #include #include #include #include #include #ifdef WIN32 #include #include #pragma comment(lib, "shell32") #endif #include "auth-listener.hpp" #include "obs-app.hpp" #include "qt-wrappers.hpp" #include "ui-config.h" #include "youtube-api-wrappers.hpp" #include "window-basic-main.hpp" #include "obf.h" using namespace json11; /* ------------------------------------------------------------------------- */ #define YOUTUBE_AUTH_URL "https://accounts.google.com/o/oauth2/v2/auth" #define YOUTUBE_TOKEN_URL "https://www.googleapis.com/oauth2/v4/token" #define YOUTUBE_SCOPE_VERSION 1 #define YOUTUBE_API_STATE_LENGTH 32 #define SECTION_NAME "YouTube" static const char allowedChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; static const int allowedCount = static_cast(sizeof(allowedChars) - 1); /* ------------------------------------------------------------------------- */ static inline void OpenBrowser(const QString auth_uri) { QUrl url(auth_uri, QUrl::StrictMode); QDesktopServices::openUrl(url); } void RegisterYoutubeAuth() { for (auto &service : youtubeServices) { OAuth::RegisterOAuth( service, [service]() { return std::make_shared( service); }, YoutubeAuth::Login, []() { return; }); } } YoutubeAuth::YoutubeAuth(const Def &d) : OAuthStreamKey(d), section(SECTION_NAME) { } bool YoutubeAuth::RetryLogin() { return true; } void YoutubeAuth::SaveInternal() { OBSBasic *main = OBSBasic::Get(); config_set_string(main->Config(), service(), "DockState", main->saveState().toBase64().constData()); const char *section_name = section.c_str(); config_set_string(main->Config(), section_name, "RefreshToken", refresh_token.c_str()); config_set_string(main->Config(), section_name, "Token", token.c_str()); config_set_uint(main->Config(), section_name, "ExpireTime", expire_time); config_set_int(main->Config(), section_name, "ScopeVer", currentScopeVer); } static inline std::string get_config_str(OBSBasic *main, const char *section, const char *name) { const char *val = config_get_string(main->Config(), section, name); return val ? val : ""; } bool YoutubeAuth::LoadInternal() { OBSBasic *main = OBSBasic::Get(); const char *section_name = section.c_str(); refresh_token = get_config_str(main, section_name, "RefreshToken"); token = get_config_str(main, section_name, "Token"); expire_time = config_get_uint(main->Config(), section_name, "ExpireTime"); currentScopeVer = (int)config_get_int(main->Config(), section_name, "ScopeVer"); return implicit ? !token.empty() : !refresh_token.empty(); } void YoutubeAuth::LoadUI() { uiLoaded = true; } QString YoutubeAuth::GenerateState() { std::uniform_int_distribution<> distr(0, allowedCount); std::string result; result.reserve(YOUTUBE_API_STATE_LENGTH); std::generate_n(std::back_inserter(result), YOUTUBE_API_STATE_LENGTH, [&] { return static_cast( allowedChars[distr(randomSeed)]); }); return result.c_str(); } // Static. std::shared_ptr YoutubeAuth::Login(QWidget *owner, const std::string &service) { QString auth_code; AuthListener server; auto it = std::find_if(youtubeServices.begin(), youtubeServices.end(), [service](auto &item) { return service == item.service; }); if (it == youtubeServices.end()) { return nullptr; } const auto auth = std::make_shared(*it); QString redirect_uri = QString("http://127.0.0.1:%1").arg(server.GetPort()); QMessageBox dlg(owner); dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint); dlg.setWindowTitle(QTStr("YouTube.Auth.WaitingAuth.Title")); std::string clientid = YOUTUBE_CLIENTID; std::string secret = YOUTUBE_SECRET; deobfuscate_str(&clientid[0], YOUTUBE_CLIENTID_HASH); deobfuscate_str(&secret[0], YOUTUBE_SECRET_HASH); QString url_template; url_template += "%1"; url_template += "?response_type=code"; url_template += "&client_id=%2"; url_template += "&redirect_uri=%3"; url_template += "&state=%4"; url_template += "&scope=https://www.googleapis.com/auth/youtube"; QString url = url_template.arg(YOUTUBE_AUTH_URL, clientid.c_str(), redirect_uri, auth->GenerateState()); QString text = QTStr("YouTube.Auth.WaitingAuth.Text"); text = text.arg( QString("Google OAuth Service").arg(url)); dlg.setText(text); dlg.setTextFormat(Qt::RichText); dlg.setStandardButtons(QMessageBox::StandardButton::Cancel); connect(&dlg, &QMessageBox::buttonClicked, &dlg, [&](QAbstractButton *) { #ifdef _DEBUG blog(LOG_DEBUG, "Action Cancelled."); #endif // TODO: Stop server. dlg.reject(); }); // Async Login. connect(&server, &AuthListener::ok, &dlg, [&dlg, &auth_code](QString code) { #ifdef _DEBUG blog(LOG_DEBUG, "Got youtube redirected answer: %s", QT_TO_UTF8(code)); #endif auth_code = code; dlg.accept(); }); connect(&server, &AuthListener::fail, &dlg, [&dlg]() { #ifdef _DEBUG blog(LOG_DEBUG, "No access granted"); #endif dlg.reject(); }); auto open_external_browser = [url]() { OpenBrowser(url); }; QScopedPointer thread(CreateQThread(open_external_browser)); thread->start(); dlg.exec(); if (!auth->GetToken(YOUTUBE_TOKEN_URL, clientid, secret, QT_TO_UTF8(redirect_uri), YOUTUBE_SCOPE_VERSION, QT_TO_UTF8(auth_code), true)) { return nullptr; } return auth; }