File indexing completed on 2024-12-22 04:57:53

0001 /*
0002     SPDX-License-Identifier: BSD-2-Clause
0003 */
0004 
0005 #include <QDateTime>
0006 #include <QNetworkAccessManager>
0007 #include <QNetworkReply>
0008 
0009 #include "o2/o0globals.h"
0010 #include "o2/o1requestor.h"
0011 #include "o2/o1timedreply.h"
0012 
0013 O1Requestor::O1Requestor(QNetworkAccessManager *manager, O1 *authenticator, QObject *parent)
0014     : QObject(parent)
0015 {
0016     manager_ = manager;
0017     authenticator_ = authenticator;
0018 }
0019 
0020 QNetworkReply *O1Requestor::get(const QNetworkRequest &req, const QList<O0RequestParameter> &signingParameters)
0021 {
0022     QNetworkRequest request = setup(req, signingParameters, QNetworkAccessManager::GetOperation);
0023     return addTimer(manager_->get(request));
0024 }
0025 
0026 QNetworkReply *O1Requestor::post(const QNetworkRequest &req, const QList<O0RequestParameter> &signingParameters, const QByteArray &data)
0027 {
0028     QNetworkRequest request = setup(req, signingParameters, QNetworkAccessManager::PostOperation);
0029     return addTimer(manager_->post(request, data));
0030 }
0031 
0032 QNetworkReply *O1Requestor::post(const QNetworkRequest &req, const QList<O0RequestParameter> &signingParameters, QHttpMultiPart *multiPart)
0033 {
0034     QNetworkRequest request = setup(req, signingParameters, QNetworkAccessManager::PostOperation);
0035     return addTimer(manager_->post(request, multiPart));
0036 }
0037 
0038 QNetworkReply *O1Requestor::put(const QNetworkRequest &req, const QList<O0RequestParameter> &signingParameters, const QByteArray &data)
0039 {
0040     QNetworkRequest request = setup(req, signingParameters, QNetworkAccessManager::PutOperation);
0041     return addTimer(manager_->put(request, data));
0042 }
0043 
0044 QNetworkReply *O1Requestor::addTimer(QNetworkReply *reply)
0045 {
0046     (void)new O1TimedReply(reply);
0047     return reply;
0048 }
0049 
0050 QNetworkRequest O1Requestor::setup(const QNetworkRequest &req, const QList<O0RequestParameter> &signingParameters, QNetworkAccessManager::Operation operation)
0051 {
0052     // Collect OAuth parameters
0053     QList<O0RequestParameter> oauthParams;
0054     oauthParams.append(O0RequestParameter(O2_OAUTH_CONSUMER_KEY, authenticator_->clientId().toLatin1()));
0055     oauthParams.append(O0RequestParameter(O2_OAUTH_VERSION, "1.0"));
0056     oauthParams.append(O0RequestParameter(O2_OAUTH_TOKEN, authenticator_->token().toLatin1()));
0057     oauthParams.append(O0RequestParameter(O2_OAUTH_SIGNATURE_METHOD, authenticator_->signatureMethod().toLatin1()));
0058     oauthParams.append(O0RequestParameter(O2_OAUTH_NONCE, O1::nonce()));
0059     oauthParams.append(O0RequestParameter(O2_OAUTH_TIMESTAMP, QString::number(QDateTime::currentDateTimeUtc().toSecsSinceEpoch()).toLatin1()));
0060 
0061     // Add signature parameter
0062     oauthParams.append(O0RequestParameter(O2_OAUTH_SIGNATURE, authenticator_->generateSignature(oauthParams, req, signingParameters, operation)));
0063 
0064     // Return a copy of the original request with authorization header set
0065     QNetworkRequest request(req);
0066     request.setRawHeader(O2_HTTP_AUTHORIZATION_HEADER, O1::buildAuthorizationHeader(oauthParams));
0067     return request;
0068 }
0069 
0070 #include "moc_o1requestor.cpp"