auth-mixer.cpp 7.5 KB

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