auth-restream.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #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,
  58. nullptr, "application/json", "",
  59. nullptr, headers, nullptr, 5);
  60. };
  61. ExecThreadedWithoutBlocking(
  62. func, QTStr("Auth.LoadingChannel.Title"),
  63. QTStr("Auth.LoadingChannel.Text").arg(service()));
  64. if (!success || output.empty())
  65. throw ErrorInfo("Failed to get stream key from remote", error);
  66. json = Json::parse(output, error);
  67. if (!error.empty())
  68. throw ErrorInfo("Failed to parse json", error);
  69. error = json["error"].string_value();
  70. if (!error.empty())
  71. throw ErrorInfo(error,
  72. json["error_description"].string_value());
  73. key_ = json["streamKey"].string_value();
  74. return true;
  75. } catch (ErrorInfo info) {
  76. QString title = QTStr("Auth.ChannelFailure.Title");
  77. QString text = QTStr("Auth.ChannelFailure.Text")
  78. .arg(service(), info.message.c_str(),
  79. info.error.c_str());
  80. QMessageBox::warning(OBSBasic::Get(), title, text);
  81. blog(LOG_WARNING, "%s: %s: %s", __FUNCTION__, info.message.c_str(),
  82. info.error.c_str());
  83. return false;
  84. }
  85. void RestreamAuth::SaveInternal()
  86. {
  87. OBSBasic *main = OBSBasic::Get();
  88. config_set_string(main->Config(), service(), "DockState",
  89. main->saveState().toBase64().constData());
  90. OAuthStreamKey::SaveInternal();
  91. }
  92. static inline std::string get_config_str(OBSBasic *main, const char *section,
  93. const char *name)
  94. {
  95. const char *val = config_get_string(main->Config(), section, name);
  96. return val ? val : "";
  97. }
  98. bool RestreamAuth::LoadInternal()
  99. {
  100. firstLoad = false;
  101. return OAuthStreamKey::LoadInternal();
  102. }
  103. void RestreamAuth::LoadUI()
  104. {
  105. if (uiLoaded)
  106. return;
  107. if (!GetChannelInfo())
  108. return;
  109. OBSBasic::InitBrowserPanelSafeBlock();
  110. OBSBasic *main = OBSBasic::Get();
  111. QCefWidget *browser;
  112. std::string url;
  113. std::string script;
  114. /* ----------------------------------- */
  115. url = "https://restream.io/chat-application";
  116. QSize size = main->frameSize();
  117. QPoint pos = main->pos();
  118. BrowserDock *chat = new BrowserDock(QTStr("Auth.Chat"));
  119. chat->setObjectName(RESTREAM_CHAT_DOCK_NAME);
  120. chat->resize(420, 600);
  121. chat->setMinimumSize(200, 300);
  122. chat->setWindowTitle(QTStr("Auth.Chat"));
  123. chat->setAllowedAreas(Qt::AllDockWidgetAreas);
  124. browser = cef->create_widget(chat, url, panel_cookies);
  125. chat->SetWidget(browser);
  126. main->AddDockWidget(chat, Qt::RightDockWidgetArea);
  127. /* ----------------------------------- */
  128. url = "https://restream.io/titles/embed";
  129. BrowserDock *info = new BrowserDock(QTStr("Auth.StreamInfo"));
  130. info->setObjectName(RESTREAM_INFO_DOCK_NAME);
  131. info->resize(410, 600);
  132. info->setMinimumSize(200, 150);
  133. info->setWindowTitle(QTStr("Auth.StreamInfo"));
  134. info->setAllowedAreas(Qt::AllDockWidgetAreas);
  135. browser = cef->create_widget(info, url, panel_cookies);
  136. info->SetWidget(browser);
  137. main->AddDockWidget(info, Qt::LeftDockWidgetArea);
  138. /* ----------------------------------- */
  139. url = "https://restream.io/channel/embed";
  140. BrowserDock *channels = new BrowserDock(QTStr("RestreamAuth.Channels"));
  141. channels->setObjectName(RESTREAM_CHANNELS_DOCK_NAME);
  142. channels->resize(410, 600);
  143. channels->setMinimumSize(410, 300);
  144. channels->setWindowTitle(QTStr("RestreamAuth.Channels"));
  145. channels->setAllowedAreas(Qt::AllDockWidgetAreas);
  146. browser = cef->create_widget(channels, url, panel_cookies);
  147. channels->SetWidget(browser);
  148. main->AddDockWidget(channels, Qt::LeftDockWidgetArea);
  149. /* ----------------------------------- */
  150. chat->setFloating(true);
  151. info->setFloating(true);
  152. channels->setFloating(true);
  153. chat->move(pos.x() + size.width() - chat->width() - 30, pos.y() + 60);
  154. info->move(pos.x() + 20, pos.y() + 60);
  155. channels->move(pos.x() + 20 + info->width() + 10, pos.y() + 60);
  156. if (firstLoad) {
  157. chat->setVisible(true);
  158. info->setVisible(true);
  159. channels->setVisible(true);
  160. } else {
  161. const char *dockStateStr = config_get_string(
  162. main->Config(), service(), "DockState");
  163. QByteArray dockState =
  164. QByteArray::fromBase64(QByteArray(dockStateStr));
  165. if (main->isVisible() || !main->isMaximized())
  166. main->restoreState(dockState);
  167. }
  168. uiLoaded = true;
  169. }
  170. bool RestreamAuth::RetryLogin()
  171. {
  172. OAuthLogin login(OBSBasic::Get(), RESTREAM_AUTH_URL, false);
  173. cef->add_popup_whitelist_url("about:blank", &login);
  174. if (login.exec() == QDialog::Rejected) {
  175. return false;
  176. }
  177. std::shared_ptr<RestreamAuth> auth =
  178. std::make_shared<RestreamAuth>(restreamDef);
  179. std::string client_id = RESTREAM_CLIENTID;
  180. deobfuscate_str(&client_id[0], RESTREAM_HASH);
  181. return GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION,
  182. QT_TO_UTF8(login.GetCode()), true);
  183. }
  184. std::shared_ptr<Auth> RestreamAuth::Login(QWidget *parent, const std::string &)
  185. {
  186. OAuthLogin login(parent, RESTREAM_AUTH_URL, false);
  187. cef->add_popup_whitelist_url("about:blank", &login);
  188. if (login.exec() == QDialog::Rejected) {
  189. return nullptr;
  190. }
  191. std::shared_ptr<RestreamAuth> auth =
  192. std::make_shared<RestreamAuth>(restreamDef);
  193. std::string client_id = RESTREAM_CLIENTID;
  194. deobfuscate_str(&client_id[0], RESTREAM_HASH);
  195. if (!auth->GetToken(RESTREAM_TOKEN_URL, client_id,
  196. RESTREAM_SCOPE_VERSION,
  197. QT_TO_UTF8(login.GetCode()))) {
  198. return nullptr;
  199. }
  200. std::string error;
  201. if (auth->GetChannelInfo()) {
  202. return auth;
  203. }
  204. return nullptr;
  205. }
  206. static std::shared_ptr<Auth> CreateRestreamAuth()
  207. {
  208. return std::make_shared<RestreamAuth>(restreamDef);
  209. }
  210. static void DeleteCookies()
  211. {
  212. if (panel_cookies) {
  213. panel_cookies->DeleteCookies("restream.io", std::string());
  214. }
  215. }
  216. void RegisterRestreamAuth()
  217. {
  218. #if !defined(__APPLE__) && !defined(_WIN32)
  219. if (QApplication::platformName().contains("wayland"))
  220. return;
  221. #endif
  222. OAuth::RegisterOAuth(restreamDef, CreateRestreamAuth,
  223. RestreamAuth::Login, DeleteCookies);
  224. }