void qevercloud::EvernoteOAuthWebView::permanentFinished(QObject *rf) { ReplyFetcher* replyFetcher = qobject_cast<ReplyFetcher*>(rf); if(replyFetcher->isError()) { setError(replyFetcher->errorText()); } else { isSucceeded_ = true; QByteArray reply = replyFetcher->receivedData(); QMap<QString, QString> params; QList<QByteArray> vals = reply.split('&'); for(int i = 0; i < vals.length(); i++) { QString decoded = QUrl::fromPercentEncoding(vals[i]); int pos = decoded.indexOf('='); params[decoded.left(pos).trimmed()] = decoded.mid(pos + 1); } oauthResult_.noteStoreUrl = params[QStringLiteral("edam_noteStoreUrl")]; oauthResult_.expires = Timestamp(params[QStringLiteral("edam_expires")].toLongLong()); oauthResult_.shardId = params[QStringLiteral("edam_shard")]; oauthResult_.userId = params[QStringLiteral("edam_userId")].toInt(); oauthResult_.webApiUrlPrefix = params[QStringLiteral("edam_webApiUrlPrefix")]; oauthResult_.authenticationToken = params[QStringLiteral("oauth_token")]; emit authenticationFinished(true); emit authenticationSuceeded(); } replyFetcher->deleteLater(); }
void qevercloud::EvernoteOAuthWebView::onUrlChanged(const QUrl &url) { // step 3: catch the rediret to our callback url (nnoauth) QString s = url.toString(); if(s.contains("nnoauth?") && s.contains("?oauth_token=")) { if(s.contains("&oauth_verifier=")) { // success QString token = s.mid(s.indexOf("?oauth_token=") + QString("?oauth_token=").length()); // step 4: acquire permanent token ReplyFetcher* replyFetcher = new ReplyFetcher(); connect(replyFetcher, SIGNAL(replyFetched(QObject*)), this, SLOT(permanentFinished(QObject*))); QUrl url(oauthUrlBase_ + QString("&oauth_token=%1").arg(token)); replyFetcher->start(page()->networkAccessManager(), url); } else {
void qevercloud::EvernoteOAuthWebView::temporaryFinished(QObject *rf) { ReplyFetcher* replyFetcher = qobject_cast<ReplyFetcher*>(rf); if(replyFetcher->isError()) { setError(replyFetcher->errorText()); } else { QString reply = QString(replyFetcher->receivedData()); int index = reply.indexOf(QStringLiteral("&oauth_token_secret")); QString token = reply.left(index); // step 2: directing a user to the login page connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(onUrlChanged(QUrl))); QUrl loginUrl(QStringLiteral("https://%1//OAuth.action?%2").arg(host_).arg(token)); this->setUrl(loginUrl); } replyFetcher->deleteLater(); }
void qevercloud::EvernoteOAuthWebView::authenticate(QString host, QString consumerKey, QString consumerSecret) { host_ = host; isSucceeded_ = false; this->setHtml(""); this->history()->clear(); qint64 timestamp = QDateTime::currentMSecsSinceEpoch()/1000; qint64 nonce = nonceGenerator()(); oauthUrlBase_ = QStringLiteral("https://%1/oauth?oauth_consumer_key=%2&oauth_signature=%3&oauth_signature_method=PLAINTEXT&oauth_timestamp=%4&oauth_nonce=%5") .arg(host).arg(consumerKey).arg(consumerSecret).arg(timestamp).arg(nonce); // step 1: acquire temporary token ReplyFetcher* replyFetcher = new ReplyFetcher(); connect(replyFetcher, &ReplyFetcher::replyFetched, this, &EvernoteOAuthWebView::temporaryFinished); QUrl url(oauthUrlBase_ + QStringLiteral("&oauth_callback=nnoauth")); replyFetcher->start(page()->networkAccessManager(), url); }
void qevercloud::EvernoteOAuthWebView::onUrlChanged(const QUrl &url) { // step 3: catch the rediret to our callback url (nnoauth) QString s = url.toString(); QString oauthMarker = QStringLiteral("?oauth_token="); if(s.contains(QStringLiteral("nnoauth?")) && s.contains(oauthMarker)) { if(s.contains(QStringLiteral("&oauth_verifier="))) { // success QString token = s.mid(s.indexOf(oauthMarker) + oauthMarker.length()); // step 4: acquire permanent token ReplyFetcher* replyFetcher = new ReplyFetcher(); connect(replyFetcher, &ReplyFetcher::replyFetched, this, &EvernoteOAuthWebView::permanentFinished); QUrl url(oauthUrlBase_ + QStringLiteral("&oauth_token=%1").arg(token)); replyFetcher->start(page()->networkAccessManager(), url); } else { setError(QStringLiteral("Authentification failed.")); } disconnect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(onUrlChanged(QUrl))); QMetaObject::invokeMethod(this, "clearHtml", Qt::QueuedConnection); } }
void qevercloud::AsyncResult::onReplyFetched(QObject *rp) { ReplyFetcher* reply = qobject_cast<ReplyFetcher*>(rp); QSharedPointer<EverCloudExceptionData> error; QVariant result; try { if(reply->isError()) { error = QSharedPointer<EverCloudExceptionData>(new EverCloudExceptionData(reply->errorText())); } else if(reply->httpStatusCode() != 200) { error = QSharedPointer<EverCloudExceptionData>(new EverCloudExceptionData(QStringLiteral("HTTP Status Code = %1").arg(reply->httpStatusCode()))); } else { result = readFunction_(reply->receivedData()); } } catch(const EverCloudException& e) { error = e.exceptionData(); } catch(const std::exception& e) { error = QSharedPointer<EverCloudExceptionData>(new EverCloudExceptionData(QStringLiteral("Exception of type \"%1\" with the message: %2").arg(typeid(e).name()).arg(e.what()))); } catch(...) { error = QSharedPointer<EverCloudExceptionData>(new EverCloudExceptionData(QStringLiteral("Unknown exception"))); } emit finished(result, error); reply->deleteLater(); if(autoDelete_) this->deleteLater(); }
void qevercloud::AsyncResult::start() { ReplyFetcher* f = new ReplyFetcher; QObject::connect(f, &ReplyFetcher::replyFetched, this, &AsyncResult::onReplyFetched); f->start(evernoteNetworkAccessManager(), request_, postData_); }