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