auth-oauth.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. virtual void reject() override;
  20. virtual void accept() override;
  21. public slots:
  22. void urlChanged(const QString &url);
  23. };
  24. class OAuth : public Auth {
  25. Q_OBJECT
  26. public:
  27. inline OAuth(const Def &d) : Auth(d) {}
  28. typedef std::function<std::shared_ptr<Auth>(QWidget *, const std::string &service_name)> login_cb;
  29. typedef std::function<void()> delete_cookies_cb;
  30. static std::shared_ptr<Auth> Login(QWidget *parent, const std::string &service);
  31. static void DeleteCookies(const std::string &service);
  32. static void RegisterOAuth(const Def &d, create_cb create, 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, int scope_ver,
  44. const std::string &auth_code = std::string(), bool retry = false);
  45. bool GetToken(const char *url, const std::string &client_id, const std::string &secret,
  46. const std::string &redirect_uri, int scope_ver, const std::string &auth_code, bool retry);
  47. private:
  48. bool GetTokenInternal(const char *url, const std::string &client_id, const std::string &secret,
  49. const std::string &redirect_uri, int scope_ver, const std::string &auth_code, bool retry);
  50. };
  51. class OAuthStreamKey : public OAuth {
  52. Q_OBJECT
  53. protected:
  54. std::string key_;
  55. public:
  56. inline OAuthStreamKey(const Def &d) : OAuth(d) {}
  57. inline const std::string &key() const { return key_; }
  58. virtual void OnStreamConfig() override;
  59. };