1
0

OAuthLogin.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "OAuthLogin.hpp"
  2. #include <widgets/OBSBasic.hpp>
  3. #ifdef BROWSER_AVAILABLE
  4. #include <browser-panel.hpp>
  5. #endif
  6. #include <ui-config.h>
  7. #include "moc_OAuthLogin.cpp"
  8. #ifdef BROWSER_AVAILABLE
  9. extern QCef *cef;
  10. extern QCefCookieManager *panel_cookies;
  11. #endif
  12. OAuthLogin::OAuthLogin(QWidget *parent, const std::string &url, bool token) : QDialog(parent), get_token(token)
  13. {
  14. #ifdef BROWSER_AVAILABLE
  15. if (!cef) {
  16. return;
  17. }
  18. setWindowTitle("Auth");
  19. setMinimumSize(400, 400);
  20. resize(700, 700);
  21. Qt::WindowFlags flags = windowFlags();
  22. Qt::WindowFlags helpFlag = Qt::WindowContextHelpButtonHint;
  23. setWindowFlags(flags & (~helpFlag));
  24. OBSBasic::InitBrowserPanelSafeBlock();
  25. cefWidget = cef->create_widget(nullptr, url, panel_cookies);
  26. if (!cefWidget) {
  27. fail = true;
  28. return;
  29. }
  30. connect(cefWidget, &QCefWidget::titleChanged, this, &OAuthLogin::setWindowTitle);
  31. connect(cefWidget, &QCefWidget::urlChanged, this, &OAuthLogin::urlChanged);
  32. QPushButton *close = new QPushButton(QTStr("Cancel"));
  33. connect(close, &QAbstractButton::clicked, this, &QDialog::reject);
  34. QHBoxLayout *bottomLayout = new QHBoxLayout();
  35. bottomLayout->addStretch();
  36. bottomLayout->addWidget(close);
  37. bottomLayout->addStretch();
  38. QVBoxLayout *topLayout = new QVBoxLayout(this);
  39. topLayout->addWidget(cefWidget);
  40. topLayout->addLayout(bottomLayout);
  41. #else
  42. UNUSED_PARAMETER(url);
  43. #endif
  44. }
  45. OAuthLogin::~OAuthLogin() {}
  46. int OAuthLogin::exec()
  47. {
  48. #ifdef BROWSER_AVAILABLE
  49. if (cefWidget) {
  50. return QDialog::exec();
  51. }
  52. #endif
  53. return QDialog::Rejected;
  54. }
  55. void OAuthLogin::reject()
  56. {
  57. #ifdef BROWSER_AVAILABLE
  58. delete cefWidget;
  59. #endif
  60. QDialog::reject();
  61. }
  62. void OAuthLogin::accept()
  63. {
  64. #ifdef BROWSER_AVAILABLE
  65. delete cefWidget;
  66. #endif
  67. QDialog::accept();
  68. }
  69. void OAuthLogin::urlChanged(const QString &url)
  70. {
  71. std::string uri = get_token ? "access_token=" : "code=";
  72. int code_idx = url.indexOf(uri.c_str());
  73. if (code_idx == -1)
  74. return;
  75. if (!url.startsWith(OAUTH_BASE_URL))
  76. return;
  77. code_idx += (int)uri.size();
  78. int next_idx = url.indexOf("&", code_idx);
  79. if (next_idx != -1)
  80. code = url.mid(code_idx, next_idx - code_idx);
  81. else
  82. code = url.right(url.size() - code_idx);
  83. accept();
  84. }