1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include "onlinecredentials.h" #include <QThread> #include <QFile> #include "nwutils.h" OnlineCredentials::OnlineCredentials() : lock(1000), mHasVerifiedCredentials(false) { } bool OnlineCredentials::readConfig(QString configFilePath) { if (!QFile(configFilePath).exists()) { return false; } std::string user, password, userAgent; nw::JsonReader jr(configFilePath.toStdString()); jr.describe("user", user); jr.describe("password", password); NwUtils::describe(jr, "userAgent", userAgent); jr.close(); if (user.length() > 0 && password.length() > 0) { this->set(user.data(), password.data(), userAgent.data()); } return true; } CURL* OnlineCredentials::curlNoAuthClientNoLock(const char* url, CurlResult& userdata) const { CURL* handle = userdata.curlClient(url); curl_easy_setopt(handle, CURLOPT_USERAGENT, userAgent.toLatin1().data()); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &userdata); return handle; } CURL* OnlineCredentials::curlClientNoLock(const char* url, CurlResult& userdata) const { CURL* handle = curlNoAuthClientNoLock(url, userdata); this->setCredentialsForHandle(userdata, handle); return handle; } void OnlineCredentials::setCredentialsForHandle(CurlResult& , CURL* handle) const { curl_easy_setopt(handle, CURLOPT_USERNAME, username.toUtf8().data()); curl_easy_setopt(handle, CURLOPT_PASSWORD, password.toUtf8().data()); } CURL*OnlineCredentials::curlClient(OnlineCredentials::TimeLock& lock, const char* url, CurlResult& userdata) const { while (lock.blockUntilReady()) { // sleeps as a side-effect } return this->curlClientNoLock(url, userdata); } CURL*OnlineCredentials::curlClient(const char* url, CurlResult& userdata) { return this->curlClient(lock, url, userdata); } CURL*OnlineCredentials::curlNoAuthClient(OnlineCredentials::TimeLock& lock, const char* url, CurlResult& userdata) const { while (lock.blockUntilReady()) { // sleeps as a side-effect } return this->curlNoAuthClientNoLock(url, userdata); } CURL*OnlineCredentials::curlNoAuthClient(const char* url, CurlResult& userdata) { return this->curlNoAuthClient(this->lock, url, userdata); } void OnlineCredentials::set(const QString name, const QString password) { this->username = name; this->password = password; } void OnlineCredentials::set(const QString name, const QString password, QString userAgent) { this->set(name, password); this->userAgent = userAgent; } bool OnlineCredentials::assureFreshness() { if (!this->isFresh()) { return this->refresh(); } return true; } bool OnlineCredentials::hasVerifiedCredentials() const { return mHasVerifiedCredentials; } QString OnlineCredentials::getUsername() const { return username; } OnlineCredentials::TimeLock::TimeLock(int timeToWaitInMs) : timeToWaitInMs(timeToWaitInMs) { } bool OnlineCredentials::TimeLock::blockUntilReady() { bool success = this->lock(); if (!success) { QThread::msleep(std::max(0, this->timeToWaitInMs - timer.elapsed())); return false; } return true; } bool OnlineCredentials::TimeLock::lock() { if (timer.isNull() || this->timer.elapsed() > this->timeToWaitInMs) { this->timer.start(); return true; } return false; }