File indexing completed on 2025-01-19 03:55:40

0001 #include <QDebug>
0002 #include <QDateTime>
0003 #include <QMap>
0004 #include <QString>
0005 #include <QStringList>
0006 #if QT_VERSION >= 0x050000
0007 #include <QUrlQuery>
0008 #endif
0009 
0010 #include "o2skydrive.h"
0011 #include "o0globals.h"
0012 
0013 O2Skydrive::O2Skydrive(QObject *parent): O2(parent) {
0014     setRequestUrl("https://login.live.com/oauth20_authorize.srf");
0015     setTokenUrl("https://login.live.com/oauth20_token.srf");
0016     setRefreshTokenUrl("https://login.live.com/oauth20_token.srf");
0017 }
0018 
0019 void O2Skydrive::link() {
0020     qDebug() << "O2Skydrive::link";
0021     if (linked()) {
0022         qDebug() << "O2kydrive::link: Linked already";
0023         return;
0024     }
0025 
0026     setLinked(false);
0027     setToken("");
0028     setTokenSecret("");
0029     setExtraTokens(QVariantMap());
0030     setRefreshToken(QString());
0031     setExpires(0);
0032 
0033     redirectUri_ = QString("https://login.live.com/oauth20_desktop.srf");
0034 
0035     // Assemble intial authentication URL
0036     QList<QPair<QString, QString> > parameters;
0037     parameters.append(qMakePair(QString(O2_OAUTH2_RESPONSE_TYPE), (grantFlow_ == GrantFlowAuthorizationCode) ? QString(O2_OAUTH2_GRANT_TYPE_CODE) : QString(O2_OAUTH2_GRANT_TYPE_TOKEN)));
0038     parameters.append(qMakePair(QString(O2_OAUTH2_CLIENT_ID), clientId_));
0039     parameters.append(qMakePair(QString(O2_OAUTH2_REDIRECT_URI), redirectUri_));
0040     parameters.append(qMakePair(QString(O2_OAUTH2_SCOPE), scope_));
0041 
0042     // Show authentication URL with a web browser
0043     QUrl url(requestUrl_);
0044 #if QT_VERSION < 0x050000
0045     url.setQueryItems(parameters);
0046 #else
0047     QUrlQuery query(url);
0048     query.setQueryItems(parameters);
0049     url.setQuery(query);
0050 #endif
0051     Q_EMIT openBrowser(url);
0052 }
0053 
0054 void O2Skydrive::redirected(const QUrl &url) {
0055     qDebug() << "O2Skydrive::redirected" << url;
0056 
0057     Q_EMIT closeBrowser();
0058 
0059     if (grantFlow_ == GrantFlowAuthorizationCode) {
0060         // Get access code
0061         QString urlCode;
0062 #if QT_VERSION < 0x050000
0063         urlCode = url.queryItemValue(O2_OAUTH2_GRANT_TYPE_CODE);
0064 #else
0065         QUrlQuery query(url);
0066         urlCode = query.queryItemValue(O2_OAUTH2_GRANT_TYPE_CODE);
0067 #endif
0068         if (urlCode.isEmpty()) {
0069             qDebug() << "O2Skydrive::redirected: Code not received";
0070             Q_EMIT linkingFailed();
0071             return;
0072         }
0073         setCode(urlCode);
0074 
0075         // Exchange access code for access/refresh tokens
0076         QNetworkRequest tokenRequest(tokenUrl_);
0077         tokenRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
0078         QMap<QString, QString> parameters;
0079         parameters.insert(O2_OAUTH2_GRANT_TYPE_CODE, code());
0080         parameters.insert(O2_OAUTH2_CLIENT_ID, clientId_);
0081         parameters.insert(O2_OAUTH2_CLIENT_SECRET, clientSecret_);
0082         parameters.insert(O2_OAUTH2_REDIRECT_URI, redirectUri_);
0083         parameters.insert(O2_OAUTH2_GRANT_TYPE, O2_AUTHORIZATION_CODE);
0084         QByteArray data = buildRequestBody(parameters);
0085         QNetworkReply *tokenReply = manager_->post(tokenRequest, data);
0086         timedReplies_.add(tokenReply);
0087         connect(tokenReply, SIGNAL(finished()), this, SLOT(onTokenReplyFinished()), Qt::QueuedConnection);
0088 #if QT_VERSION < 0x051500
0089         connect(tokenReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onTokenReplyError(QNetworkReply::NetworkError)), Qt::QueuedConnection);
0090 #else
0091         connect(tokenReply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(onTokenReplyError(QNetworkReply::NetworkError)), Qt::QueuedConnection);
0092 #endif
0093     } else {
0094         // Get access token
0095         QString urlToken = "";
0096         QString urlRefreshToken = "";
0097         int urlExpiresIn = 0;
0098 
0099         QStringList parts = url.toString().split("#");
0100         if (parts.length() > 1) {
0101             foreach (QString item, parts[1].split("&")) {
0102                 int index = item.indexOf("=");
0103                 if (index == -1) {
0104                     continue;
0105                 }
0106                 QString key = item.left(index);
0107                 QString value = item.mid(index + 1);
0108                 qDebug() << "O2Skydrive::redirected: Got" << key;
0109                 if (key == O2_OAUTH2_ACCESS_TOKEN) {
0110                     urlToken = value;
0111                 } else if (key == O2_OAUTH2_EXPIRES_IN) {
0112                     urlExpiresIn = value.toInt();
0113                 } else if (key == O2_OAUTH2_REFRESH_TOKEN) {
0114                     urlRefreshToken = value;
0115                 }
0116             }
0117         }
0118 
0119         setToken(urlToken);
0120         setRefreshToken(urlRefreshToken);
0121         setExpires(QDateTime::currentMSecsSinceEpoch() / 1000 + urlExpiresIn);
0122         if (urlToken.isEmpty()) {
0123             Q_EMIT linkingFailed();
0124         } else {
0125             setLinked(true);
0126             Q_EMIT linkingSucceeded();
0127         }
0128     }
0129 }
0130 
0131 #include "moc_o2skydrive.cpp"