auth-restream.cpp 7.0 KB

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