1
0

auth-oauth.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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,
  33. delete_cookies_cb delete_cookies);
  34. protected:
  35. std::string refresh_token;
  36. std::string token;
  37. bool implicit = false;
  38. uint64_t expire_time = 0;
  39. int currentScopeVer = 0;
  40. virtual void SaveInternal() override;
  41. virtual bool LoadInternal() override;
  42. virtual bool RetryLogin() = 0;
  43. bool TokenExpired();
  44. bool GetToken(const char *url, const std::string &client_id,
  45. int scope_ver,
  46. const std::string &auth_code = std::string(),
  47. bool retry = false);
  48. };
  49. class OAuthStreamKey : public OAuth {
  50. Q_OBJECT
  51. protected:
  52. std::string key_;
  53. public:
  54. inline OAuthStreamKey(const Def &d) : OAuth(d) {}
  55. inline const std::string &key() const { return key_; }
  56. virtual void OnStreamConfig() override;
  57. };