auth-mixer.cpp 7.4 KB

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