Commit 7c06c1a0 authored by Jesus Fernandez's avatar Jesus Fernandez
Browse files

Removed Credetial structs

parent 742d3fb9
......@@ -77,8 +77,8 @@ public:
static QString generateRandomState();
QAbstractOAuth2::Credential consumer;
QAbstractOAuth2::Credential token;
QPair<QString, QString> clientCredentials;
QString token;
QString scope;
QString userAgent = "qoauth/0.1";
const QString bearerFormat = QStringLiteral("Bearer %1");
......
......@@ -72,11 +72,11 @@ class OAUTH_EXPORT QOAuth1Private : public QAbstractOAuthPrivate
public:
QOAuth1Private(QNetworkAccessManager *networkAccessManager);
QPair<QString, QString> clientCredentials;
QPair<QString, QString> tokenCredentials;
QString verifier;
QUrl temporaryCredentialsUrl;
QUrl tokenCredentialsUrl;
QOAuth1::Credential clientCredentials;
QOAuth1::Credential token;
QOAuth1::SignatureMethod signatureMethod = QOAuth1::SignatureMethod::Hmac_Sha1;
const QString oauthVersion = QStringLiteral("1.0");
......@@ -96,7 +96,7 @@ public:
QNetworkReply *requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QOAuth1::Credential &token,
const QPair<QString, QString> &token,
const QVariantMap &additionalParameters);
QString signatureMethodString() const;
......
......@@ -95,13 +95,6 @@ QUrlQuery QAbstractOAuthPrivate::createQuery(const QVariantMap &parameters)
return query;
}
QAbstractOAuth::Credential::Credential()
{}
QAbstractOAuth::Credential::Credential(const QString &credentialKey)
: key(credentialKey)
{}
QAbstractOAuth::QAbstractOAuth(QAbstractOAuthPrivate &dd, QObject *parent)
: QObject(dd, parent)
{
......@@ -198,4 +191,3 @@ QString QAbstractOAuth::callback() const
QT_END_NAMESPACE
#endif // QT_NO_HTTP
......@@ -73,13 +73,6 @@ class OAUTH_EXPORT QAbstractOAuth : public QObject
Q_PROPERTY(QVariantMap extraTokens READ extraTokens NOTIFY extraTokensChanged)
public:
struct OAUTH_EXPORT Credential {
QString key;
Credential();
Credential(const QString &credentialKey);
};
enum class Status {
NotAuthenticated,
TemporaryTokenReceived,
......@@ -106,6 +99,12 @@ public:
virtual ~QAbstractOAuth();
virtual QString clientIdentifier() const = 0;
virtual void setClientIdentifier(const QString &clientIdentifier) = 0;
virtual QString token() const = 0;
virtual void setToken(const QString &token) = 0;
QNetworkAccessManager *networkAccessManager() const;
void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager);
......@@ -132,12 +131,15 @@ public Q_SLOTS:
virtual void grant() = 0;
Q_SIGNALS:
void granted();
void requestFailed(const Error error);
void clientIdentifierChanged(const QString &clientIdentifier);
void tokenChanged(const QString &token);
void statusChanged(Status status);
void authorizationUrlChanged(const QUrl &url);
void extraTokensChanged(const QVariantMap &tokens);
void requestFailed(const Error error);
void authorizationRequested(const QUrl &url);
void granted();
protected:
explicit QAbstractOAuth(QAbstractOAuthPrivate &, QObject *parent = nullptr);
......
......@@ -74,9 +74,7 @@ const QString Key::tokenType = QStringLiteral("token_type");
QAbstractOAuth2Private::QAbstractOAuth2Private(QNetworkAccessManager *manager)
: QAbstractOAuthPrivate(manager)
{
qRegisterMetaType<QAbstractOAuth2::Credential>("QAbstractOAuth2::Credential");
}
{}
QAbstractOAuth2Private::~QAbstractOAuth2Private()
{}
......@@ -122,7 +120,7 @@ QNetworkReply *QAbstractOAuth2::get(const QNetworkRequest &req, const QVariantMa
QNetworkRequest request(req);
request.setUrl(url);
request.setHeader(QNetworkRequest::UserAgentHeader, d->userAgent);
const QString bearer = d->bearerFormat.arg(d->token.key);
const QString bearer = d->bearerFormat.arg(d->token);
request.setRawHeader("Authorization", bearer.toUtf8());
return d->networkAccessManager()->get(request);
......@@ -143,7 +141,7 @@ QNetworkReply *QAbstractOAuth2::post(const QNetworkRequest &req, const QVariantM
QNetworkRequest request(req);
request.setUrl(url);
request.setHeader(QNetworkRequest::UserAgentHeader, d->userAgent);
const QString bearer = d->bearerFormat.arg(d->token.key);
const QString bearer = d->bearerFormat.arg(d->token);
request.setRawHeader("Authorization", bearer.toUtf8());
return d->networkAccessManager()->post(request, QByteArray());
......@@ -179,31 +177,46 @@ void QAbstractOAuth2::setUserAgent(const QString &userAgent)
}
}
QAbstractOAuth2::Credential QAbstractOAuth2::consumerToken() const
QString QAbstractOAuth2::clientIdentifier() const
{
Q_D(const QAbstractOAuth2);
return d->consumer;
return d->clientCredentials.first;
}
void QAbstractOAuth2::setClientIdentifier(const QString &clientIdentifier)
{
Q_D(QAbstractOAuth2);
if (d->clientCredentials.first != clientIdentifier) {
d->clientCredentials.first = clientIdentifier;
Q_EMIT clientIdentifierChanged(clientIdentifier);
}
}
QAbstractOAuth2::Credential QAbstractOAuth2::token() const
QString QAbstractOAuth2::clientIdentifierSharedKey() const
{
Q_D(const QAbstractOAuth2);
return d->token;
return d->clientCredentials.second;
}
void QAbstractOAuth2::setConsumerToken(const QAbstractOAuth2::Credential consumerToken)
void QAbstractOAuth2::setClientIdentifierSharedKey(const QString &clientIdentifierSharedKey)
{
Q_D(QAbstractOAuth2);
if (d->consumer.key != consumerToken.key) {
d->consumer.key = consumerToken.key;
Q_EMIT consumerTokenChanged(consumerToken);
if (d->clientCredentials.second != clientIdentifierSharedKey) {
d->clientCredentials.second = clientIdentifierSharedKey;
Q_EMIT clientIdentifierSharedKeyChanged(clientIdentifierSharedKey);
}
}
void QAbstractOAuth2::setToken(const QAbstractOAuth2::Credential &token)
QString QAbstractOAuth2::token() const
{
Q_D(const QAbstractOAuth2);
return d->token;
}
void QAbstractOAuth2::setToken(const QString &token)
{
Q_D(QAbstractOAuth2);
if (d->token.key != token.key) {
if (d->token != token) {
d->token = token;
Q_EMIT tokenChanged(token);
}
......
......@@ -50,8 +50,13 @@ class QAbstractOAuth2Private;
class OAUTH_EXPORT QAbstractOAuth2 : public QAbstractOAuth
{
Q_OBJECT
Q_PROPERTY(QString clientIdentifier
READ clientIdentifier
WRITE setClientIdentifier
NOTIFY clientIdentifierChanged)
public:
explicit QAbstractOAuth2(QObject *parent = nullptr);
explicit QAbstractOAuth2(QNetworkAccessManager *manager, QObject *parent = nullptr);
virtual ~QAbstractOAuth2();
......@@ -69,20 +74,24 @@ public:
virtual QString responseType() const = 0;
virtual QString grantType() const = 0;
Credential consumerToken() const;
Credential token() const;
virtual QString clientIdentifier() const override;
void setClientIdentifier(const QString &clientIdentifier);
QString clientIdentifierSharedKey() const;
void setClientIdentifierSharedKey(const QString &clientIdentifierSharedKey);
QString token() const;
void setToken(const QString &token);
Q_SIGNALS:
void scopeChanged(const QString &scope);
void userAgentChanged(const QString &userAgent);
void apiKeyChanged(const QString &apiKey);
void consumerTokenChanged(const Credential &token);
void tokenChanged(const Credential &token);
void clientIdentifierChanged(const QString &clientIdentifier);
void clientIdentifierSharedKeyChanged(const QString &clientIdentifierSharedKey);
void consumerTokenKeyChanged(const QString &key);
void tokenChanged(const QString &token);
public Q_SLOTS:
void setConsumerToken(const QAbstractOAuth2::Credential consumerToken);
void setToken(const QAbstractOAuth2::Credential &token);
protected:
explicit QAbstractOAuth2(QAbstractOAuth2Private &, QObject *parent = nullptr);
......
......@@ -77,8 +77,6 @@ static auto networkReplyErrorFunctionPointer = static_cast<void(QNetworkReply::*
QOAuth1Private::QOAuth1Private(QNetworkAccessManager *networkAccessManager)
: QAbstractOAuthPrivate(networkAccessManager)
{
Q_ASSERT(networkAccessManager);
qRegisterMetaType<QOAuth1::Credential>("QOAuth1::Credential");
qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
}
......@@ -115,9 +113,9 @@ void QOAuth1Private::_q_requestAuthorizationGrantAnswer()
extraTokens.clear();
for (auto it = response.begin(), end = response.end(); it != end; ++it)
extraTokens.insert(it.key(), it.value());
const auto oauthToken = response.value(Key::oauthToken);
const auto oauthTokenSecret = response.value(Key::oauthTokenSecret);
q->setToken(QOAuth1::Credential(oauthToken, oauthTokenSecret));
const QString oauthToken = response.value(Key::oauthToken);
const QString oauthTokenSecret = response.value(Key::oauthTokenSecret);
q->setTokenCredentials(qMakePair(oauthToken, oauthTokenSecret));
response.remove(Key::oauthToken);
response.remove(Key::oauthTokenSecret);
q->setStatus(QAbstractOAuth::Status::Granted);
......@@ -126,7 +124,7 @@ void QOAuth1Private::_q_requestAuthorizationGrantAnswer()
}
else {
q->setStatus(QAbstractOAuth::Status::NotAuthenticated);
q->setToken(QOAuth1::Credential());
q->setTokenCredentials(QPair<QString, QString>());
}
} else {
Q_EMIT q->requestFailed(QAbstractOAuth::Error::OAuthTokenNotFoundError);
......@@ -138,17 +136,20 @@ void QOAuth1Private::_q_tokensReceived(const QVariantMap &tokens)
Q_Q(QOAuth1);
typedef OAuth1KeyString Key;
QOAuth1::Credential credential(tokens.value(Key::oauthToken).toString(),
tokens.value(Key::oauthTokenSecret).toString());
QPair<QString, QString> credential(tokens.value(Key::oauthToken).toString(),
tokens.value(Key::oauthTokenSecret).toString());
switch (status) {
case QAbstractOAuth::Status::NotAuthenticated:
if (tokens.value(Key::oauthCallbackConfirmed, true).toBool())
q->setToken(credential);
if (tokens.value(Key::oauthCallbackConfirmed, true).toBool()) {
q->setTokenCredentials(credential);
setStatus(QAbstractOAuth::Status::TemporaryTokenReceived);
}
else
Q_EMIT q->requestFailed(QAbstractOAuth::Error::OAuthCallbackNotVerified);
break;
case QAbstractOAuth::Status::TemporaryTokenReceived:
q->setToken(credential);
q->setTokenCredentials(credential);
setStatus(QAbstractOAuth::Status::Granted);
break;
case QAbstractOAuth::Status::Granted:
break;
......@@ -163,7 +164,7 @@ void QOAuth1Private::appendCommonHeaders(QVariantMap *headers)
const auto currentDateTime = QDateTime::currentDateTimeUtc();
headers->insert(Key::oauthNonce, QOAuth1::nonce());
headers->insert(Key::oauthConsumerKey, clientCredentials.key);
headers->insert(Key::oauthConsumerKey, clientCredentials.first);
headers->insert(Key::oauthTimestamp, QString::number(currentDateTime.toTime_t()));
headers->insert(Key::oauthVersion, oauthVersion);
headers->insert(Key::oauthSignatureMethod, signatureMethodString().toUtf8());
......@@ -190,7 +191,7 @@ void QOAuth1Private::appendSignature(QAbstractOAuth::Stage stage,
QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QOAuth1::Credential &token,
const QPair<QString, QString> &token,
const QVariantMap &parameters)
{
Q_Q(QOAuth1);
......@@ -207,8 +208,8 @@ QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation ope
QVariantMap headers;
appendCommonHeaders(&headers);
headers.insert(Key::oauthCallback, q->callback());
if (!token.key.isEmpty()) {
headers.insert(Key::oauthToken, token.key);
if (!token.first.isEmpty()) {
headers.insert(Key::oauthToken, token.first);
stage = QAbstractOAuth::Stage::RequestingAccessToken;
}
appendSignature(stage, &headers, url, operation, parameters);
......@@ -276,52 +277,119 @@ QString QOAuth1Private::operationName(QNetworkAccessManager::Operation op)
return QString();
}
QOAuth1::Credential::Credential()
QOAuth1::QOAuth1(QNetworkAccessManager *manager, QObject *parent)
: QAbstractOAuth(*new QOAuth1Private(manager), parent)
{}
QOAuth1::Credential::Credential(const QString &credentialKey, const QString &credentialSecret)
: QAbstractOAuth::Credential(credentialKey), secret(credentialSecret)
QOAuth1::QOAuth1(const QString &clientIdentifier,
const QString &clientSharedSecret,
QNetworkAccessManager *manager,
QObject *parent)
: QAbstractOAuth(*new QOAuth1Private(manager), parent)
{
Q_D(QOAuth1);
d->clientCredentials.first = clientIdentifier;
d->clientCredentials.second = clientSharedSecret;
}
QOAuth1::~QOAuth1()
{}
bool QOAuth1::Credential::operator ==(const QOAuth1::Credential &other)
QString QOAuth1::clientIdentifier() const
{
return key == other.key && secret == other.secret;
Q_D(const QOAuth1);
return d->clientCredentials.first;
}
bool QOAuth1::Credential::operator !=(const QOAuth1::Credential &other)
void QOAuth1::setClientIdentifier(const QString &clientIdentifier)
{
return !operator ==(other);
Q_D(QOAuth1);
if (d->clientCredentials.first != clientIdentifier) {
d->clientCredentials.first = clientIdentifier;
Q_EMIT clientIdentifierChanged(clientIdentifier);
}
}
QOAuth1::QOAuth1(QNetworkAccessManager *manager, QObject *parent)
: QAbstractOAuth(*new QOAuth1Private(manager), parent)
{}
QString QOAuth1::clientSharedSecret() const
{
Q_D(const QOAuth1);
return d->clientCredentials.second;
}
QOAuth1::QOAuth1(const Credential &consumerToken, QNetworkAccessManager *manager, QObject *parent)
: QAbstractOAuth(*new QOAuth1Private(manager), parent)
void QOAuth1::setClientSharedSecret(const QString &clientSharedSecret)
{
Q_D(QOAuth1);
d->clientCredentials = consumerToken;
if (d->clientCredentials.second != clientSharedSecret) {
d->clientCredentials.second = clientSharedSecret;
Q_EMIT clientSharedSecretChanged(clientSharedSecret);
}
}
QOAuth1::~QOAuth1()
{}
QOAuth1::Credential QOAuth1::clientCredentials() const
QPair<QString, QString> QOAuth1::clientCredentials() const
{
Q_D(const QOAuth1);
return d->clientCredentials;
}
void QOAuth1::setClientCredentials(const QOAuth1::Credential &clientCredentials)
void QOAuth1::setClientCredentials(const QPair<QString, QString> &clientCredentials)
{
setClientIdentifier(clientCredentials.first);
setClientSharedSecret(clientCredentials.second);
}
void QOAuth1::setClientCredentials(const QString &clientIdentifier,
const QString &clientSharedSecret)
{
setClientCredentials(qMakePair(clientIdentifier, clientSharedSecret));
}
QString QOAuth1::token() const
{
Q_D(const QOAuth1);
return d->tokenCredentials.first;
}
void QOAuth1::setToken(const QString &token)
{
Q_D(QOAuth1);
if (d->clientCredentials != clientCredentials) {
d->clientCredentials = clientCredentials;
Q_EMIT clientCredentialsChanged(clientCredentials);
if (d->tokenCredentials.first != token) {
d->tokenCredentials.first = token;
Q_EMIT tokenChanged(token);
}
}
QString QOAuth1::tokenSecret() const
{
Q_D(const QOAuth1);
return d->tokenCredentials.second;
}
void QOAuth1::setTokenSecret(const QString &tokenSecret)
{
Q_D(QOAuth1);
if (d->tokenCredentials.second != tokenSecret) {
d->tokenCredentials.second = tokenSecret;
Q_EMIT tokenSecretChanged(tokenSecret);
}
}
QPair<QString, QString> QOAuth1::tokenCredentials() const
{
Q_D(const QOAuth1);
return d->tokenCredentials;
}
void QOAuth1::setTokenCredentials(const QPair<QString, QString> &tokenCredentials)
{
setToken(tokenCredentials.first);
setTokenSecret(tokenCredentials.second);
}
void QOAuth1::setTokenCredentials(const QString &token, const QString &tokenSecret)
{
setTokenCredentials(qMakePair(token, tokenSecret));
}
QUrl QOAuth1::temporaryCredentialsUrl() const
{
Q_D(const QOAuth1);
......@@ -367,21 +435,6 @@ void QOAuth1::setSignatureMethod(QOAuth1::SignatureMethod value)
}
}
QOAuth1::Credential QOAuth1::token() const
{
Q_D(const QOAuth1);
return d->token;
}
void QOAuth1::setToken(const QOAuth1::Credential &token)
{
Q_D(QOAuth1);
if (d->token != token) {
d->token = token;
Q_EMIT tokenChanged(token);
}
}
QNetworkReply *QOAuth1::get(const QNetworkRequest &req, const QVariantMap &parameters)
{
Q_D(QOAuth1);
......@@ -431,14 +484,14 @@ QNetworkReply *QOAuth1::requestTemporaryCredentials(QNetworkAccessManager::Opera
qCritical("Operation not supported");
return nullptr;
}
d->token = Credential();
return d->requestToken(operation, url, d->token, parameters);
d->tokenCredentials = QPair<QString, QString>();
return d->requestToken(operation, url, d->tokenCredentials, parameters);
}
QNetworkReply *QOAuth1::requestTokenCredentials(QNetworkAccessManager::Operation operation,
const QUrl &url,
const Credential &temporaryCredentials,
const QVariantMap &parameters)
const QUrl &url,
const QPair<QString, QString> &temporaryToken,
const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (!d->networkAccessManager()) {
......@@ -454,7 +507,7 @@ QNetworkReply *QOAuth1::requestTokenCredentials(QNetworkAccessManager::Operation
qCritical("Operation not supported");
return nullptr;
}
return d->requestToken(operation, url, temporaryCredentials, parameters);
return d->requestToken(operation, url, temporaryToken, parameters);
}
QByteArray QOAuth1::generateSignature(const QVariantMap &parameters,
......@@ -464,11 +517,12 @@ QByteArray QOAuth1::generateSignature(const QVariantMap &parameters,
Q_D(QOAuth1);
QByteArray ret;
if (signatureMethod() == SignatureMethod::Hmac_Sha1) {
ret = signature(parameters, url, operation, d->clientCredentials.secret, d->token.secret);
ret = signature(parameters, url, operation, d->clientCredentials.second,
d->tokenCredentials.second);
} else if (signatureMethod() == SignatureMethod::PlainText) {
ret.append(d->clientCredentials.key.toUtf8());
ret.append(d->clientCredentials.first.toUtf8());
ret.append('&');
ret.append(d->clientCredentials.secret.toUtf8());
ret.append(d->clientCredentials.second.toUtf8());
}
return ret;
}
......@@ -492,9 +546,9 @@ void QOAuth1::setup(QNetworkRequest *request,
const auto currentDateTime = QDateTime::currentDateTimeUtc();
oauthParams.insert(Key::oauthConsumerKey, d->clientCredentials.key);
oauthParams.insert(Key::oauthConsumerKey, d->clientCredentials.first);
oauthParams.insert(Key::oauthVersion, QStringLiteral("1.0"));
oauthParams.insert(Key::oauthToken, d->token.key);
oauthParams.insert(Key::oauthToken, d->tokenCredentials.first);
oauthParams.insert(Key::oauthSignatureMethod, d->signatureMethodString());
oauthParams.insert(Key::oauthNonce, QOAuth1::nonce());
oauthParams.insert(Key::oauthTimestamp, QString::number(currentDateTime.toTime_t()));
......@@ -635,14 +689,13 @@ void QOAuth1::grant()
return;
}
connect(this, &QOAuth1::tokenChanged, [&](const Credential &) {
connect(this, &QAbstractOAuth::statusChanged, [&](Status status) {
Q_D(QOAuth1);
if (d->status == Status::NotAuthenticated) {
d->setStatus(Status::TemporaryTokenReceived);
if (status == Status::TemporaryTokenReceived) {
if (!d->authorizationUrl.isEmpty()) {
QVariantMap parameters;
parameters.insert(Key::oauthToken, d->token.key);
parameters.insert(Key::oauthToken, d->tokenCredentials.first);
if (d->modifyParametersFunction)
d->modifyParametersFunction(Stage::RequestingAuthorization, &parameters);
......@@ -654,10 +707,9 @@ void QOAuth1::grant()
// try upgrading token without verifier
requestTokenCredentials(QNetworkAccessManager::PostOperation,
d->tokenCredentialsUrl,
d->token);
d->tokenCredentials);
}
} else if (d->status == Status::TemporaryTokenReceived) {
d->setStatus(Status::Granted);
} else if (status == Status::Granted) {
Q_EMIT granted();
}
});
......@@ -688,7 +740,7 @@ void QOAuth1::continueGrantWithVerifier(const QString &verifier)
parameters.insert(Key::oauthVerifier, verifier);
requestTokenCredentials(QNetworkAccessManager::PostOperation,
d->tokenCredentialsUrl,
d->token,
d->tokenCredentials,
parameters);
}
......
......@@ -52,16 +52,6 @@ class OAUTH_EXPORT QOAuth1: public QAbstractOAuth
Q_OBJECT
public:
struct OAUTH_EXPORT Credential : QAbstractOAuth::Credential
{
QString secret;
Credential();
Credential(const QString &credentialKey, const QString &credentialSecret);
bool operator ==(const Credential &other);
bool operator !=(const Credential &other);
};
enum class SignatureMethod {
Hmac_Sha1,
Rsa_Sha1,
......@@ -73,15 +63,30 @@ public:
explicit QOAuth1(QNetworkAccessManager *manager,
QObject *parent = nullptr);
QOAuth1(const Credential &clientCredentials,
QOAuth1(const QString &clientIdentifier,
const QString &clientSharedSecret,
QNetworkAccessManager *manager,
QObject *parent = nullptr);
virtual ~QOAuth1();
// Client credentials
QOAuth1::Credential clientCredentials() const;
void setClientCredentials(const QOAuth1::Credential &clientCredentials);
QString clientIdentifier() const override;
void setClientIdentifier(const QString &clientIdentifier) override;
QString clientSharedSecret() const;
void setClientSharedSecret(const QString &clientSharedSecret);
QPair<QString, QString> client