| 1 |
/** |
| 2 |
* KQOAuth - An OAuth authentication library for Qt. |
| 3 |
* |
| 4 |
* Author: Johan Paul ([email protected]) |
| 5 |
* http://www.johanpaul.com |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* KQOAuth is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with KQOAuth. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
#include <QtCore> |
| 21 |
#include <QDesktopServices> |
| 22 |
#if QT_VERSION >= 0x050000 |
| 23 |
#include <QUrlQuery> |
| 24 |
#endif |
| 25 |
#include <QNetworkCookie> |
| 26 |
|
| 27 |
#include "kqoauthmanager.h" |
| 28 |
#include "kqoauthmanager_p.h" |
| 29 |
|
| 30 |
|
| 31 |
////////////// Private d_ptr implementation //////////////// |
| 32 |
|
| 33 |
KQOAuthManagerPrivate::KQOAuthManagerPrivate(KQOAuthManager *parent) : |
| 34 |
error(KQOAuthManager::NoError) , |
| 35 |
r(0) , |
| 36 |
opaqueRequest(new KQOAuthRequest) , |
| 37 |
q_ptr(parent) , |
| 38 |
callbackServer(new KQOAuthAuthReplyServer(parent)) , |
| 39 |
isVerified(false) , |
| 40 |
isAuthorized(false) , |
| 41 |
autoAuth(false), |
| 42 |
handleAuthPageOpening(true), |
| 43 |
networkManager(new QNetworkAccessManager), |
| 44 |
managerUserSet(false) |
| 45 |
{ |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
KQOAuthManagerPrivate::~KQOAuthManagerPrivate() { |
| 50 |
delete opaqueRequest; |
| 51 |
opaqueRequest = 0; |
| 52 |
|
| 53 |
if (!managerUserSet) { |
| 54 |
delete networkManager; |
| 55 |
networkManager = 0; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
QList< QPair<QString, QString> > KQOAuthManagerPrivate::createQueryParams(const KQOAuthParameters &requestParams) { |
| 60 |
QList<QString> requestKeys = requestParams.keys(); |
| 61 |
QList<QString> requestValues = requestParams.values(); |
| 62 |
|
| 63 |
QList< QPair<QString, QString> > result; |
| 64 |
for(int i=0; i<requestKeys.size(); i++) { |
| 65 |
result.append( qMakePair(requestKeys.at(i), |
| 66 |
requestValues.at(i)) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
QMultiMap<QString, QString> KQOAuthManagerPrivate::createTokensFromResponse(QByteArray reply) { |
| 74 |
QMultiMap<QString, QString> result; |
| 75 |
QString replyString(reply); |
| 76 |
|
| 77 |
QStringList parameterPairs = replyString.split('&', QString::SkipEmptyParts); |
| 78 |
foreach (const QString ¶meterPair, parameterPairs) { |
| 79 |
QStringList parameter = parameterPair.split('='); |
| 80 |
result.insert(parameter.value(0), parameter.value(1)); |
| 81 |
} |
| 82 |
|
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
bool KQOAuthManagerPrivate::setSuccessfulRequestToken(const QMultiMap<QString, QString> &request) { |
| 87 |
if (currentRequestType == KQOAuthRequest::TemporaryCredentials) { |
| 88 |
hasTemporaryToken = (!QString(request.value("oauth_token")).isEmpty() && !QString(request.value("oauth_token_secret")).isEmpty()); |
| 89 |
} else { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if (hasTemporaryToken) { |
| 94 |
requestToken = QUrl::fromPercentEncoding( QString(request.value("oauth_token")).toLocal8Bit() ); |
| 95 |
requestTokenSecret = QUrl::fromPercentEncoding( QString(request.value("oauth_token_secret")).toLocal8Bit() ); |
| 96 |
} |
| 97 |
|
| 98 |
return hasTemporaryToken; |
| 99 |
} |
| 100 |
|
| 101 |
bool KQOAuthManagerPrivate::setSuccessfulAuthorized(const QMultiMap<QString, QString> &request ) { |
| 102 |
if (currentRequestType == KQOAuthRequest::AccessToken) { |
| 103 |
isAuthorized = (!QString(request.value("oauth_token")).isEmpty() && !QString(request.value("oauth_token_secret")).isEmpty()); |
| 104 |
} else { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
if (isAuthorized) { |
| 109 |
requestToken = QUrl::fromPercentEncoding( QString(request.value("oauth_token")).toLocal8Bit() ); |
| 110 |
requestTokenSecret = QUrl::fromPercentEncoding( QString(request.value("oauth_token_secret")).toLocal8Bit() ); |
| 111 |
} |
| 112 |
|
| 113 |
return isAuthorized; |
| 114 |
} |
| 115 |
|
| 116 |
void KQOAuthManagerPrivate::emitTokens() { |
| 117 |
Q_Q(KQOAuthManager); |
| 118 |
|
| 119 |
if (this->requestToken.isEmpty() || this->requestTokenSecret.isEmpty()) { |
| 120 |
error = KQOAuthManager::RequestUnauthorized; |
| 121 |
} |
| 122 |
|
| 123 |
if (currentRequestType == KQOAuthRequest::TemporaryCredentials) { |
| 124 |
// Signal that we are ready to use the protected resources. |
| 125 |
emit q->temporaryTokenReceived(this->requestToken, this->requestTokenSecret); |
| 126 |
} |
| 127 |
|
| 128 |
if (currentRequestType == KQOAuthRequest::AccessToken) { |
| 129 |
// Signal that we are ready to use the protected resources. |
| 130 |
emit q->accessTokenReceived(this->requestToken, this->requestTokenSecret); |
| 131 |
} |
| 132 |
|
| 133 |
emit q->receivedToken(this->requestToken, this->requestTokenSecret); |
| 134 |
} |
| 135 |
|
| 136 |
bool KQOAuthManagerPrivate::setupCallbackServer() { |
| 137 |
return callbackServer->listen(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/////////////// Public implementation //////////////// |
| 142 |
|
| 143 |
KQOAuthManager::KQOAuthManager(QObject *parent) : |
| 144 |
QObject(parent) , |
| 145 |
d_ptr(new KQOAuthManagerPrivate(this)) |
| 146 |
{ |
| 147 |
|
| 148 |
qsrand(QTime::currentTime().msec()); // We need to seed the nonce random number with something. |
| 149 |
// However, we cannot do this while generating the nonce since |
| 150 |
// we might get the same seed. So initializing here should be fine. |
| 151 |
} |
| 152 |
|
| 153 |
KQOAuthManager::~KQOAuthManager() |
| 154 |
{ |
| 155 |
delete d_ptr; |
| 156 |
} |
| 157 |
|
| 158 |
void KQOAuthManager::executeRequest(KQOAuthRequest *request) { |
| 159 |
Q_D(KQOAuthManager); |
| 160 |
|
| 161 |
d->r = request; |
| 162 |
|
| 163 |
if (request == 0) { |
| 164 |
qWarning() << "Request is NULL. Cannot proceed."; |
| 165 |
d->error = KQOAuthManager::RequestError; |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
if (!request->requestEndpoint().isValid()) { |
| 170 |
qWarning() << "Request endpoint URL is not valid. Cannot proceed."; |
| 171 |
d->error = KQOAuthManager::RequestEndpointError; |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if (!request->isValid()) { |
| 176 |
qWarning() << "Request is not valid. Cannot proceed."; |
| 177 |
d->error = KQOAuthManager::RequestValidationError; |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
d->currentRequestType = request->requestType(); |
| 182 |
|
| 183 |
QNetworkRequest networkRequest; |
| 184 |
networkRequest.setUrl( request->requestEndpoint() ); |
| 185 |
|
| 186 |
if (d->autoAuth && d->currentRequestType == KQOAuthRequest::TemporaryCredentials) { |
| 187 |
d->setupCallbackServer(); |
| 188 |
connect(d->callbackServer, SIGNAL(verificationReceived(QMultiMap<QString, QString>)), |
| 189 |
this, SLOT( onVerificationReceived(QMultiMap<QString, QString>))); |
| 190 |
|
| 191 |
QString serverString = "http://localhost:"; |
| 192 |
serverString.append(QString::number(d->callbackServer->serverPort())); |
| 193 |
request->setCallbackUrl(QUrl(serverString)); |
| 194 |
} |
| 195 |
|
| 196 |
// And now fill the request with "Authorization" header data. |
| 197 |
QList<QByteArray> requestHeaders = request->requestParameters(); |
| 198 |
QByteArray authHeader; |
| 199 |
|
| 200 |
bool first = true; |
| 201 |
foreach (const QByteArray header, requestHeaders) { |
| 202 |
if (!first) { |
| 203 |
authHeader.append(", "); |
| 204 |
} else { |
| 205 |
authHeader.append("OAuth "); |
| 206 |
first = false; |
| 207 |
} |
| 208 |
|
| 209 |
authHeader.append(header); |
| 210 |
} |
| 211 |
networkRequest.setRawHeader("Authorization", authHeader); |
| 212 |
|
| 213 |
if (request->httpMethod() == KQOAuthRequest::GET) { |
| 214 |
// Get the requested additional params as a list of pairs we can give QUrl |
| 215 |
QList< QPair<QString, QString> > urlParams = d->createQueryParams(request->additionalParameters()); |
| 216 |
|
| 217 |
// Take the original URL and append the query params to it. |
| 218 |
QUrl urlWithParams = networkRequest.url(); |
| 219 |
#if QT_VERSION < 0x050000 |
| 220 |
urlWithParams.setQueryItems(urlParams); |
| 221 |
#else |
| 222 |
QUrlQuery query; |
| 223 |
query.setQueryItems(urlParams); |
| 224 |
urlWithParams.setQuery(query); |
| 225 |
#endif |
| 226 |
networkRequest.setUrl(urlWithParams); |
| 227 |
|
| 228 |
// Submit the request including the params. |
| 229 |
QNetworkReply *reply = d->networkManager->get(networkRequest); |
| 230 |
connect(reply, SIGNAL(finished()), |
| 231 |
this, SLOT(onRequestReplyReceived())); |
| 232 |
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 233 |
this, SLOT(slotError(QNetworkReply::NetworkError))); |
| 234 |
d->requestMap.insert( request, reply ); |
| 235 |
|
| 236 |
} else if (request->httpMethod() == KQOAuthRequest::POST) { |
| 237 |
QNetworkReply *reply; |
| 238 |
if (request->contentType() == "application/x-www-form-urlencoded") { |
| 239 |
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, request->contentType()); |
| 240 |
reply = d->networkManager->post(networkRequest, request->requestBody()); |
| 241 |
} else if (request->contentType() == "multipart/form-data") { |
| 242 |
reply = d->networkManager->post(networkRequest, request->httpMultiPart()); |
| 243 |
} else { |
| 244 |
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, request->contentType()); |
| 245 |
reply = d->networkManager->post(networkRequest, request->rawData()); |
| 246 |
} |
| 247 |
connect(reply, SIGNAL(finished()), |
| 248 |
this, SLOT(onRequestReplyReceived()), Qt::UniqueConnection); |
| 249 |
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 250 |
this, SLOT(slotError(QNetworkReply::NetworkError))); |
| 251 |
d->requestMap.insert( request, reply ); |
| 252 |
} |
| 253 |
|
| 254 |
d->r->requestTimerStart(); |
| 255 |
} |
| 256 |
|
| 257 |
void KQOAuthManager::executeAuthorizedRequest(KQOAuthRequest *request, int id) { |
| 258 |
Q_D(KQOAuthManager); |
| 259 |
|
| 260 |
d->r = request; |
| 261 |
|
| 262 |
if (request == 0) { |
| 263 |
qWarning() << "Request is NULL. Cannot proceed."; |
| 264 |
d->error = KQOAuthManager::RequestError; |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
if (!request->requestEndpoint().isValid()) { |
| 269 |
qWarning() << "Request endpoint URL is not valid. Cannot proceed."; |
| 270 |
d->error = KQOAuthManager::RequestEndpointError; |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
if (!request->isValid()) { |
| 275 |
qWarning() << "Request is not valid. Cannot proceed."; |
| 276 |
d->error = KQOAuthManager::RequestValidationError; |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
d->currentRequestType = request->requestType(); |
| 281 |
|
| 282 |
QNetworkRequest networkRequest; |
| 283 |
networkRequest.setUrl( request->requestEndpoint() ); |
| 284 |
|
| 285 |
if ( d->currentRequestType != KQOAuthRequest::AuthorizedRequest){ |
| 286 |
qWarning() << "Not Authorized Request. Cannot proceed"; |
| 287 |
d->error = KQOAuthManager::RequestError; |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
// And now fill the request with "Authorization" header data. |
| 293 |
QList<QByteArray> requestHeaders = request->requestParameters(); |
| 294 |
QByteArray authHeader; |
| 295 |
|
| 296 |
bool first = true; |
| 297 |
foreach (const QByteArray header, requestHeaders) { |
| 298 |
if (!first) { |
| 299 |
authHeader.append(", "); |
| 300 |
} else { |
| 301 |
authHeader.append("OAuth "); |
| 302 |
first = false; |
| 303 |
} |
| 304 |
|
| 305 |
authHeader.append(header); |
| 306 |
} |
| 307 |
networkRequest.setRawHeader("Authorization", authHeader); |
| 308 |
|
| 309 |
QNetworkReply *reply = 0; |
| 310 |
|
| 311 |
if (request->httpMethod() == KQOAuthRequest::POST) { |
| 312 |
|
| 313 |
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, request->contentType()); |
| 314 |
|
| 315 |
/* |
| 316 |
qDebug() << networkRequest.rawHeaderList(); |
| 317 |
qDebug() << networkRequest.rawHeader("Authorization"); |
| 318 |
qDebug() << networkRequest.rawHeader("Content-Type"); |
| 319 |
*/ |
| 320 |
|
| 321 |
if (request->contentType() == "application/x-www-form-urlencoded") { |
| 322 |
reply = d->networkManager->post(networkRequest, request->requestBody()); |
| 323 |
} else if (request->contentType() == "multipart/form-data") { |
| 324 |
reply = d->networkManager->post(networkRequest, request->httpMultiPart()); |
| 325 |
} else { |
| 326 |
reply = d->networkManager->post(networkRequest, request->rawData()); |
| 327 |
} |
| 328 |
connect(reply, SIGNAL(finished(QNetworkReply *)), |
| 329 |
this, SLOT(onAuthorizedRequestReplyReceived(QNetworkReply*)), Qt::UniqueConnection); |
| 330 |
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 331 |
this, SLOT(slotError(QNetworkReply::NetworkError))); |
| 332 |
connect(request, SIGNAL(requestTimedout()), |
| 333 |
this, SLOT(requestTimeout())); |
| 334 |
d->requestMap.insert( request, reply ); |
| 335 |
} else { |
| 336 |
// Get the requested additional params as a list of pairs we can give QUrl |
| 337 |
QList< QPair<QString, QString> > urlParams = d->createQueryParams(request->additionalParameters()); |
| 338 |
|
| 339 |
// Take the original URL and append the query params to it. |
| 340 |
QUrl urlWithParams = networkRequest.url(); |
| 341 |
#if QT_VERSION < 0x050000 |
| 342 |
urlWithParams.setQueryItems(urlParams); |
| 343 |
#else |
| 344 |
QUrlQuery query; |
| 345 |
query.setQueryItems(urlParams); |
| 346 |
urlWithParams.setQuery(query); |
| 347 |
#endif |
| 348 |
networkRequest.setUrl(urlWithParams); |
| 349 |
|
| 350 |
// Submit the request including the params. |
| 351 |
if (request->httpMethod() == KQOAuthRequest::GET) |
| 352 |
reply = d->networkManager->get(networkRequest); |
| 353 |
else if (request->httpMethod() == KQOAuthRequest::HEAD) |
| 354 |
reply = d->networkManager->head(networkRequest); |
| 355 |
else if (request->httpMethod() == KQOAuthRequest::DELETE) |
| 356 |
reply = d->networkManager->deleteResource(networkRequest); |
| 357 |
|
| 358 |
connect(reply, SIGNAL(finished()), |
| 359 |
this, SLOT(onAuthorizedRequestReplyReceived())); |
| 360 |
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 361 |
this, SLOT(slotError(QNetworkReply::NetworkError))); |
| 362 |
connect(request, SIGNAL(requestTimedout()), |
| 363 |
this, SLOT(requestTimeout())); |
| 364 |
d->requestMap.insert( request, reply ); |
| 365 |
} |
| 366 |
d->requestIds.insert(reply, id); |
| 367 |
d->r->requestTimerStart(); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
void KQOAuthManager::setHandleUserAuthorization(bool set) { |
| 372 |
Q_D(KQOAuthManager); |
| 373 |
|
| 374 |
d->autoAuth = set; |
| 375 |
} |
| 376 |
|
| 377 |
void KQOAuthManager::setHandleAuthorizationPageOpening(bool set) { |
| 378 |
Q_D(KQOAuthManager); |
| 379 |
|
| 380 |
d->handleAuthPageOpening = set; |
| 381 |
} |
| 382 |
|
| 383 |
bool KQOAuthManager::hasTemporaryToken() { |
| 384 |
Q_D(KQOAuthManager); |
| 385 |
|
| 386 |
return d->hasTemporaryToken; |
| 387 |
} |
| 388 |
|
| 389 |
bool KQOAuthManager::isVerified() { |
| 390 |
Q_D(KQOAuthManager); |
| 391 |
|
| 392 |
return d->isVerified; |
| 393 |
} |
| 394 |
|
| 395 |
bool KQOAuthManager::isAuthorized() { |
| 396 |
Q_D(KQOAuthManager); |
| 397 |
|
| 398 |
return d->isAuthorized; |
| 399 |
} |
| 400 |
|
| 401 |
KQOAuthManager::KQOAuthError KQOAuthManager::lastError() { |
| 402 |
Q_D(KQOAuthManager); |
| 403 |
|
| 404 |
return d->error; |
| 405 |
} |
| 406 |
|
| 407 |
void KQOAuthManager::setNetworkManager(QNetworkAccessManager *manager) { |
| 408 |
Q_D(KQOAuthManager); |
| 409 |
|
| 410 |
if (manager == 0) { |
| 411 |
d->error = KQOAuthManager::ManagerError; |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
if (!d->managerUserSet) { |
| 416 |
delete d->networkManager; |
| 417 |
} |
| 418 |
|
| 419 |
d->managerUserSet = true; |
| 420 |
d->networkManager = manager; |
| 421 |
} |
| 422 |
|
| 423 |
QNetworkAccessManager * KQOAuthManager::networkManager() const { |
| 424 |
Q_D(const KQOAuthManager); |
| 425 |
|
| 426 |
if (d->managerUserSet) { |
| 427 |
return d->networkManager; |
| 428 |
} else { |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
//////////// Public convenience API ///////////// |
| 436 |
|
| 437 |
void KQOAuthManager::getUserAuthorization(QUrl authorizationEndpoint) { |
| 438 |
Q_D(KQOAuthManager); |
| 439 |
|
| 440 |
if (!d->hasTemporaryToken) { |
| 441 |
qWarning() << "No temporary tokens retreieved. Cannot get user authorization."; |
| 442 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
if (!authorizationEndpoint.isValid()) { |
| 447 |
qWarning() << "Authorization endpoint not valid. Cannot proceed."; |
| 448 |
d->error = KQOAuthManager::RequestEndpointError; |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
d->error = KQOAuthManager::NoError; |
| 453 |
|
| 454 |
QPair<QString, QString> tokenParam = qMakePair(QString("oauth_token"), QString(d->requestToken)); |
| 455 |
QUrl openWebPageUrl(authorizationEndpoint.toString(), QUrl::StrictMode); |
| 456 |
|
| 457 |
#if QT_VERSION < 0x050000 |
| 458 |
openWebPageUrl.addQueryItem(tokenParam.first, tokenParam.second); |
| 459 |
#else |
| 460 |
QUrlQuery query(openWebPageUrl); |
| 461 |
query.addQueryItem(tokenParam.first, tokenParam.second); |
| 462 |
openWebPageUrl.setQuery(query); |
| 463 |
#endif |
| 464 |
|
| 465 |
if (d->handleAuthPageOpening) { |
| 466 |
// Open the user's default browser to the resource authorization page provided |
| 467 |
// by the service. |
| 468 |
QDesktopServices::openUrl(openWebPageUrl); |
| 469 |
} else { |
| 470 |
emit authorizationPageRequested(openWebPageUrl); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
void KQOAuthManager::getUserAccessTokens(QUrl accessTokenEndpoint) { |
| 475 |
Q_D(KQOAuthManager); |
| 476 |
|
| 477 |
if (!d->isVerified) { |
| 478 |
qWarning() << "Not verified. Cannot get access tokens."; |
| 479 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 480 |
return; |
| 481 |
} |
| 482 |
|
| 483 |
if (!accessTokenEndpoint.isValid()) { |
| 484 |
qWarning() << "Endpoint for access token exchange is not valid. Cannot proceed."; |
| 485 |
d->error = KQOAuthManager::RequestEndpointError; |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
d->error = KQOAuthManager::NoError; |
| 490 |
|
| 491 |
d->opaqueRequest->clearRequest(); |
| 492 |
d->opaqueRequest->initRequest(KQOAuthRequest::AccessToken, accessTokenEndpoint); |
| 493 |
d->opaqueRequest->setToken(d->requestToken); |
| 494 |
d->opaqueRequest->setTokenSecret(d->requestTokenSecret); |
| 495 |
d->opaqueRequest->setVerifier(d->requestVerifier); |
| 496 |
d->opaqueRequest->setConsumerKey(d->consumerKey); |
| 497 |
d->opaqueRequest->setConsumerSecretKey(d->consumerKeySecret); |
| 498 |
d->opaqueRequest->setSignatureMethod(d->signatureMethod); |
| 499 |
|
| 500 |
executeRequest(d->opaqueRequest); |
| 501 |
} |
| 502 |
|
| 503 |
void KQOAuthManager::verifyToken(const QString &token, const QString &verifier) { |
| 504 |
QMultiMap<QString, QString> params; |
| 505 |
params.insert("oauth_token", token); |
| 506 |
params.insert("oauth_verifier", verifier); |
| 507 |
onVerificationReceived(params); |
| 508 |
} |
| 509 |
|
| 510 |
void KQOAuthManager::sendAuthorizedRequest(QUrl requestEndpoint, const KQOAuthParameters &requestParameters) { |
| 511 |
Q_D(KQOAuthManager); |
| 512 |
|
| 513 |
if (!d->isAuthorized) { |
| 514 |
qWarning() << "No access tokens retrieved. Cannot send authorized requests."; |
| 515 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 516 |
return; |
| 517 |
} |
| 518 |
|
| 519 |
if (!requestEndpoint.isValid()) { |
| 520 |
qWarning() << "Endpoint for authorized request is not valid. Cannot proceed."; |
| 521 |
d->error = KQOAuthManager::RequestEndpointError; |
| 522 |
return; |
| 523 |
} |
| 524 |
|
| 525 |
d->error = KQOAuthManager::NoError; |
| 526 |
|
| 527 |
d->opaqueRequest->clearRequest(); |
| 528 |
d->opaqueRequest->initRequest(KQOAuthRequest::AuthorizedRequest, requestEndpoint); |
| 529 |
d->opaqueRequest->setAdditionalParameters(requestParameters); |
| 530 |
d->opaqueRequest->setToken(d->requestToken); |
| 531 |
d->opaqueRequest->setTokenSecret(d->requestTokenSecret); |
| 532 |
d->opaqueRequest->setConsumerKey(d->consumerKey); |
| 533 |
d->opaqueRequest->setConsumerSecretKey(d->consumerKeySecret); |
| 534 |
|
| 535 |
executeRequest(d->opaqueRequest); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
/////////////// Private slots ////////////////// |
| 540 |
|
| 541 |
void KQOAuthManager::onRequestReplyReceived() { |
| 542 |
Q_D(KQOAuthManager); |
| 543 |
QNetworkReply* reply = qobject_cast<QNetworkReply*>(QObject::sender()); |
| 544 |
QNetworkReply::NetworkError networkError = reply->error(); |
| 545 |
switch (networkError) { |
| 546 |
case QNetworkReply::NoError: |
| 547 |
d->error = KQOAuthManager::NoError; |
| 548 |
break; |
| 549 |
|
| 550 |
case QNetworkReply::ContentAccessDenied: |
| 551 |
case QNetworkReply::UnknownContentError: |
| 552 |
case QNetworkReply::AuthenticationRequiredError: |
| 553 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 554 |
break; |
| 555 |
case QNetworkReply::ContentNotFoundError: |
| 556 |
d->error = KQOAuthManager::ContentNotFound; |
| 557 |
break; |
| 558 |
case QNetworkReply::ContentOperationNotPermittedError: |
| 559 |
d->error = KQOAuthManager::ContentOperationNotPermittedError; |
| 560 |
break; |
| 561 |
default: |
| 562 |
d->error = KQOAuthManager::NetworkError; |
| 563 |
break; |
| 564 |
} |
| 565 |
|
| 566 |
// Let's disconnect this slot first |
| 567 |
/* |
| 568 |
disconnect(d->networkManager, SIGNAL(finished(QNetworkReply *)), |
| 569 |
this, SLOT(onRequestReplyReceived(QNetworkReply *))); |
| 570 |
*/ |
| 571 |
|
| 572 |
// Read the content of the reply from the network. |
| 573 |
QByteArray networkReply = reply->readAll(); |
| 574 |
|
| 575 |
d->r = d->requestMap.key(reply); |
| 576 |
if( d->r ) { |
| 577 |
d->requestMap.remove(d->r); |
| 578 |
disconnect(d->r, SIGNAL(requestTimedout()), |
| 579 |
this, SLOT(requestTimeout())); |
| 580 |
// Stop any timer we have set on the request. |
| 581 |
d->r->requestTimerStop(); |
| 582 |
d->currentRequestType = d->r->requestType(); |
| 583 |
} |
| 584 |
|
| 585 |
QVariant v = reply->header(QNetworkRequest::SetCookieHeader); |
| 586 |
|
| 587 |
/*QList<QNetworkCookie> c = qvariant_cast<QList<QNetworkCookie> >(v); |
| 588 |
foreach (QNetworkCookie cookie , c) |
| 589 |
qDebug() << cookie.name() << cookie.value();*/ |
| 590 |
|
| 591 |
// We need to emit the signal even if we got an error. |
| 592 |
if (d->error != KQOAuthManager::NoError) { |
| 593 |
reply->deleteLater(); |
| 594 |
emit requestReady(networkReply); |
| 595 |
//d->emitTokens(); |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
// Just don't do anything if we didn't get anything useful. |
| 600 |
if(networkReply.isEmpty()) { |
| 601 |
reply->deleteLater(); |
| 602 |
return; |
| 603 |
} |
| 604 |
QMultiMap<QString, QString> responseTokens; |
| 605 |
|
| 606 |
responseTokens = d->createTokensFromResponse(networkReply); |
| 607 |
d->opaqueRequest->clearRequest(); |
| 608 |
d->opaqueRequest->setHttpMethod(KQOAuthRequest::POST); // XXX FIXME: Convenient API does not support GET |
| 609 |
emit requestReady(networkReply); |
| 610 |
if (!d->isAuthorized || !d->isVerified) { |
| 611 |
if (d->setSuccessfulRequestToken(responseTokens)) { |
| 612 |
//qDebug() << "Successfully got request tokens."; |
| 613 |
d->consumerKey = d->r->consumerKeyForManager(); |
| 614 |
d->consumerKeySecret = d->r->consumerKeySecretForManager(); |
| 615 |
d->signatureMethod = d->r->requestSignatureMethodForManager(); |
| 616 |
d->opaqueRequest->setSignatureMethod(KQOAuthRequest::HMAC_SHA1); |
| 617 |
d->opaqueRequest->setCallbackUrl(d->r->callbackUrlForManager()); |
| 618 |
|
| 619 |
d->emitTokens(); |
| 620 |
|
| 621 |
} else if (d->setSuccessfulAuthorized(responseTokens)) { |
| 622 |
//qDebug() << "Successfully got access tokens."; |
| 623 |
d->opaqueRequest->setSignatureMethod(KQOAuthRequest::HMAC_SHA1); |
| 624 |
d->emitTokens(); |
| 625 |
} else if (d->currentRequestType == KQOAuthRequest::AuthorizedRequest) { |
| 626 |
emit authorizedRequestDone(); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
reply->deleteLater(); // We need to clean this up, after the event processing is done. |
| 631 |
} |
| 632 |
|
| 633 |
void KQOAuthManager::onAuthorizedRequestReplyReceived() { |
| 634 |
Q_D(KQOAuthManager); |
| 635 |
QNetworkReply* reply = qobject_cast<QNetworkReply*>(QObject::sender()); |
| 636 |
QNetworkReply::NetworkError networkError = reply->error(); |
| 637 |
switch (networkError) { |
| 638 |
case QNetworkReply::NoError: |
| 639 |
d->error = KQOAuthManager::NoError; |
| 640 |
break; |
| 641 |
|
| 642 |
case QNetworkReply::ContentAccessDenied: |
| 643 |
case QNetworkReply::UnknownContentError: |
| 644 |
case QNetworkReply::AuthenticationRequiredError: |
| 645 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 646 |
break; |
| 647 |
case QNetworkReply::ContentNotFoundError: |
| 648 |
d->error = KQOAuthManager::ContentNotFound; |
| 649 |
break; |
| 650 |
case QNetworkReply::ContentOperationNotPermittedError: |
| 651 |
d->error = KQOAuthManager::ContentOperationNotPermittedError; |
| 652 |
break; |
| 653 |
default: |
| 654 |
d->error = KQOAuthManager::NetworkError; |
| 655 |
break; |
| 656 |
} |
| 657 |
|
| 658 |
/* |
| 659 |
disconnect(d->networkManager, SIGNAL(finished(QNetworkReply *)), |
| 660 |
this, SLOT(onAuthorizedRequestReplyReceived(QNetworkReply *))); |
| 661 |
*/ |
| 662 |
|
| 663 |
// Read the content of the reply from the network. |
| 664 |
QByteArray networkReply = reply->readAll(); |
| 665 |
|
| 666 |
int id = d->requestIds.take(reply); |
| 667 |
d->r = d->requestMap.key(reply); |
| 668 |
if( d->r ) { |
| 669 |
d->requestMap.remove(d->r); |
| 670 |
disconnect(d->r, SIGNAL(requestTimedout()), |
| 671 |
this, SLOT(requestTimeout())); |
| 672 |
|
| 673 |
// Stop any timer we have set on the request. |
| 674 |
d->r->requestTimerStop(); |
| 675 |
d->currentRequestType = d->r->requestType(); |
| 676 |
} |
| 677 |
|
| 678 |
// Just don't do anything if we didn't get anything useful. |
| 679 |
if(networkReply.isEmpty()) { |
| 680 |
reply->deleteLater(); |
| 681 |
return; |
| 682 |
} |
| 683 |
|
| 684 |
// We need to emit the signal even if we got an error. |
| 685 |
if (d->error != KQOAuthManager::NoError) { |
| 686 |
qWarning() << "Network reply error"; |
| 687 |
return; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
d->opaqueRequest->clearRequest(); |
| 692 |
d->opaqueRequest->setHttpMethod(KQOAuthRequest::POST); // XXX FIXME: Convenient API does not support GET |
| 693 |
if (d->currentRequestType == KQOAuthRequest::AuthorizedRequest) { |
| 694 |
emit authorizedRequestDone(); |
| 695 |
} |
| 696 |
|
| 697 |
emit authorizedRequestReady(networkReply, id); |
| 698 |
reply->deleteLater(); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
void KQOAuthManager::onVerificationReceived(QMultiMap<QString, QString> response) { |
| 703 |
Q_D(KQOAuthManager); |
| 704 |
|
| 705 |
QString token = response.value("oauth_token"); |
| 706 |
QString verifier = response.value("oauth_verifier"); |
| 707 |
if (verifier.isEmpty()) { |
| 708 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 709 |
} |
| 710 |
|
| 711 |
verifier = QUrl::fromPercentEncoding(verifier.toUtf8()); // We get the raw URL response here so we need to convert it back |
| 712 |
// to plain string so we can percent encode it again later in requests. |
| 713 |
|
| 714 |
if (d->error == KQOAuthManager::NoError) { |
| 715 |
d->requestVerifier = verifier; |
| 716 |
d->isVerified = true; |
| 717 |
} |
| 718 |
|
| 719 |
emit authorizationReceived(token, verifier); |
| 720 |
} |
| 721 |
|
| 722 |
void KQOAuthManager::slotError(QNetworkReply::NetworkError error) { |
| 723 |
Q_UNUSED(error) |
| 724 |
//qDebug() << error; |
| 725 |
|
| 726 |
Q_D(KQOAuthManager); |
| 727 |
|
| 728 |
switch (error) { |
| 729 |
case QNetworkReply::NoError: |
| 730 |
d->error = KQOAuthManager::NoError; |
| 731 |
break; |
| 732 |
case QNetworkReply::ContentAccessDenied: |
| 733 |
case QNetworkReply::UnknownContentError: |
| 734 |
case QNetworkReply::AuthenticationRequiredError: |
| 735 |
d->error = KQOAuthManager::RequestUnauthorized; |
| 736 |
break; |
| 737 |
case QNetworkReply::ContentNotFoundError: |
| 738 |
d->error = KQOAuthManager::ContentNotFound; |
| 739 |
break; |
| 740 |
case QNetworkReply::ContentOperationNotPermittedError: |
| 741 |
d->error = KQOAuthManager::ContentOperationNotPermittedError; |
| 742 |
break; |
| 743 |
default: |
| 744 |
d->error = KQOAuthManager::NetworkError; |
| 745 |
break; |
| 746 |
} |
| 747 |
QByteArray emptyResponse; |
| 748 |
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender()); |
| 749 |
//qDebug() << "STATUS" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); |
| 750 |
d->r = d->requestMap.key(reply); |
| 751 |
d->currentRequestType = d->r->requestType(); |
| 752 |
if( d->requestIds.contains(reply) ) { |
| 753 |
int id = d->requestIds.value(reply); |
| 754 |
emit authorizedRequestReady(emptyResponse, id); |
| 755 |
reply->deleteLater(); |
| 756 |
} |
| 757 |
else if ( d->currentRequestType == KQOAuthRequest::AuthorizedRequest) { |
| 758 |
// does this signal always have to be emitted if there is an error |
| 759 |
// or can is it only valid for KQOAuthRequest::AuthorizedRequest? |
| 760 |
if (error != QNetworkReply::ContentAccessDenied && |
| 761 |
error != QNetworkReply::AuthenticationRequiredError) |
| 762 |
emit requestReady(emptyResponse); |
| 763 |
|
| 764 |
emit authorizedRequestDone(); |
| 765 |
} |
| 766 |
else { |
| 767 |
if (error != QNetworkReply::ContentAccessDenied && |
| 768 |
error!= QNetworkReply::AuthenticationRequiredError) |
| 769 |
emit requestReady(emptyResponse); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
void KQOAuthManager::requestTimeout() { |
| 775 |
Q_D(KQOAuthManager); |
| 776 |
KQOAuthRequest *request = qobject_cast<KQOAuthRequest *>(sender()); |
| 777 |
if( d->requestMap.contains(request)) { |
| 778 |
qWarning() << "KQOAuthManager::requestTimeout: Calling abort"; |
| 779 |
d->requestMap.value(request)->abort(); |
| 780 |
} |
| 781 |
else |
| 782 |
qWarning() << "KQOAuthManager::requestTimeout: The KQOAuthRequest was not found"; |
| 783 |
} |