Merge pull request #624 from nextcloud/fix/279/abstract_webflow_qnam

Add a WebFlowCredentialsAccessManager
This commit is contained in:
Roeland Jago Douma 2018-09-05 13:01:06 +02:00 committed by GitHub
commit 599f6f0228
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,6 +24,35 @@ namespace OCC {
Q_LOGGING_CATEGORY(lcWebFlowCredentials, "sync.credentials.webflow", QtInfoMsg) Q_LOGGING_CATEGORY(lcWebFlowCredentials, "sync.credentials.webflow", QtInfoMsg)
class WebFlowCredentialsAccessManager : public AccessManager
{
public:
WebFlowCredentialsAccessManager(const WebFlowCredentials *cred, QObject *parent = nullptr)
: AccessManager(parent)
, _cred(cred)
{
}
protected:
QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) Q_DECL_OVERRIDE
{
QNetworkRequest req(request);
if (!req.attribute(HttpCredentials::DontAddCredentialsAttribute).toBool()) {
if (_cred && !_cred->password().isEmpty()) {
QByteArray credHash = QByteArray(_cred->user().toUtf8() + ":" + _cred->password().toUtf8()).toBase64();
req.setRawHeader("Authorization", "Basic " + credHash);
}
}
return AccessManager::createRequest(op, req, outgoingData);
}
private:
// The credentials object dies along with the account, while the QNAM might
// outlive both.
QPointer<const WebFlowCredentials> _cred;
};
WebFlowCredentials::WebFlowCredentials() WebFlowCredentials::WebFlowCredentials()
: _ready(false), : _ready(false),
_credentialsValid(false) _credentialsValid(false)
@ -56,7 +85,7 @@ QString WebFlowCredentials::password() const {
QNetworkAccessManager *WebFlowCredentials::createQNAM() const { QNetworkAccessManager *WebFlowCredentials::createQNAM() const {
qCInfo(lcWebFlowCredentials()) << "Get QNAM"; qCInfo(lcWebFlowCredentials()) << "Get QNAM";
AccessManager *qnam = new AccessManager(); AccessManager *qnam = new WebFlowCredentialsAccessManager(this);
connect(qnam, &AccessManager::authenticationRequired, this, &WebFlowCredentials::slotAuthentication); connect(qnam, &AccessManager::authenticationRequired, this, &WebFlowCredentials::slotAuthentication);
connect(qnam, &AccessManager::finished, this, &WebFlowCredentials::slotFinished); connect(qnam, &AccessManager::finished, this, &WebFlowCredentials::slotFinished);