auth-oauth.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include <QDialog>
  3. #include <string>
  4. #include <memory>
  5. #include "auth-base.hpp"
  6. class QCefWidget;
  7. class OAuthLogin : public QDialog {
  8. Q_OBJECT
  9. QCefWidget *cefWidget = nullptr;
  10. QString code;
  11. bool get_token = false;
  12. bool fail = false;
  13. public:
  14. OAuthLogin(QWidget *parent, const std::string &url, bool token);
  15. ~OAuthLogin();
  16. inline QString GetCode() const { return code; }
  17. inline bool LoadFail() const { return fail; }
  18. virtual int exec() override;
  19. virtual void reject() override;
  20. virtual void accept() override;
  21. public slots:
  22. void urlChanged(const QString &url);
  23. };
  24. class OAuth : public Auth {
  25. Q_OBJECT
  26. public:
  27. inline OAuth(const Def &d) : Auth(d) {}
  28. typedef std::function<std::shared_ptr<Auth>(
  29. QWidget *, const std::string &service_name)>
  30. login_cb;
  31. typedef std::function<void()> delete_cookies_cb;
  32. static std::shared_ptr<Auth> Login(QWidget *parent,
  33. const std::string &service);
  34. static void DeleteCookies(const std::string &service);
  35. static void RegisterOAuth(const Def &d, create_cb create,
  36. login_cb login,
  37. delete_cookies_cb delete_cookies);
  38. protected:
  39. std::string refresh_token;
  40. std::string token;
  41. bool implicit = false;
  42. uint64_t expire_time = 0;
  43. int currentScopeVer = 0;
  44. virtual void SaveInternal() override;
  45. virtual bool LoadInternal() override;
  46. virtual bool RetryLogin() = 0;
  47. bool TokenExpired();
  48. bool GetToken(const char *url, const std::string &client_id,
  49. int scope_ver,
  50. const std::string &auth_code = std::string(),
  51. bool retry = false);
  52. bool GetToken(const char *url, const std::string &client_id,
  53. const std::string &secret,
  54. const std::string &redirect_uri, int scope_ver,
  55. const std::string &auth_code, bool retry);
  56. private:
  57. bool GetTokenInternal(const char *url, const std::string &client_id,
  58. const std::string &secret,
  59. const std::string &redirect_uri, int scope_ver,
  60. const std::string &auth_code, bool retry);
  61. };
  62. class OAuthStreamKey : public OAuth {
  63. Q_OBJECT
  64. protected:
  65. std::string key_;
  66. public:
  67. inline OAuthStreamKey(const Def &d) : OAuth(d) {}
  68. inline const std::string &key() const { return key_; }
  69. virtual void OnStreamConfig() override;
  70. };