| 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 <QByteArray> |
| 21 |
#include <QDateTime> |
| 22 |
#include <QCryptographicHash> |
| 23 |
#include <QPair> |
| 24 |
#include <QStringList> |
| 25 |
|
| 26 |
#include <QtDebug> |
| 27 |
#include <QtAlgorithms> |
| 28 |
|
| 29 |
#include "kqoauthrequest.h" |
| 30 |
#include "kqoauthrequest_p.h" |
| 31 |
#include "kqoauthutils.h" |
| 32 |
#include "kqoauthglobals.h" |
| 33 |
|
| 34 |
|
| 35 |
//////////// Private d_ptr implementation ///////// |
| 36 |
|
| 37 |
KQOAuthRequestPrivate::KQOAuthRequestPrivate() : |
| 38 |
timeout(0) |
| 39 |
{ |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
KQOAuthRequestPrivate::~KQOAuthRequestPrivate() |
| 44 |
{ |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
// This method will not include the "oauthSignature" paramater, since it is calculated from these parameters. |
| 49 |
void KQOAuthRequestPrivate::prepareRequest() { |
| 50 |
|
| 51 |
// If parameter list is not empty, we don't want to insert these values by |
| 52 |
// accident a second time. So giving up. |
| 53 |
if( !requestParameters.isEmpty() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
switch ( requestType ) { |
| 58 |
case KQOAuthRequest::TemporaryCredentials: |
| 59 |
requestParameters.append( qMakePair( OAUTH_KEY_CALLBACK, oauthCallbackUrl.toString()) ); // This is so ugly that it is almost beautiful. |
| 60 |
requestParameters.append( qMakePair( OAUTH_KEY_SIGNATURE_METHOD, oauthSignatureMethod) ); |
| 61 |
requestParameters.append( qMakePair( OAUTH_KEY_CONSUMER_KEY, oauthConsumerKey )); |
| 62 |
requestParameters.append( qMakePair( OAUTH_KEY_VERSION, oauthVersion )); |
| 63 |
requestParameters.append( qMakePair( OAUTH_KEY_TIMESTAMP, this->oauthTimestamp() )); |
| 64 |
requestParameters.append( qMakePair( OAUTH_KEY_NONCE, this->oauthNonce() )); |
| 65 |
break; |
| 66 |
|
| 67 |
case KQOAuthRequest::AccessToken: |
| 68 |
requestParameters.append( qMakePair( OAUTH_KEY_SIGNATURE_METHOD, oauthSignatureMethod )); |
| 69 |
requestParameters.append( qMakePair( OAUTH_KEY_CONSUMER_KEY, oauthConsumerKey )); |
| 70 |
requestParameters.append( qMakePair( OAUTH_KEY_VERSION, oauthVersion )); |
| 71 |
requestParameters.append( qMakePair( OAUTH_KEY_TIMESTAMP, this->oauthTimestamp() )); |
| 72 |
requestParameters.append( qMakePair( OAUTH_KEY_NONCE, this->oauthNonce() )); |
| 73 |
requestParameters.append( qMakePair( OAUTH_KEY_VERIFIER, oauthVerifier )); |
| 74 |
requestParameters.append( qMakePair( OAUTH_KEY_TOKEN, oauthToken )); |
| 75 |
break; |
| 76 |
|
| 77 |
case KQOAuthRequest::AuthorizedRequest: |
| 78 |
requestParameters.append( qMakePair( OAUTH_KEY_SIGNATURE_METHOD, oauthSignatureMethod )); |
| 79 |
requestParameters.append( qMakePair( OAUTH_KEY_CONSUMER_KEY, oauthConsumerKey )); |
| 80 |
requestParameters.append( qMakePair( OAUTH_KEY_VERSION, oauthVersion )); |
| 81 |
requestParameters.append( qMakePair( OAUTH_KEY_TIMESTAMP, this->oauthTimestamp() )); |
| 82 |
requestParameters.append( qMakePair( OAUTH_KEY_NONCE, this->oauthNonce() )); |
| 83 |
requestParameters.append( qMakePair( OAUTH_KEY_TOKEN, oauthToken )); |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void KQOAuthRequestPrivate::signRequest() { |
| 92 |
QString signature = this->oauthSignature(); |
| 93 |
requestParameters.append( qMakePair( OAUTH_KEY_SIGNATURE, signature) ); |
| 94 |
} |
| 95 |
|
| 96 |
QString KQOAuthRequestPrivate::oauthSignature() { |
| 97 |
/** |
| 98 |
* http://oauth.net/core/1.0/#anchor16 |
| 99 |
* The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104] where the |
| 100 |
* Signature Base String is the text and the key is the concatenated values (each first encoded per Parameter |
| 101 |
* Encoding) of the Consumer Secret and Token Secret, separated by an ‘&’ character (ASCII code 38) even if empty. |
| 102 |
* |
| 103 |
**/ |
| 104 |
QByteArray baseString = this->requestBaseString(); |
| 105 |
|
| 106 |
|
| 107 |
// Default: Use HMAC-SHA1 |
| 108 |
QString secret = QString(QUrl::toPercentEncoding(oauthConsumerSecretKey)) + "&" + QString(QUrl::toPercentEncoding(oauthTokenSecret)); |
| 109 |
QString signature = KQOAuthUtils::hmac_sha1(baseString, secret); |
| 110 |
|
| 111 |
|
| 112 |
if (debugOutput) { |
| 113 |
qDebug() << "========== KQOAuthRequest has the following signature:"; |
| 114 |
qDebug() << " * Signature : " << QUrl::toPercentEncoding(signature) << "\n"; |
| 115 |
} |
| 116 |
return QString( QUrl::toPercentEncoding(signature) ); |
| 117 |
} |
| 118 |
|
| 119 |
bool normalizedParameterSort(const QPair<QString, QString> &left, const QPair<QString, QString> &right) { |
| 120 |
QString keyLeft = left.first; |
| 121 |
QString valueLeft = left.second; |
| 122 |
QString keyRight = right.first; |
| 123 |
QString valueRight = right.second; |
| 124 |
|
| 125 |
if(keyLeft == keyRight) { |
| 126 |
return (valueLeft < valueRight); |
| 127 |
} else { |
| 128 |
return (keyLeft < keyRight); |
| 129 |
} |
| 130 |
} |
| 131 |
QByteArray KQOAuthRequestPrivate::requestBaseString() { |
| 132 |
QByteArray baseString; |
| 133 |
|
| 134 |
// Every request has these as the commont parameters. |
| 135 |
baseString.append( oauthHttpMethodString.toUtf8() + "&"); // HTTP method |
| 136 |
baseString.append( QUrl::toPercentEncoding( oauthRequestEndpoint.toString(QUrl::RemoveQuery) ) + "&" ); // The path and query components |
| 137 |
|
| 138 |
QList< QPair<QString, QString> > baseStringParameters; |
| 139 |
baseStringParameters.append(requestParameters); |
| 140 |
baseStringParameters.append(additionalParameters); |
| 141 |
|
| 142 |
// Sort the request parameters. These parameters have been |
| 143 |
// initialized earlier. |
| 144 |
qSort(baseStringParameters.begin(), |
| 145 |
baseStringParameters.end(), |
| 146 |
normalizedParameterSort |
| 147 |
); |
| 148 |
|
| 149 |
// Last append the request parameters correctly encoded. |
| 150 |
baseString.append( encodedParamaterList(baseStringParameters) ); |
| 151 |
|
| 152 |
if (debugOutput) { |
| 153 |
qDebug() << "========== KQOAuthRequest has the following base string:"; |
| 154 |
qDebug() << baseString << "\n"; |
| 155 |
} |
| 156 |
|
| 157 |
return baseString; |
| 158 |
} |
| 159 |
|
| 160 |
QByteArray KQOAuthRequestPrivate::encodedParamaterList(const QList< QPair<QString, QString> > ¶meters) { |
| 161 |
QByteArray resultList; |
| 162 |
|
| 163 |
bool first = true; |
| 164 |
QPair<QString, QString> parameter; |
| 165 |
|
| 166 |
// Do the debug output. |
| 167 |
if (debugOutput) { |
| 168 |
qDebug() << "========== KQOAuthRequest has the following parameters:"; |
| 169 |
} |
| 170 |
foreach (parameter, parameters) { |
| 171 |
if(!first) { |
| 172 |
resultList.append( "&" ); |
| 173 |
} else { |
| 174 |
first = false; |
| 175 |
} |
| 176 |
|
| 177 |
// Here we don't need to explicitely encode the strings to UTF-8 since |
| 178 |
// QUrl::toPercentEncoding() takes care of that for us. |
| 179 |
resultList.append( QUrl::toPercentEncoding(parameter.first) // Parameter key |
| 180 |
+ "=" |
| 181 |
+ QUrl::toPercentEncoding(parameter.second) // Parameter value |
| 182 |
); |
| 183 |
if (debugOutput) { |
| 184 |
qDebug() << " * " |
| 185 |
<< parameter.first |
| 186 |
<< " : " |
| 187 |
<< parameter.second; |
| 188 |
} |
| 189 |
} |
| 190 |
if (debugOutput) { |
| 191 |
qDebug() << "\n"; |
| 192 |
} |
| 193 |
|
| 194 |
return QUrl::toPercentEncoding(resultList); |
| 195 |
} |
| 196 |
|
| 197 |
QString KQOAuthRequestPrivate::oauthTimestamp() const { |
| 198 |
// This is basically for unit tests only. In most cases we don't set the nonce beforehand. |
| 199 |
if (!oauthTimestamp_.isEmpty()) { |
| 200 |
return oauthTimestamp_; |
| 201 |
} |
| 202 |
|
| 203 |
#if QT_VERSION >= 0x040700 |
| 204 |
return QString::number(QDateTime::currentDateTimeUtc().toTime_t()); |
| 205 |
#else |
| 206 |
return QString::number(QDateTime::currentDateTime().toUTC().toTime_t()); |
| 207 |
#endif |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
QString KQOAuthRequestPrivate::oauthNonce() const { |
| 212 |
// This is basically for unit tests only. In most cases we don't set the nonce beforehand. |
| 213 |
if (!oauthNonce_.isEmpty()) { |
| 214 |
return oauthNonce_; |
| 215 |
} |
| 216 |
|
| 217 |
return QString::number(qrand()); |
| 218 |
} |
| 219 |
|
| 220 |
bool KQOAuthRequestPrivate::validateRequest() const { |
| 221 |
switch ( requestType ) { |
| 222 |
case KQOAuthRequest::TemporaryCredentials: |
| 223 |
if (oauthRequestEndpoint.isEmpty() |
| 224 |
|| oauthConsumerKey.isEmpty() |
| 225 |
|| oauthNonce_.isEmpty() |
| 226 |
|| oauthSignatureMethod.isEmpty() |
| 227 |
|| oauthTimestamp_.isEmpty() |
| 228 |
|| oauthVersion.isEmpty()) |
| 229 |
{ |
| 230 |
return false; |
| 231 |
} |
| 232 |
return true; |
| 233 |
|
| 234 |
case KQOAuthRequest::AccessToken: |
| 235 |
if (oauthRequestEndpoint.isEmpty() |
| 236 |
|| oauthVerifier.isEmpty() |
| 237 |
|| oauthConsumerKey.isEmpty() |
| 238 |
|| oauthNonce_.isEmpty() |
| 239 |
|| oauthSignatureMethod.isEmpty() |
| 240 |
|| oauthTimestamp_.isEmpty() |
| 241 |
|| oauthToken.isEmpty() |
| 242 |
|| oauthTokenSecret.isEmpty() |
| 243 |
|| oauthVersion.isEmpty()) |
| 244 |
{ |
| 245 |
return false; |
| 246 |
} |
| 247 |
return true; |
| 248 |
|
| 249 |
case KQOAuthRequest::AuthorizedRequest: |
| 250 |
if (oauthRequestEndpoint.isEmpty() |
| 251 |
|| oauthConsumerKey.isEmpty() |
| 252 |
|| oauthNonce_.isEmpty() |
| 253 |
|| oauthSignatureMethod.isEmpty() |
| 254 |
|| oauthTimestamp_.isEmpty() |
| 255 |
|| oauthToken.isEmpty() |
| 256 |
|| oauthTokenSecret.isEmpty() |
| 257 |
|| oauthVersion.isEmpty()) |
| 258 |
{ |
| 259 |
return false; |
| 260 |
} |
| 261 |
return true; |
| 262 |
|
| 263 |
default: |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
// We should not come here. |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
//////////// Public implementation //////////////// |
| 272 |
|
| 273 |
KQOAuthRequest::KQOAuthRequest(QObject *parent) : |
| 274 |
QObject(parent), |
| 275 |
d_ptr(new KQOAuthRequestPrivate) |
| 276 |
{ |
| 277 |
Q_D(KQOAuthRequest); |
| 278 |
d_ptr->debugOutput = false; // No debug output by default. |
| 279 |
connect(&(d->timer), SIGNAL(timeout()), this, SIGNAL(requestTimedout())); |
| 280 |
} |
| 281 |
|
| 282 |
KQOAuthRequest::~KQOAuthRequest() |
| 283 |
{ |
| 284 |
delete d_ptr; |
| 285 |
} |
| 286 |
|
| 287 |
void KQOAuthRequest::initRequest(KQOAuthRequest::RequestType type, const QUrl &requestEndpoint) { |
| 288 |
Q_D(KQOAuthRequest); |
| 289 |
|
| 290 |
if (!requestEndpoint.isValid()) { |
| 291 |
qWarning() << "Endpoint URL is not valid. Ignoring. This request might not work."; |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
if (type < 0 || type > KQOAuthRequest::AuthorizedRequest) { |
| 296 |
qWarning() << "Invalid request type. Ignoring. This request might not work."; |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
// Clear the request |
| 301 |
clearRequest(); |
| 302 |
|
| 303 |
// Set smart defaults. |
| 304 |
d->requestType = type; |
| 305 |
d->oauthRequestEndpoint = requestEndpoint; |
| 306 |
d->oauthTimestamp_ = d->oauthTimestamp(); |
| 307 |
d->oauthNonce_ = d->oauthNonce(); |
| 308 |
this->setSignatureMethod(KQOAuthRequest::HMAC_SHA1); |
| 309 |
this->setHttpMethod(KQOAuthRequest::POST); |
| 310 |
d->oauthVersion = "1.0"; // Currently supports only version 1.0 |
| 311 |
|
| 312 |
d->contentType = "application/x-www-form-urlencoded"; |
| 313 |
} |
| 314 |
|
| 315 |
void KQOAuthRequest::setConsumerKey(const QString &consumerKey) { |
| 316 |
Q_D(KQOAuthRequest); |
| 317 |
d->oauthConsumerKey = consumerKey; |
| 318 |
} |
| 319 |
|
| 320 |
void KQOAuthRequest::setConsumerSecretKey(const QString &consumerSecretKey) { |
| 321 |
Q_D(KQOAuthRequest); |
| 322 |
d->oauthConsumerSecretKey = consumerSecretKey; |
| 323 |
} |
| 324 |
|
| 325 |
void KQOAuthRequest::setCallbackUrl(const QUrl &callbackUrl) { |
| 326 |
Q_D(KQOAuthRequest); |
| 327 |
|
| 328 |
d->oauthCallbackUrl = callbackUrl; |
| 329 |
} |
| 330 |
|
| 331 |
void KQOAuthRequest::setSignatureMethod(KQOAuthRequest::RequestSignatureMethod requestMethod) { |
| 332 |
Q_D(KQOAuthRequest); |
| 333 |
QString requestMethodString; |
| 334 |
|
| 335 |
switch (requestMethod) { |
| 336 |
case KQOAuthRequest::PLAINTEXT: |
| 337 |
requestMethodString = "PLAINTEXT"; |
| 338 |
break; |
| 339 |
case KQOAuthRequest::HMAC_SHA1: |
| 340 |
requestMethodString = "HMAC-SHA1"; |
| 341 |
break; |
| 342 |
default: |
| 343 |
// We should not come here |
| 344 |
qWarning() << "Invalid signature method set."; |
| 345 |
break; |
| 346 |
} |
| 347 |
|
| 348 |
d->oauthSignatureMethod = requestMethodString; |
| 349 |
d->requestSignatureMethod = requestMethod; |
| 350 |
} |
| 351 |
|
| 352 |
void KQOAuthRequest::setTokenSecret(const QString &tokenSecret) { |
| 353 |
Q_D(KQOAuthRequest); |
| 354 |
|
| 355 |
d->oauthTokenSecret = tokenSecret; |
| 356 |
} |
| 357 |
|
| 358 |
void KQOAuthRequest::setToken(const QString &token) { |
| 359 |
Q_D(KQOAuthRequest); |
| 360 |
|
| 361 |
d->oauthToken = token; |
| 362 |
} |
| 363 |
|
| 364 |
void KQOAuthRequest::setVerifier(const QString &verifier) { |
| 365 |
Q_D(KQOAuthRequest); |
| 366 |
|
| 367 |
d->oauthVerifier = verifier; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
void KQOAuthRequest::setHttpMethod(KQOAuthRequest::RequestHttpMethod httpMethod) { |
| 372 |
Q_D(KQOAuthRequest); |
| 373 |
|
| 374 |
QString requestHttpMethodString; |
| 375 |
|
| 376 |
switch (httpMethod) { |
| 377 |
case KQOAuthRequest::GET: |
| 378 |
requestHttpMethodString = "GET"; |
| 379 |
break; |
| 380 |
case KQOAuthRequest::POST: |
| 381 |
requestHttpMethodString = "POST"; |
| 382 |
break; |
| 383 |
case KQOAuthRequest::HEAD: |
| 384 |
requestHttpMethodString = "HEAD"; |
| 385 |
break; |
| 386 |
case KQOAuthRequest::DELETE: |
| 387 |
requestHttpMethodString = "DELETE"; |
| 388 |
break; |
| 389 |
default: |
| 390 |
qWarning() << "Invalid HTTP method set."; |
| 391 |
break; |
| 392 |
} |
| 393 |
|
| 394 |
d->oauthHttpMethod = httpMethod; |
| 395 |
d->oauthHttpMethodString = requestHttpMethodString; |
| 396 |
} |
| 397 |
|
| 398 |
KQOAuthRequest::RequestHttpMethod KQOAuthRequest::httpMethod() const { |
| 399 |
Q_D(const KQOAuthRequest); |
| 400 |
|
| 401 |
return d->oauthHttpMethod; |
| 402 |
} |
| 403 |
|
| 404 |
void KQOAuthRequest::setAdditionalParameters(const KQOAuthParameters &additionalParams) { |
| 405 |
Q_D(KQOAuthRequest); |
| 406 |
|
| 407 |
QList<QString> additionalKeys = additionalParams.keys(); |
| 408 |
QList<QString> additionalValues = additionalParams.values(); |
| 409 |
|
| 410 |
int i=0; |
| 411 |
foreach(QString key, additionalKeys) { |
| 412 |
QString value = additionalValues.at(i); |
| 413 |
d->additionalParameters.append( qMakePair(key, value) ); |
| 414 |
i++; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
KQOAuthParameters KQOAuthRequest::additionalParameters() const { |
| 419 |
Q_D(const KQOAuthRequest); |
| 420 |
|
| 421 |
QMultiMap<QString, QString> additionalParams; |
| 422 |
for(int i=0; i<d->additionalParameters.size(); i++) { |
| 423 |
additionalParams.insert(d->additionalParameters.at(i).first, |
| 424 |
d->additionalParameters.at(i).second); |
| 425 |
} |
| 426 |
|
| 427 |
return additionalParams; |
| 428 |
} |
| 429 |
|
| 430 |
KQOAuthRequest::RequestType KQOAuthRequest::requestType() const { |
| 431 |
Q_D(const KQOAuthRequest); |
| 432 |
return d->requestType; |
| 433 |
} |
| 434 |
|
| 435 |
QUrl KQOAuthRequest::requestEndpoint() const { |
| 436 |
Q_D(const KQOAuthRequest); |
| 437 |
return d->oauthRequestEndpoint; |
| 438 |
} |
| 439 |
|
| 440 |
QList<QByteArray> KQOAuthRequest::requestParameters() { |
| 441 |
Q_D(KQOAuthRequest); |
| 442 |
|
| 443 |
QList<QByteArray> requestParamList; |
| 444 |
|
| 445 |
d->prepareRequest(); |
| 446 |
if (!isValid() ) { |
| 447 |
qWarning() << "Request is not valid! I will still sign it, but it will probably not work."; |
| 448 |
} |
| 449 |
|
| 450 |
d->signRequest(); |
| 451 |
|
| 452 |
QPair<QString, QString> requestParam; |
| 453 |
QString param; |
| 454 |
QString value; |
| 455 |
foreach (requestParam, d->requestParameters) { |
| 456 |
param = requestParam.first; |
| 457 |
value = requestParam.second; |
| 458 |
if (param != OAUTH_KEY_SIGNATURE) { |
| 459 |
value = QUrl::toPercentEncoding(value); |
| 460 |
} |
| 461 |
|
| 462 |
requestParamList.append(QString(param + "=\"" + value +"\"").toUtf8()); |
| 463 |
} |
| 464 |
|
| 465 |
return requestParamList; |
| 466 |
} |
| 467 |
|
| 468 |
QString KQOAuthRequest::contentType() |
| 469 |
{ |
| 470 |
Q_D(const KQOAuthRequest); |
| 471 |
return d->contentType; |
| 472 |
} |
| 473 |
|
| 474 |
void KQOAuthRequest::setContentType(const QString &contentType) |
| 475 |
{ |
| 476 |
Q_D(KQOAuthRequest); |
| 477 |
d->contentType = contentType; |
| 478 |
} |
| 479 |
|
| 480 |
QByteArray KQOAuthRequest::rawData() |
| 481 |
{ |
| 482 |
Q_D(const KQOAuthRequest); |
| 483 |
return d->postRawData; |
| 484 |
} |
| 485 |
|
| 486 |
void KQOAuthRequest::setRawData(const QByteArray &rawData) |
| 487 |
{ |
| 488 |
Q_D(KQOAuthRequest); |
| 489 |
d->postRawData = rawData; |
| 490 |
} |
| 491 |
|
| 492 |
QHttpMultiPart* KQOAuthRequest::httpMultiPart() |
| 493 |
{ |
| 494 |
Q_D(const KQOAuthRequest); |
| 495 |
return d->httpMultiPart; |
| 496 |
} |
| 497 |
|
| 498 |
void KQOAuthRequest::setHttpMultiPart(QHttpMultiPart* httpMultiPart) |
| 499 |
{ |
| 500 |
Q_D(KQOAuthRequest); |
| 501 |
d->httpMultiPart = httpMultiPart; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
QByteArray KQOAuthRequest::requestBody() const { |
| 506 |
Q_D(const KQOAuthRequest); |
| 507 |
|
| 508 |
QByteArray postBodyContent; |
| 509 |
bool first = true; |
| 510 |
for(int i=0; i < d->additionalParameters.size(); i++) { |
| 511 |
if(!first) { |
| 512 |
postBodyContent.append("&"); |
| 513 |
} else { |
| 514 |
first = false; |
| 515 |
} |
| 516 |
|
| 517 |
QString key = d->additionalParameters.at(i).first; |
| 518 |
QString value = d->additionalParameters.at(i).second; |
| 519 |
|
| 520 |
postBodyContent.append(QUrl::toPercentEncoding(key) + QString("=").toUtf8() + |
| 521 |
QUrl::toPercentEncoding(value)); |
| 522 |
} |
| 523 |
return postBodyContent; |
| 524 |
} |
| 525 |
|
| 526 |
bool KQOAuthRequest::isValid() const { |
| 527 |
Q_D(const KQOAuthRequest); |
| 528 |
|
| 529 |
return d->validateRequest(); |
| 530 |
} |
| 531 |
|
| 532 |
void KQOAuthRequest::setTimeout(int timeoutMilliseconds) { |
| 533 |
Q_D(KQOAuthRequest); |
| 534 |
d->timeout = timeoutMilliseconds; |
| 535 |
} |
| 536 |
|
| 537 |
void KQOAuthRequest::clearRequest() { |
| 538 |
Q_D(KQOAuthRequest); |
| 539 |
|
| 540 |
d->oauthRequestEndpoint = ""; |
| 541 |
d->oauthHttpMethodString = ""; |
| 542 |
d->oauthConsumerKey = ""; |
| 543 |
d->oauthConsumerSecretKey = ""; |
| 544 |
d->oauthToken = ""; |
| 545 |
d->oauthTokenSecret = ""; |
| 546 |
d->oauthSignatureMethod = ""; |
| 547 |
d->oauthCallbackUrl = ""; |
| 548 |
d->oauthVerifier = ""; |
| 549 |
d->oauthTimestamp_ = ""; |
| 550 |
d->oauthNonce_ = ""; |
| 551 |
d->requestParameters.clear(); |
| 552 |
d->additionalParameters.clear(); |
| 553 |
d->timeout = 0; |
| 554 |
} |
| 555 |
|
| 556 |
void KQOAuthRequest::setEnableDebugOutput(bool enabled) { |
| 557 |
Q_D(KQOAuthRequest); |
| 558 |
d->debugOutput = enabled; |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Protected implementations for inherited classes |
| 563 |
*/ |
| 564 |
bool KQOAuthRequest::validateXAuthRequest() const { |
| 565 |
Q_D(const KQOAuthRequest); |
| 566 |
|
| 567 |
if (d->oauthRequestEndpoint.isEmpty() |
| 568 |
|| d->oauthConsumerKey.isEmpty() |
| 569 |
|| d->oauthNonce_.isEmpty() |
| 570 |
|| d->oauthSignatureMethod.isEmpty() |
| 571 |
|| d->oauthTimestamp_.isEmpty() |
| 572 |
|| d->oauthVersion.isEmpty()) |
| 573 |
{ |
| 574 |
return false; |
| 575 |
} |
| 576 |
return true; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
/** |
| 581 |
* Private implementations for friend classes |
| 582 |
*/ |
| 583 |
QString KQOAuthRequest::consumerKeyForManager() const { |
| 584 |
Q_D(const KQOAuthRequest); |
| 585 |
return d->oauthConsumerKey; |
| 586 |
} |
| 587 |
|
| 588 |
QString KQOAuthRequest::consumerKeySecretForManager() const { |
| 589 |
Q_D(const KQOAuthRequest); |
| 590 |
return d->oauthConsumerSecretKey; |
| 591 |
} |
| 592 |
|
| 593 |
KQOAuthRequest::RequestSignatureMethod KQOAuthRequest::requestSignatureMethodForManager() const { |
| 594 |
Q_D(const KQOAuthRequest); |
| 595 |
return d->requestSignatureMethod; |
| 596 |
} |
| 597 |
|
| 598 |
QUrl KQOAuthRequest::callbackUrlForManager() const { |
| 599 |
Q_D(const KQOAuthRequest); |
| 600 |
return d->oauthCallbackUrl; |
| 601 |
} |
| 602 |
|
| 603 |
void KQOAuthRequest::requestTimerStart() |
| 604 |
{ |
| 605 |
Q_D(KQOAuthRequest); |
| 606 |
if (d->timeout > 0) { |
| 607 |
d->timer.start(d->timeout); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
void KQOAuthRequest::requestTimerStop() |
| 612 |
{ |
| 613 |
Q_D(KQOAuthRequest); |
| 614 |
if( d->timer.isActive() ) |
| 615 |
d->timer.stop(); |
| 616 |
} |