BBS2chProxyAuth.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include <pthread.h>
  3. #include <curl/curl.h>
  4. #include <string>
  5. #include "BBS2chProxyKeyManager.h"
  6. #include "BBS2chProxyFormData.h"
  7. class BBS2chProxyAuth {
  8. private:
  9. std::string sid;
  10. time_t expire;
  11. pthread_mutex_t mutex;
  12. public:
  13. BBS2chProxyAuth() : expire(0) {
  14. pthread_mutex_init(&mutex, NULL);
  15. };
  16. ~BBS2chProxyAuth() {
  17. pthread_mutex_destroy(&mutex);
  18. };
  19. std::string requestBodyForURL(const char *url, CURL *curl);
  20. const std::string& getSID();
  21. private:
  22. bool updateSID(CURL *curl);
  23. };
  24. class IBBS2chProxyCookieAuth {
  25. protected:
  26. time_t _lastUpdated;
  27. std::string _mail;
  28. std::string _passwd;
  29. pthread_mutex_t _mutex;
  30. bool _isUpdating;
  31. std::string _headerTitle;
  32. std::string _uniqueId;
  33. int _updateInterval;
  34. public:
  35. IBBS2chProxyCookieAuth() : _lastUpdated(), _isUpdating(), _updateInterval(60*60*12) {
  36. pthread_mutex_init(&_mutex, NULL);
  37. };
  38. virtual ~IBBS2chProxyCookieAuth() {
  39. pthread_mutex_destroy(&_mutex);
  40. };
  41. int loginWith(const std::string &mail, const std::string &passwd);
  42. void prepareForLogin(CURL *curl, const std::string &authURL, BBS2chProxyFormData &requestBody);
  43. void update(bool force);
  44. std::string responseHTML();
  45. virtual bool login() = 0;
  46. virtual void logout() = 0;
  47. virtual bool syncCookie(BBS2chProxyKeyManager::CookieJar &jar) = 0;
  48. virtual bool isLoggedIn() = 0;
  49. };
  50. class BBS2chProxyAcornAuth : public IBBS2chProxyCookieAuth {
  51. private:
  52. std::string _cookie;
  53. public:
  54. BBS2chProxyAcornAuth() : IBBS2chProxyCookieAuth() {
  55. _headerTitle = "どんぐりシステム";
  56. _uniqueId = "donguri";
  57. };
  58. bool login();
  59. void logout();
  60. bool syncCookie(BBS2chProxyKeyManager::CookieJar &jar);
  61. bool isLoggedIn();
  62. };
  63. class BBS2chProxyBeAuth : public IBBS2chProxyCookieAuth {
  64. private:
  65. std::string _dmdm;
  66. std::string _mdmd;
  67. public:
  68. BBS2chProxyBeAuth() : IBBS2chProxyCookieAuth() {
  69. _headerTitle = "Be";
  70. _uniqueId = "be";
  71. };
  72. bool login();
  73. void logout();
  74. bool syncCookie(BBS2chProxyKeyManager::CookieJar &jar);
  75. bool isLoggedIn();
  76. };
  77. class BBS2chProxyUpliftAuth : public IBBS2chProxyCookieAuth {
  78. private:
  79. std::string _cookie;
  80. public:
  81. BBS2chProxyUpliftAuth() : IBBS2chProxyCookieAuth() {
  82. _headerTitle = "UPLIFT";
  83. _uniqueId = "uplift";
  84. };
  85. bool login();
  86. void logout();
  87. bool syncCookie(BBS2chProxyKeyManager::CookieJar &jar);
  88. bool isLoggedIn();
  89. };
  90. class BBS2chProxyCookieAuthManager {
  91. private:
  92. BBS2chProxyAcornAuth _acornAuth;
  93. BBS2chProxyBeAuth _beAuth;
  94. BBS2chProxyUpliftAuth _upliftAuth;
  95. public:
  96. int loginWith(const std::string &service, const std::string &mail, const std::string &passwd);
  97. void logout(const std::string &service);
  98. void syncCookies(BBS2chProxyKeyManager::CookieJar &jar);
  99. std::string responseHTML();
  100. };