1
0

auth-mixer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. void MixerAuth::LoadUI()
  140. {
  141. if (!cef)
  142. return;
  143. if (uiLoaded)
  144. return;
  145. if (!GetChannelInfo())
  146. return;
  147. OBSBasic::InitBrowserPanelSafeBlock();
  148. OBSBasic *main = OBSBasic::Get();
  149. std::string url;
  150. url += "https://mixer.com/embed/chat/";
  151. url += id;
  152. QSize size = main->frameSize();
  153. QPoint pos = main->pos();
  154. chat.reset(new BrowserDock());
  155. chat->setObjectName("mixerChat");
  156. chat->resize(300, 600);
  157. chat->setMinimumSize(200, 300);
  158. chat->setWindowTitle(QTStr("Auth.Chat"));
  159. chat->setAllowedAreas(Qt::AllDockWidgetAreas);
  160. QCefWidget *browser = cef->create_widget(nullptr, url, panel_cookies);
  161. chat->SetWidget(browser);
  162. main->addDockWidget(Qt::RightDockWidgetArea, chat.data());
  163. chatMenu.reset(main->AddDockWidget(chat.data()));
  164. /* ----------------------------------- */
  165. chat->setFloating(true);
  166. chat->move(pos.x() + size.width() - chat->width() - 50, pos.y() + 50);
  167. if (firstLoad) {
  168. chat->setVisible(true);
  169. } else {
  170. const char *dockStateStr = config_get_string(
  171. main->Config(), service(), "DockState");
  172. QByteArray dockState =
  173. QByteArray::fromBase64(QByteArray(dockStateStr));
  174. main->restoreState(dockState);
  175. }
  176. uiLoaded = true;
  177. }
  178. bool MixerAuth::RetryLogin()
  179. {
  180. if (!cef)
  181. return false;
  182. OAuthLogin login(OBSBasic::Get(), MIXER_AUTH_URL, false);
  183. cef->add_popup_whitelist_url("about:blank", &login);
  184. if (login.exec() == QDialog::Rejected) {
  185. return false;
  186. }
  187. std::shared_ptr<MixerAuth> auth = std::make_shared<MixerAuth>(mixerDef);
  188. std::string client_id = MIXER_CLIENTID;
  189. deobfuscate_str(&client_id[0], MIXER_HASH);
  190. return GetToken(MIXER_TOKEN_URL, client_id, MIXER_SCOPE_VERSION,
  191. QT_TO_UTF8(login.GetCode()), true);
  192. }
  193. std::shared_ptr<Auth> MixerAuth::Login(QWidget *parent)
  194. {
  195. if (!cef) {
  196. return nullptr;
  197. }
  198. OAuthLogin login(parent, MIXER_AUTH_URL, false);
  199. cef->add_popup_whitelist_url("about:blank", &login);
  200. if (login.exec() == QDialog::Rejected) {
  201. return nullptr;
  202. }
  203. std::shared_ptr<MixerAuth> auth = std::make_shared<MixerAuth>(mixerDef);
  204. std::string client_id = MIXER_CLIENTID;
  205. deobfuscate_str(&client_id[0], MIXER_HASH);
  206. if (!auth->GetToken(MIXER_TOKEN_URL, client_id, MIXER_SCOPE_VERSION,
  207. QT_TO_UTF8(login.GetCode()))) {
  208. return nullptr;
  209. }
  210. std::string error;
  211. if (auth->GetChannelInfo(false)) {
  212. return auth;
  213. }
  214. return nullptr;
  215. }
  216. static std::shared_ptr<Auth> CreateMixerAuth()
  217. {
  218. return std::make_shared<MixerAuth>(mixerDef);
  219. }
  220. static void DeleteCookies()
  221. {
  222. if (panel_cookies) {
  223. panel_cookies->DeleteCookies("mixer.com", std::string());
  224. panel_cookies->DeleteCookies("microsoft.com", std::string());
  225. }
  226. }
  227. void RegisterMixerAuth()
  228. {
  229. OAuth::RegisterOAuth(mixerDef, CreateMixerAuth, MixerAuth::Login,
  230. DeleteCookies);
  231. }