auth-restream.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "moc_auth-restream.cpp"
  2. #include <QPushButton>
  3. #include <QHBoxLayout>
  4. #include <QVBoxLayout>
  5. #include <qt-wrappers.hpp>
  6. #include <json11.hpp>
  7. #include <ctime>
  8. #include <sstream>
  9. #include <obs-app.hpp>
  10. #include "window-dock-browser.hpp"
  11. #include "window-basic-main.hpp"
  12. #include "remote-text.hpp"
  13. #include "ui-config.h"
  14. #include "obf.h"
  15. using namespace json11;
  16. /* ------------------------------------------------------------------------- */
  17. #define RESTREAM_AUTH_URL OAUTH_BASE_URL "v1/restream/redirect"
  18. #define RESTREAM_TOKEN_URL OAUTH_BASE_URL "v1/restream/token"
  19. #define RESTREAM_STREAMKEY_URL "https://api.restream.io/v2/user/streamKey"
  20. #define RESTREAM_SCOPE_VERSION 1
  21. #define RESTREAM_CHAT_DOCK_NAME "restreamChat"
  22. #define RESTREAM_INFO_DOCK_NAME "restreamInfo"
  23. #define RESTREAM_CHANNELS_DOCK_NAME "restreamChannel"
  24. static Auth::Def restreamDef = {"Restream", Auth::Type::OAuth_StreamKey};
  25. /* ------------------------------------------------------------------------- */
  26. RestreamAuth::RestreamAuth(const Def &d) : OAuthStreamKey(d) {}
  27. RestreamAuth::~RestreamAuth()
  28. {
  29. if (!uiLoaded)
  30. return;
  31. OBSBasic *main = OBSBasic::Get();
  32. main->RemoveDockWidget(RESTREAM_CHAT_DOCK_NAME);
  33. main->RemoveDockWidget(RESTREAM_INFO_DOCK_NAME);
  34. main->RemoveDockWidget(RESTREAM_CHANNELS_DOCK_NAME);
  35. }
  36. bool RestreamAuth::GetChannelInfo()
  37. try {
  38. std::string client_id = RESTREAM_CLIENTID;
  39. deobfuscate_str(&client_id[0], RESTREAM_HASH);
  40. if (!GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION))
  41. return false;
  42. if (token.empty())
  43. return false;
  44. if (!key_.empty())
  45. return true;
  46. std::string auth;
  47. auth += "Authorization: Bearer ";
  48. auth += token;
  49. std::vector<std::string> headers;
  50. headers.push_back(std::string("Client-ID: ") + client_id);
  51. headers.push_back(std::move(auth));
  52. std::string output;
  53. std::string error;
  54. Json json;
  55. bool success;
  56. auto func = [&]() {
  57. success = GetRemoteFile(RESTREAM_STREAMKEY_URL, output, error, nullptr, "application/json", "", nullptr,
  58. headers, nullptr, 5);
  59. };
  60. ExecThreadedWithoutBlocking(func, QTStr("Auth.LoadingChannel.Title"),
  61. QTStr("Auth.LoadingChannel.Text").arg(service()));
  62. if (!success || output.empty())
  63. throw ErrorInfo("Failed to get stream key from remote", error);
  64. json = Json::parse(output, error);
  65. if (!error.empty())
  66. throw ErrorInfo("Failed to parse json", error);
  67. error = json["error"].string_value();
  68. if (!error.empty())
  69. throw ErrorInfo(error, json["error_description"].string_value());
  70. key_ = json["streamKey"].string_value();
  71. return true;
  72. } catch (ErrorInfo info) {
  73. QString title = QTStr("Auth.ChannelFailure.Title");
  74. QString text = QTStr("Auth.ChannelFailure.Text").arg(service(), info.message.c_str(), info.error.c_str());
  75. QMessageBox::warning(OBSBasic::Get(), title, text);
  76. blog(LOG_WARNING, "%s: %s: %s", __FUNCTION__, info.message.c_str(), info.error.c_str());
  77. return false;
  78. }
  79. void RestreamAuth::SaveInternal()
  80. {
  81. OBSBasic *main = OBSBasic::Get();
  82. config_set_string(main->Config(), service(), "DockState", main->saveState().toBase64().constData());
  83. OAuthStreamKey::SaveInternal();
  84. }
  85. static inline std::string get_config_str(OBSBasic *main, const char *section, const char *name)
  86. {
  87. const char *val = config_get_string(main->Config(), section, name);
  88. return val ? val : "";
  89. }
  90. bool RestreamAuth::LoadInternal()
  91. {
  92. firstLoad = false;
  93. return OAuthStreamKey::LoadInternal();
  94. }
  95. void RestreamAuth::LoadUI()
  96. {
  97. if (uiLoaded)
  98. return;
  99. if (!GetChannelInfo())
  100. return;
  101. OBSBasic::InitBrowserPanelSafeBlock();
  102. OBSBasic *main = OBSBasic::Get();
  103. QCefWidget *browser;
  104. std::string url;
  105. std::string script;
  106. /* ----------------------------------- */
  107. url = "https://restream.io/chat-application";
  108. QSize size = main->frameSize();
  109. QPoint pos = main->pos();
  110. BrowserDock *chat = new BrowserDock(QTStr("Auth.Chat"));
  111. chat->setObjectName(RESTREAM_CHAT_DOCK_NAME);
  112. chat->resize(420, 600);
  113. chat->setMinimumSize(200, 300);
  114. chat->setWindowTitle(QTStr("Auth.Chat"));
  115. chat->setAllowedAreas(Qt::AllDockWidgetAreas);
  116. browser = cef->create_widget(chat, url, panel_cookies);
  117. chat->SetWidget(browser);
  118. main->AddDockWidget(chat, Qt::RightDockWidgetArea);
  119. /* ----------------------------------- */
  120. url = "https://restream.io/titles/embed";
  121. BrowserDock *info = new BrowserDock(QTStr("Auth.StreamInfo"));
  122. info->setObjectName(RESTREAM_INFO_DOCK_NAME);
  123. info->resize(410, 600);
  124. info->setMinimumSize(200, 150);
  125. info->setWindowTitle(QTStr("Auth.StreamInfo"));
  126. info->setAllowedAreas(Qt::AllDockWidgetAreas);
  127. browser = cef->create_widget(info, url, panel_cookies);
  128. info->SetWidget(browser);
  129. main->AddDockWidget(info, Qt::LeftDockWidgetArea);
  130. /* ----------------------------------- */
  131. url = "https://restream.io/channel/embed";
  132. BrowserDock *channels = new BrowserDock(QTStr("RestreamAuth.Channels"));
  133. channels->setObjectName(RESTREAM_CHANNELS_DOCK_NAME);
  134. channels->resize(410, 600);
  135. channels->setMinimumSize(410, 300);
  136. channels->setWindowTitle(QTStr("RestreamAuth.Channels"));
  137. channels->setAllowedAreas(Qt::AllDockWidgetAreas);
  138. browser = cef->create_widget(channels, url, panel_cookies);
  139. channels->SetWidget(browser);
  140. main->AddDockWidget(channels, Qt::LeftDockWidgetArea);
  141. /* ----------------------------------- */
  142. chat->setFloating(true);
  143. info->setFloating(true);
  144. channels->setFloating(true);
  145. chat->move(pos.x() + size.width() - chat->width() - 30, pos.y() + 60);
  146. info->move(pos.x() + 20, pos.y() + 60);
  147. channels->move(pos.x() + 20 + info->width() + 10, pos.y() + 60);
  148. if (firstLoad) {
  149. chat->setVisible(true);
  150. info->setVisible(true);
  151. channels->setVisible(true);
  152. } else {
  153. const char *dockStateStr = config_get_string(main->Config(), service(), "DockState");
  154. QByteArray dockState = QByteArray::fromBase64(QByteArray(dockStateStr));
  155. if (main->isVisible() || !main->isMaximized())
  156. main->restoreState(dockState);
  157. }
  158. uiLoaded = true;
  159. }
  160. bool RestreamAuth::RetryLogin()
  161. {
  162. OAuthLogin login(OBSBasic::Get(), RESTREAM_AUTH_URL, false);
  163. cef->add_popup_whitelist_url("about:blank", &login);
  164. if (login.exec() == QDialog::Rejected) {
  165. return false;
  166. }
  167. std::shared_ptr<RestreamAuth> auth = std::make_shared<RestreamAuth>(restreamDef);
  168. std::string client_id = RESTREAM_CLIENTID;
  169. deobfuscate_str(&client_id[0], RESTREAM_HASH);
  170. return GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION, QT_TO_UTF8(login.GetCode()), true);
  171. }
  172. std::shared_ptr<Auth> RestreamAuth::Login(QWidget *parent, const std::string &)
  173. {
  174. OAuthLogin login(parent, RESTREAM_AUTH_URL, false);
  175. cef->add_popup_whitelist_url("about:blank", &login);
  176. if (login.exec() == QDialog::Rejected) {
  177. return nullptr;
  178. }
  179. std::shared_ptr<RestreamAuth> auth = std::make_shared<RestreamAuth>(restreamDef);
  180. std::string client_id = RESTREAM_CLIENTID;
  181. deobfuscate_str(&client_id[0], RESTREAM_HASH);
  182. if (!auth->GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION, QT_TO_UTF8(login.GetCode()))) {
  183. return nullptr;
  184. }
  185. std::string error;
  186. if (auth->GetChannelInfo()) {
  187. return auth;
  188. }
  189. return nullptr;
  190. }
  191. static std::shared_ptr<Auth> CreateRestreamAuth()
  192. {
  193. return std::make_shared<RestreamAuth>(restreamDef);
  194. }
  195. static void DeleteCookies()
  196. {
  197. if (panel_cookies) {
  198. panel_cookies->DeleteCookies("restream.io", std::string());
  199. }
  200. }
  201. void RegisterRestreamAuth()
  202. {
  203. #if !defined(__APPLE__) && !defined(_WIN32)
  204. if (QApplication::platformName().contains("wayland"))
  205. return;
  206. #endif
  207. OAuth::RegisterOAuth(restreamDef, CreateRestreamAuth, RestreamAuth::Login, DeleteCookies);
  208. }