auth-mixer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "auth-mixer.hpp"
  2. #include <QPushButton>
  3. #include <QHBoxLayout>
  4. #include <QVBoxLayout>
  5. #include <qt-wrappers.hpp>
  6. #include <obs-app.hpp>
  7. #include "window-dock-browser.hpp"
  8. #include "window-basic-main.hpp"
  9. #include "remote-text.hpp"
  10. #include <json11.hpp>
  11. #include <ctime>
  12. #include "ui-config.h"
  13. #include "obf.h"
  14. using namespace json11;
  15. /* ------------------------------------------------------------------------- */
  16. #define MIXER_AUTH_URL "https://obsproject.com/app-auth/mixer?action=redirect"
  17. #define MIXER_TOKEN_URL "https://obsproject.com/app-auth/mixer-token"
  18. #define MIXER_SCOPE_VERSION 1
  19. static Auth::Def mixerDef = {"Mixer", Auth::Type::OAuth_StreamKey};
  20. /* ------------------------------------------------------------------------- */
  21. MixerAuth::MixerAuth(const Def &d) : OAuthStreamKey(d) {}
  22. bool MixerAuth::GetChannelInfo(bool allow_retry)
  23. try {
  24. std::string client_id = MIXER_CLIENTID;
  25. deobfuscate_str(&client_id[0], MIXER_HASH);
  26. if (!GetToken(MIXER_TOKEN_URL, client_id, MIXER_SCOPE_VERSION))
  27. return false;
  28. if (token.empty())
  29. return false;
  30. if (!key_.empty())
  31. return true;
  32. std::string auth;
  33. auth += "Authorization: Bearer ";
  34. auth += token;
  35. std::vector<std::string> headers;
  36. headers.push_back(std::string("Client-ID: ") + client_id);
  37. headers.push_back(std::move(auth));
  38. std::string output;
  39. std::string error;
  40. Json json;
  41. bool success;
  42. if (id.empty()) {
  43. auto func = [&]() {
  44. success = GetRemoteFile(
  45. "https://mixer.com/api/v1/users/current",
  46. output, error, 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 user info from remote",
  54. error);
  55. Json 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(
  61. error,
  62. json["error_description"].string_value());
  63. id = std::to_string(json["channel"]["id"].int_value());
  64. name = json["channel"]["token"].string_value();
  65. }
  66. /* ------------------ */
  67. std::string url;
  68. url += "https://mixer.com/api/v1/channels/";
  69. url += id;
  70. url += "/details";
  71. output.clear();
  72. auto func = [&]() {
  73. success = GetRemoteFile(url.c_str(), output, error, nullptr,
  74. "application/json", nullptr, headers,
  75. nullptr, 5);
  76. };
  77. ExecThreadedWithoutBlocking(
  78. func, QTStr("Auth.LoadingChannel.Title"),
  79. QTStr("Auth.LoadingChannel.Text").arg(service()));
  80. if (!success || output.empty())
  81. throw ErrorInfo("Failed to get stream key from remote", error);
  82. json = Json::parse(output, error);
  83. if (!error.empty())
  84. throw ErrorInfo("Failed to parse json", error);
  85. error = json["error"].string_value();
  86. if (!error.empty())
  87. throw ErrorInfo(error,
  88. json["error_description"].string_value());
  89. std::string key_suffix = json["streamKey"].string_value();
  90. /* Mixer does not throw an error; instead it gives you the channel data
  91. * json without the data you normally have privileges for, which means
  92. * it'll be an empty stream key usually. So treat empty stream key as
  93. * an error. */
  94. if (key_suffix.empty()) {
  95. if (allow_retry && RetryLogin()) {
  96. return GetChannelInfo(false);
  97. }
  98. throw ErrorInfo("Auth Failure", "Could not get channel data");
  99. }
  100. key_ = id + "-" + key_suffix;
  101. return true;
  102. } catch (ErrorInfo info) {
  103. QString title = QTStr("Auth.ChannelFailure.Title");
  104. QString text = QTStr("Auth.ChannelFailure.Text")
  105. .arg(service(), info.message.c_str(),
  106. info.error.c_str());
  107. QMessageBox::warning(OBSBasic::Get(), title, text);
  108. blog(LOG_WARNING, "%s: %s: %s", __FUNCTION__, info.message.c_str(),
  109. info.error.c_str());
  110. return false;
  111. }
  112. void MixerAuth::SaveInternal()
  113. {
  114. OBSBasic *main = OBSBasic::Get();
  115. config_set_string(main->Config(), service(), "Name", name.c_str());
  116. config_set_string(main->Config(), service(), "Id", id.c_str());
  117. if (uiLoaded) {
  118. config_set_string(main->Config(), service(), "DockState",
  119. main->saveState().toBase64().constData());
  120. }
  121. OAuthStreamKey::SaveInternal();
  122. }
  123. static inline std::string get_config_str(OBSBasic *main, const char *section,
  124. const char *name)
  125. {
  126. const char *val = config_get_string(main->Config(), section, name);
  127. return val ? val : "";
  128. }
  129. bool MixerAuth::LoadInternal()
  130. {
  131. if (!cef)
  132. return false;
  133. OBSBasic *main = OBSBasic::Get();
  134. name = get_config_str(main, service(), "Name");
  135. id = get_config_str(main, service(), "Id");
  136. firstLoad = false;
  137. return OAuthStreamKey::LoadInternal();
  138. }
  139. static const char *elixr_script = "\
  140. var elixr = document.createElement('script');\
  141. elixr.setAttribute('src','https://api.mixrelixr.com/scripts/elixr-emotes-embedded-chat.bundle.js');\
  142. document.head.appendChild(elixr);";
  143. void MixerAuth::LoadUI()
  144. {
  145. if (!cef)
  146. return;
  147. if (uiLoaded)
  148. return;
  149. if (!GetChannelInfo())
  150. return;
  151. OBSBasic::InitBrowserPanelSafeBlock();
  152. OBSBasic *main = OBSBasic::Get();
  153. std::string script = "";
  154. std::string url;
  155. url += "https://mixer.com/embed/chat/";
  156. url += id;
  157. QSize size = main->frameSize();
  158. QPoint pos = main->pos();
  159. chat.reset(new BrowserDock());
  160. chat->setObjectName("mixerChat");
  161. chat->resize(300, 600);
  162. chat->setMinimumSize(200, 300);
  163. chat->setWindowTitle(QTStr("Auth.Chat"));
  164. chat->setAllowedAreas(Qt::AllDockWidgetAreas);
  165. QCefWidget *browser = cef->create_widget(nullptr, url, panel_cookies);
  166. chat->SetWidget(browser);
  167. const int mxAddonChoice =
  168. config_get_int(main->Config(), service(), "AddonChoice");
  169. if (mxAddonChoice) {
  170. if (mxAddonChoice & 0x1)
  171. script += elixr_script;
  172. }
  173. browser->setStartupScript(script);
  174. main->addDockWidget(Qt::RightDockWidgetArea, chat.data());
  175. chatMenu.reset(main->AddDockWidget(chat.data()));
  176. /* ----------------------------------- */
  177. chat->setFloating(true);
  178. chat->move(pos.x() + size.width() - chat->width() - 50, pos.y() + 50);
  179. if (firstLoad) {
  180. chat->setVisible(true);
  181. } else {
  182. const char *dockStateStr = config_get_string(
  183. main->Config(), service(), "DockState");
  184. QByteArray dockState =
  185. QByteArray::fromBase64(QByteArray(dockStateStr));
  186. main->restoreState(dockState);
  187. }
  188. uiLoaded = true;
  189. }
  190. bool MixerAuth::RetryLogin()
  191. {
  192. if (!cef)
  193. return false;
  194. OAuthLogin login(OBSBasic::Get(), MIXER_AUTH_URL, false);
  195. cef->add_popup_whitelist_url("about:blank", &login);
  196. if (login.exec() == QDialog::Rejected) {
  197. return false;
  198. }
  199. std::shared_ptr<MixerAuth> auth = std::make_shared<MixerAuth>(mixerDef);
  200. std::string client_id = MIXER_CLIENTID;
  201. deobfuscate_str(&client_id[0], MIXER_HASH);
  202. return GetToken(MIXER_TOKEN_URL, client_id, MIXER_SCOPE_VERSION,
  203. QT_TO_UTF8(login.GetCode()), true);
  204. }
  205. std::shared_ptr<Auth> MixerAuth::Login(QWidget *parent)
  206. {
  207. if (!cef) {
  208. return nullptr;
  209. }
  210. OAuthLogin login(parent, MIXER_AUTH_URL, false);
  211. cef->add_popup_whitelist_url("about:blank", &login);
  212. if (login.exec() == QDialog::Rejected) {
  213. return nullptr;
  214. }
  215. std::shared_ptr<MixerAuth> auth = std::make_shared<MixerAuth>(mixerDef);
  216. std::string client_id = MIXER_CLIENTID;
  217. deobfuscate_str(&client_id[0], MIXER_HASH);
  218. if (!auth->GetToken(MIXER_TOKEN_URL, client_id, MIXER_SCOPE_VERSION,
  219. QT_TO_UTF8(login.GetCode()))) {
  220. return nullptr;
  221. }
  222. std::string error;
  223. if (auth->GetChannelInfo(false)) {
  224. return auth;
  225. }
  226. return nullptr;
  227. }
  228. static std::shared_ptr<Auth> CreateMixerAuth()
  229. {
  230. return std::make_shared<MixerAuth>(mixerDef);
  231. }
  232. static void DeleteCookies()
  233. {
  234. if (panel_cookies) {
  235. panel_cookies->DeleteCookies("mixer.com", std::string());
  236. panel_cookies->DeleteCookies("microsoft.com", std::string());
  237. }
  238. }
  239. void RegisterMixerAuth()
  240. {
  241. OAuth::RegisterOAuth(mixerDef, CreateMixerAuth, MixerAuth::Login,
  242. DeleteCookies);
  243. }