From 9c20ff45555cbe74c9afa24fafe5fc0c4ff9e7b8 Mon Sep 17 00:00:00 2001 From: Javier Date: Mon, 8 Dec 2014 02:09:07 +0100 Subject: implement xhr authentication --- daemon/jskitobjects.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'daemon/jskitobjects.cpp') diff --git a/daemon/jskitobjects.cpp b/daemon/jskitobjects.cpp index 3386f16..5f5acaf 100644 --- a/daemon/jskitobjects.cpp +++ b/daemon/jskitobjects.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -201,6 +202,8 @@ JSKitXMLHttpRequest::JSKitXMLHttpRequest(JSKitManager *mgr, QObject *parent) _net(new QNetworkAccessManager(this)), _timeout(0), _reply(0) { logger()->debug() << "constructed"; + connect(_net, &QNetworkAccessManager::authenticationRequired, + this, &JSKitXMLHttpRequest::handleAuthenticationRequired); } JSKitXMLHttpRequest::~JSKitXMLHttpRequest() @@ -215,9 +218,13 @@ void JSKitXMLHttpRequest::open(const QString &method, const QString &url, bool a _reply = 0; } + _username = username; + _password = password; _request = QNetworkRequest(QUrl(url)); _verb = method; Q_UNUSED(async); + + logger()->debug() << "opened to URL" << _request.url().toString(); } void JSKitXMLHttpRequest::setRequestHeader(const QString &header, const QString &value) @@ -455,6 +462,22 @@ void JSKitXMLHttpRequest::handleReplyError(QNetworkReply::NetworkError code) } } +void JSKitXMLHttpRequest::handleAuthenticationRequired(QNetworkReply *reply, QAuthenticator *auth) +{ + if (_reply == reply) { + logger()->debug() << "authentication required"; + + if (!_username.isEmpty() || !_password.isEmpty()) { + logger()->debug() << "using provided authorization:" << _username; + + auth->setUser(_username); + auth->setPassword(_password); + } else { + logger()->debug() << "no username or password provided"; + } + } +} + JSKitGeolocation::JSKitGeolocation(JSKitManager *mgr) : QObject(mgr), _mgr(mgr), _source(0), _lastWatchId(0) { -- cgit v1.2.3