auth-oauth.hpp 1.6 KB

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