OAuth.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include "Auth.hpp"
  3. class OAuth : public Auth {
  4. Q_OBJECT
  5. public:
  6. inline OAuth(const Def &d) : Auth(d) {}
  7. typedef std::function<std::shared_ptr<Auth>(QWidget *, const std::string &service_name)> login_cb;
  8. typedef std::function<void()> delete_cookies_cb;
  9. static std::shared_ptr<Auth> Login(QWidget *parent, const std::string &service);
  10. static void DeleteCookies(const std::string &service);
  11. static void RegisterOAuth(const Def &d, create_cb create, login_cb login, delete_cookies_cb delete_cookies);
  12. protected:
  13. std::string refresh_token;
  14. std::string token;
  15. bool implicit = false;
  16. uint64_t expire_time = 0;
  17. int currentScopeVer = 0;
  18. virtual void SaveInternal() override;
  19. virtual bool LoadInternal() override;
  20. virtual bool RetryLogin() = 0;
  21. bool TokenExpired();
  22. bool GetToken(const char *url, const std::string &client_id, int scope_ver,
  23. const std::string &auth_code = std::string(), bool retry = false);
  24. bool GetToken(const char *url, const std::string &client_id, const std::string &secret,
  25. const std::string &redirect_uri, int scope_ver, const std::string &auth_code, bool retry);
  26. private:
  27. bool GetTokenInternal(const char *url, const std::string &client_id, const std::string &secret,
  28. const std::string &redirect_uri, int scope_ver, const std::string &auth_code, bool retry);
  29. };
  30. class OAuthStreamKey : public OAuth {
  31. Q_OBJECT
  32. protected:
  33. std::string key_;
  34. public:
  35. inline OAuthStreamKey(const Def &d) : OAuth(d) {}
  36. inline const std::string &key() const { return key_; }
  37. virtual void OnStreamConfig() override;
  38. };