Commit 8e36a693 authored by Jesus Fernandez's avatar Jesus Fernandez

QAuth1Signature does not inherits from QVariantMap. Now using composition.

parent b8461709
......@@ -45,7 +45,7 @@
class QOAuth1SignaturePrivate
{
public:
QByteArray signatureBaseString(const QOAuth1Signature &instance);
QByteArray signatureBaseString() const;
QByteArray secret() const;
static QByteArray parameterString(const QVariantMap &parameters);
static QByteArray encodeHeaders(const QVariantMap &headers);
......@@ -55,6 +55,7 @@ public:
QUrl url;
QString clientSharedKey;
QString tokenSecret;
QVariantMap parameters;
};
#endif // QOAUTH1SIGNATURE_P_H
......@@ -62,16 +62,7 @@ static_assert(static_cast<int>(QOAuth1Signature::HttpRequestMethod::Head) ==
static_cast<int>(QNetworkAccessManager::DeleteOperation),
"Invalid QOAuth1Signature::HttpRequestMethod enumeration values");
inline static QOAuth1SignaturePrivate *d_ptr(
const QScopedPointer<QOAuth1SignaturePrivate> *instance)
{
auto casted = const_cast<QScopedPointer<QOAuth1SignaturePrivate> *>(instance);
if (casted->isNull())
casted->reset(new QOAuth1SignaturePrivate);
return casted->data();
}
QByteArray QOAuth1SignaturePrivate::signatureBaseString(const QOAuth1Signature &instance)
QByteArray QOAuth1SignaturePrivate::signatureBaseString() const
{
// https://tools.ietf.org/html/rfc5849#section-3.4.1
QByteArray base;
......@@ -98,13 +89,13 @@ QByteArray QOAuth1SignaturePrivate::signatureBaseString(const QOAuth1Signature &
base.append('&');
base.append(QUrl::toPercentEncoding(url.toString(QUrl::RemoveQuery)) + "&");
QVariantMap parameters = instance;
QVariantMap p = parameters;
{
const auto queryItems = QUrlQuery(url.query()).queryItems();
for (auto it = queryItems.begin(), end = queryItems.end(); it != end; ++it)
parameters.insert(it->first, it->second);
p.insert(it->first, it->second);
}
base.append(encodeHeaders(parameters));
base.append(encodeHeaders(p));
return base;
}
......@@ -140,20 +131,21 @@ QByteArray QOAuth1SignaturePrivate::encodeHeaders(const QVariantMap &headers)
}
QOAuth1Signature::QOAuth1Signature(const QUrl &url, QOAuth1Signature::HttpRequestMethod method,
const QVariantMap &parameters) : QVariantMap(parameters)
const QVariantMap &parameters) :
d_ptr(new QOAuth1SignaturePrivate)
{
d_ptr(&d)->url = url;
d_ptr(&d)->method = method;
d_ptr->url = url;
d_ptr->method = method;
d_ptr->parameters = parameters;
}
QOAuth1Signature::QOAuth1Signature(const QUrl &url, const QString &clientSharedKey,
const QString &tokenSecret, HttpRequestMethod method,
const QVariantMap &parameters) : QVariantMap(parameters)
const QVariantMap &parameters) :
QOAuth1Signature(url, method, parameters)
{
d_ptr(&d)->url = url;
d_ptr(&d)->method = method;
d_ptr(&d)->clientSharedKey = clientSharedKey;
d_ptr(&d)->tokenSecret = tokenSecret;
d_ptr->clientSharedKey = clientSharedKey;
d_ptr->tokenSecret = tokenSecret;
}
QOAuth1Signature::~QOAuth1Signature()
......@@ -161,67 +153,84 @@ QOAuth1Signature::~QOAuth1Signature()
QOAuth1Signature::HttpRequestMethod QOAuth1Signature::httpRequestMethod() const
{
return d_ptr(&d)->method;
Q_D(const QOAuth1Signature);
return d->method;
}
void QOAuth1Signature::setHttpRequestMethod(QOAuth1Signature::HttpRequestMethod method)
{
d_ptr(&d)->method = method;
Q_D(QOAuth1Signature);
d->method = method;
}
QUrl QOAuth1Signature::url() const
{
return d_ptr(&d)->url;
Q_D(const QOAuth1Signature);
return d->url;
}
void QOAuth1Signature::setUrl(const QUrl &url)
{
d_ptr(&d)->url = url;
Q_D(QOAuth1Signature);
d->url = url;
}
QVariantMap QOAuth1Signature::parameters() const
{
return *this;
Q_D(const QOAuth1Signature);
return d->parameters;
}
void QOAuth1Signature::setParameters(const QVariantMap &parameters)
{
static_cast<QVariantMap&>(*this) = parameters;
Q_D(QOAuth1Signature);
d->parameters = parameters;
}
void QOAuth1Signature::addRequestBody(const QUrlQuery &body)
{
Q_D(QOAuth1Signature);
const auto list = body.queryItems();
for (auto it = list.begin(), end = list.end(); it != end; ++it)
insert(it->first, it->second);
d->parameters.insert(it->first, it->second);
}
void QOAuth1Signature::insert(const QString &key, const QVariant &value)
{
Q_D(QOAuth1Signature);
d->parameters.insert(key, value);
}
QString QOAuth1Signature::clientSharedKey() const
{
return d_ptr(&d)->clientSharedKey;
Q_D(const QOAuth1Signature);
return d->clientSharedKey;
}
void QOAuth1Signature::setClientSharedKey(const QString &secret)
{
d_ptr(&d)->clientSharedKey = secret;
Q_D(QOAuth1Signature);
d->clientSharedKey = secret;
}
QString QOAuth1Signature::tokenSecret() const
{
return d_ptr(&d)->tokenSecret;
Q_D(const QOAuth1Signature);
return d->tokenSecret;
}
void QOAuth1Signature::setTokenSecret(const QString &secret)
{
d_ptr(&d)->tokenSecret = secret;
Q_D(QOAuth1Signature);
d->tokenSecret = secret;
}
QByteArray QOAuth1Signature::hmacSha1() const
{
const auto priv = d_ptr(&d);
Q_D(const QOAuth1Signature);
QMessageAuthenticationCode code(QCryptographicHash::Sha1);
code.setKey(priv->secret());
code.addData(priv->signatureBaseString(*this));
code.setKey(d->secret());
code.addData(d->signatureBaseString());
return code.result();
}
......@@ -233,9 +242,10 @@ QByteArray QOAuth1Signature::rsaSha1() const
QByteArray QOAuth1Signature::plainText(const QString &clientIdentifier) const
{
Q_D(const QOAuth1Signature);
QByteArray ret;
ret.append(clientIdentifier.toUtf8());
ret.append('&');
ret.append(d_ptr(&d)->clientSharedKey.toUtf8());
ret.append(d->clientSharedKey.toUtf8());
return ret;
}
......@@ -44,7 +44,7 @@
#include <QtCore/qvariant.h>
class QOAuth1SignaturePrivate;
class QOAuth1Signature : public QVariantMap
class QOAuth1Signature
{
public:
enum class HttpRequestMethod {
......@@ -58,9 +58,9 @@ public:
Unknown = 0
};
using QVariantMap::QVariantMap;
explicit QOAuth1Signature(const QUrl &url, HttpRequestMethod method = HttpRequestMethod::Post,
const QVariantMap &parameters = QVariantMap());
explicit QOAuth1Signature(const QUrl &url = QUrl(),
HttpRequestMethod method = HttpRequestMethod::Post,
const QVariantMap &parameters = QVariantMap());
QOAuth1Signature(const QUrl &url, const QString &clientSharedKey, const QString &tokenSecret,
HttpRequestMethod method = HttpRequestMethod::Post,
const QVariantMap &parameters = QVariantMap());
......@@ -75,6 +75,7 @@ public:
QVariantMap parameters() const;
void setParameters(const QVariantMap &parameters);
void addRequestBody(const QUrlQuery &body);
void insert(const QString &key, const QVariant &value);
QString clientSharedKey() const;
void setClientSharedKey(const QString &secret);
......@@ -87,8 +88,8 @@ public:
QByteArray plainText(const QString &clientIdentifier) const;
private:
friend class QOAuth1SignaturePrivate;
QScopedPointer<QOAuth1SignaturePrivate> d;
Q_DECLARE_PRIVATE(QOAuth1Signature)
QOAuth1SignaturePrivate *d_ptr;
};
#endif // QOAUTH1SIGNATURE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment