auth-oauth.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. public slots:
  20. void urlChanged(const QString &url);
  21. };
  22. class OAuth : public Auth {
  23. Q_OBJECT
  24. public:
  25. inline OAuth(const Def &d) : Auth(d) {}
  26. typedef std::function<std::shared_ptr<Auth>(
  27. QWidget *, const std::string &service_name)>
  28. login_cb;
  29. typedef std::function<void()> delete_cookies_cb;
  30. static std::shared_ptr<Auth> Login(QWidget *parent,
  31. const std::string &service);
  32. static void DeleteCookies(const std::string &service);
  33. static void RegisterOAuth(const Def &d, create_cb create,
  34. login_cb login,
  35. delete_cookies_cb delete_cookies);
  36. protected:
  37. std::string refresh_token;
  38. std::string token;
  39. bool implicit = false;
  40. uint64_t expire_time = 0;
  41. int currentScopeVer = 0;
  42. virtual void SaveInternal() override;
  43. virtual bool LoadInternal() override;
  44. virtual bool RetryLogin() = 0;
  45. bool TokenExpired();
  46. bool GetToken(const char *url, const std::string &client_id,
  47. int scope_ver,
  48. const std::string &auth_code = std::string(),
  49. bool retry = false);
  50. bool GetToken(const char *url, const std::string &client_id,
  51. const std::string &secret,
  52. const std::string &redirect_uri, int scope_ver,
  53. const std::string &auth_code, bool retry);
  54. private:
  55. bool GetTokenInternal(const char *url, const std::string &client_id,
  56. const std::string &secret,
  57. const std::string &redirect_uri, int scope_ver,
  58. const std::string &auth_code, bool retry);
  59. };
  60. class OAuthStreamKey : public OAuth {
  61. Q_OBJECT
  62. protected:
  63. std::string key_;
  64. public:
  65. inline OAuthStreamKey(const Def &d) : OAuth(d) {}
  66. inline const std::string &key() const { return key_; }
  67. virtual void OnStreamConfig() override;
  68. };