auth-restream.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 \
  18. "https://obsproject.com/app-auth/restream?action=redirect"
  19. #define RESTREAM_TOKEN_URL "https://obsproject.com/app-auth/restream-token"
  20. #define RESTREAM_STREAMKEY_URL "https://api.restream.io/v2/user/streamKey"
  21. #define RESTREAM_SCOPE_VERSION 1
  22. static Auth::Def restreamDef = {"Restream", Auth::Type::OAuth_StreamKey};
  23. /* ------------------------------------------------------------------------- */
  24. RestreamAuth::RestreamAuth(const Def &d) : OAuthStreamKey(d) {}
  25. bool RestreamAuth::GetChannelInfo()
  26. try {
  27. std::string client_id = RESTREAM_CLIENTID;
  28. deobfuscate_str(&client_id[0], RESTREAM_HASH);
  29. if (!GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION))
  30. return false;
  31. if (token.empty())
  32. return false;
  33. if (!key_.empty())
  34. return true;
  35. std::string auth;
  36. auth += "Authorization: Bearer ";
  37. auth += token;
  38. std::vector<std::string> headers;
  39. headers.push_back(std::string("Client-ID: ") + client_id);
  40. headers.push_back(std::move(auth));
  41. std::string output;
  42. std::string error;
  43. Json json;
  44. bool success;
  45. auto func = [&]() {
  46. success = GetRemoteFile(RESTREAM_STREAMKEY_URL, output, error,
  47. nullptr, "application/json", nullptr,
  48. headers, nullptr, 5);
  49. };
  50. ExecThreadedWithoutBlocking(
  51. func, QTStr("Auth.LoadingChannel.Title"),
  52. QTStr("Auth.LoadingChannel.Text").arg(service()));
  53. if (!success || output.empty())
  54. throw ErrorInfo("Failed to get stream key from remote", error);
  55. json = Json::parse(output, error);
  56. if (!error.empty())
  57. throw ErrorInfo("Failed to parse json", error);
  58. error = json["error"].string_value();
  59. if (!error.empty())
  60. throw ErrorInfo(error,
  61. json["error_description"].string_value());
  62. key_ = json["streamKey"].string_value();
  63. return true;
  64. } catch (ErrorInfo info) {
  65. QString title = QTStr("Auth.ChannelFailure.Title");
  66. QString text = QTStr("Auth.ChannelFailure.Text")
  67. .arg(service(), info.message.c_str(),
  68. info.error.c_str());
  69. QMessageBox::warning(OBSBasic::Get(), title, text);
  70. blog(LOG_WARNING, "%s: %s: %s", __FUNCTION__, info.message.c_str(),
  71. info.error.c_str());
  72. return false;
  73. }
  74. void RestreamAuth::SaveInternal()
  75. {
  76. OBSBasic *main = OBSBasic::Get();
  77. config_set_string(main->Config(), service(), "DockState",
  78. main->saveState().toBase64().constData());
  79. OAuthStreamKey::SaveInternal();
  80. }
  81. static inline std::string get_config_str(OBSBasic *main, const char *section,
  82. const char *name)
  83. {
  84. const char *val = config_get_string(main->Config(), section, name);
  85. return val ? val : "";
  86. }
  87. bool RestreamAuth::LoadInternal()
  88. {
  89. firstLoad = false;
  90. return OAuthStreamKey::LoadInternal();
  91. }
  92. void RestreamAuth::LoadUI()
  93. {
  94. if (uiLoaded)
  95. return;
  96. if (!GetChannelInfo())
  97. return;
  98. OBSBasic::InitBrowserPanelSafeBlock();
  99. OBSBasic *main = OBSBasic::Get();
  100. QCefWidget *browser;
  101. std::string url;
  102. std::string script;
  103. /* ----------------------------------- */
  104. url = "https://restream.io/chat-application";
  105. QSize size = main->frameSize();
  106. QPoint pos = main->pos();
  107. chat.reset(new BrowserDock());
  108. chat->setObjectName("restreamChat");
  109. chat->resize(420, 600);
  110. chat->setMinimumSize(200, 300);
  111. chat->setWindowTitle(QTStr("Auth.Chat"));
  112. chat->setAllowedAreas(Qt::AllDockWidgetAreas);
  113. browser = cef->create_widget(nullptr, url, panel_cookies);
  114. chat->SetWidget(browser);
  115. main->addDockWidget(Qt::RightDockWidgetArea, chat.data());
  116. chatMenu.reset(main->AddDockWidget(chat.data()));
  117. /* ----------------------------------- */
  118. url = "https://restream.io/titles/embed";
  119. info.reset(new BrowserDock());
  120. info->setObjectName("restreamInfo");
  121. info->resize(410, 600);
  122. info->setMinimumSize(200, 150);
  123. info->setWindowTitle(QTStr("Auth.StreamInfo"));
  124. info->setAllowedAreas(Qt::AllDockWidgetAreas);
  125. browser = cef->create_widget(nullptr, url, panel_cookies);
  126. info->SetWidget(browser);
  127. main->addDockWidget(Qt::LeftDockWidgetArea, info.data());
  128. infoMenu.reset(main->AddDockWidget(info.data()));
  129. /* ----------------------------------- */
  130. url = "https://restream.io/channel/embed";
  131. channels.reset(new BrowserDock());
  132. channels->setObjectName("restreamChannel");
  133. channels->resize(410, 600);
  134. channels->setMinimumSize(410, 300);
  135. channels->setWindowTitle(QTStr("RestreamAuth.Channels"));
  136. channels->setAllowedAreas(Qt::AllDockWidgetAreas);
  137. browser = cef->create_widget(nullptr, url, panel_cookies);
  138. channels->SetWidget(browser);
  139. main->addDockWidget(Qt::LeftDockWidgetArea, channels.data());
  140. channelMenu.reset(main->AddDockWidget(channels.data()));
  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(
  154. main->Config(), service(), "DockState");
  155. QByteArray dockState =
  156. QByteArray::fromBase64(QByteArray(dockStateStr));
  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)
  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. }