OAuthLogin.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(this, 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. if (cefWidget) {
  59. cefWidget->closeBrowser();
  60. }
  61. #endif
  62. QDialog::reject();
  63. }
  64. void OAuthLogin::accept()
  65. {
  66. #ifdef BROWSER_AVAILABLE
  67. if (cefWidget) {
  68. cefWidget->closeBrowser();
  69. }
  70. #endif
  71. QDialog::accept();
  72. }
  73. void OAuthLogin::urlChanged(const QString &url)
  74. {
  75. std::string uri = get_token ? "access_token=" : "code=";
  76. int code_idx = url.indexOf(uri.c_str());
  77. if (code_idx == -1)
  78. return;
  79. if (!url.startsWith(OAUTH_BASE_URL))
  80. return;
  81. code_idx += (int)uri.size();
  82. int next_idx = url.indexOf("&", code_idx);
  83. if (next_idx != -1)
  84. code = url.mid(code_idx, next_idx - code_idx);
  85. else
  86. code = url.right(url.size() - code_idx);
  87. accept();
  88. }