auth-oauth.hpp 1.6 KB

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