Commit fe47d98d authored by Jesus Fernandez's avatar Jesus Fernandez

Fixed Twitter example

Fixed Reddit example
parent 7c06c1a0
......@@ -72,11 +72,12 @@ class OAUTH_EXPORT QAbstractOAuth2Private : public QAbstractOAuthPrivate
Q_DECLARE_PUBLIC(QAbstractOAuth2)
public:
QAbstractOAuth2Private(QNetworkAccessManager *manager);
QAbstractOAuth2Private(QNetworkAccessManager *manager = nullptr, quint16 port = 0);
~QAbstractOAuth2Private();
static QString generateRandomState();
quint16 port = 0;
QPair<QString, QString> clientCredentials;
QString token;
QString scope;
......
......@@ -84,7 +84,7 @@ public:
QVariantMap extraTokens;
QAbstractOAuth::Status status = QAbstractOAuth::Status::NotAuthenticated;
QNetworkAccessManager::Operation operation;
QScopedPointer<QAbstractOAuthReplyHandler> replyHandler;
QPointer<QAbstractOAuthReplyHandler> replyHandler;
QScopedPointer<QOAuthOobReplyHandler> defaultReplyHandler;
QPointer<QNetworkAccessManager> networkAccessManagerPointer;
QAbstractOAuth::ModifyParametersFunction modifyParametersFunction;
......
......@@ -70,7 +70,7 @@ class OAUTH_EXPORT QOAuth1Private : public QAbstractOAuthPrivate
Q_DECLARE_PUBLIC(QOAuth1)
public:
QOAuth1Private(QNetworkAccessManager *networkAccessManager);
QOAuth1Private(QNetworkAccessManager *networkAccessManager = nullptr);
QPair<QString, QString> clientCredentials;
QPair<QString, QString> tokenCredentials;
......
......@@ -67,6 +67,7 @@ class OAUTH_EXPORT QOAuth2AuthorizationCodeFlowPrivate : public QAbstractOAuth2P
Q_DECLARE_PUBLIC(QOAuth2AuthorizationCodeFlow)
public:
QOAuth2AuthorizationCodeFlowPrivate(quint16 port = 0);
QOAuth2AuthorizationCodeFlowPrivate(QNetworkAccessManager *manager, quint16 port);
void handleCallback(const QVariantMap &data, const QString state);
......
......@@ -61,9 +61,7 @@ QAbstractOAuthPrivate::QAbstractOAuthPrivate(QNetworkAccessManager *manager)
: replyHandler(nullptr),
defaultReplyHandler(new QOAuthOobReplyHandler),
networkAccessManagerPointer(manager)
{
Q_ASSERT(manager);
}
{}
QAbstractOAuthPrivate::~QAbstractOAuthPrivate()
{}
......@@ -159,7 +157,7 @@ QAbstractOAuthReplyHandler *QAbstractOAuth::replyHandler() const
void QAbstractOAuth::setReplyHandler(QAbstractOAuthReplyHandler *handler)
{
Q_D(QAbstractOAuth);
d->replyHandler.reset(handler);
d->replyHandler = handler;
}
QAbstractOAuth::ModifyParametersFunction QAbstractOAuth::modifyParametersFunction() const
......
......@@ -72,8 +72,9 @@ const QString Key::state = QStringLiteral("state");
const QString Key::tokenType = QStringLiteral("token_type");
QAbstractOAuth2Private::QAbstractOAuth2Private(QNetworkAccessManager *manager)
QAbstractOAuth2Private::QAbstractOAuth2Private(QNetworkAccessManager *manager, quint16 port)
: QAbstractOAuthPrivate(manager)
, port(port)
{}
QAbstractOAuth2Private::~QAbstractOAuth2Private()
......@@ -222,6 +223,21 @@ void QAbstractOAuth2::setToken(const QString &token)
}
}
quint16 QAbstractOAuth2::port() const
{
Q_D(const QAbstractOAuth2);
return d->port;
}
void QAbstractOAuth2::setPort(quint16 port)
{
Q_D(QAbstractOAuth2);
if (d->port != port) {
d->port = port;
Q_EMIT portChanged(port);
}
}
QT_END_NAMESPACE
#endif // QT_NO_HTTP
......@@ -82,6 +82,9 @@ public:
QString token() const;
void setToken(const QString &token);
quint16 port() const;
void setPort(quint16 port);
Q_SIGNALS:
void scopeChanged(const QString &scope);
void userAgentChanged(const QString &userAgent);
......@@ -90,6 +93,7 @@ Q_SIGNALS:
void clientIdentifierSharedKeyChanged(const QString &clientIdentifierSharedKey);
void consumerTokenKeyChanged(const QString &key);
void tokenChanged(const QString &token);
void portChanged(quint16 port);
public Q_SLOTS:
protected:
......
......@@ -277,6 +277,10 @@ QString QOAuth1Private::operationName(QNetworkAccessManager::Operation op)
return QString();
}
QOAuth1::QOAuth1(QObject *parent)
: QAbstractOAuth(*new QOAuth1Private, parent)
{}
QOAuth1::QOAuth1(QNetworkAccessManager *manager, QObject *parent)
: QAbstractOAuth(*new QOAuth1Private(manager), parent)
{}
......
......@@ -60,6 +60,7 @@ public:
Q_ENUM(SignatureMethod)
explicit QOAuth1(QObject *parent = nullptr);
explicit QOAuth1(QNetworkAccessManager *manager,
QObject *parent = nullptr);
......
......@@ -55,13 +55,16 @@
QT_BEGIN_NAMESPACE
QOAuth2AuthorizationCodeFlowPrivate::QOAuth2AuthorizationCodeFlowPrivate(
QNetworkAccessManager *manager, quint16 port)
: QAbstractOAuth2Private(manager)
QOAuth2AuthorizationCodeFlowPrivate::QOAuth2AuthorizationCodeFlowPrivate(quint16 port)
{
replyHandler.reset(new QOAuthHttpServerReplyHandler(port));
QAbstractOAuth2Private::port = port;
}
QOAuth2AuthorizationCodeFlowPrivate::QOAuth2AuthorizationCodeFlowPrivate(
QNetworkAccessManager *manager, quint16 port)
: QAbstractOAuth2Private(manager, port)
{}
void QOAuth2AuthorizationCodeFlowPrivate::handleCallback(const QVariantMap &data,
const QString state)
{
......@@ -155,6 +158,10 @@ void QOAuth2AuthorizationCodeFlowPrivate::authenticate(QNetworkReply *reply,
}
}
QOAuth2AuthorizationCodeFlow::QOAuth2AuthorizationCodeFlow(QObject *parent)
: QAbstractOAuth2(*new QOAuth2AuthorizationCodeFlowPrivate, parent)
{}
QOAuth2AuthorizationCodeFlow::QOAuth2AuthorizationCodeFlow(QNetworkAccessManager *manager,
quint16 port,
QObject *parent)
......@@ -227,14 +234,17 @@ void QOAuth2AuthorizationCodeFlow::setAccessTokenUrl(const QUrl &accessTokenUrl)
void QOAuth2AuthorizationCodeFlow::grant()
{
Q_D(const QOAuth2AuthorizationCodeFlow);
Q_D(QOAuth2AuthorizationCodeFlow);
if (d->authorizationUrl.isEmpty()) {
qCritical("QOAuth2AuthorizationCodeFlow: No authenticate Url set");
return;
} else if (d->accessTokenUrl.isEmpty()) {
qCritical("QOAuth2AuthorizationCodeFlow: No request access token Url set");
return;
} else if (d->replyHandler.isNull()) {
d->replyHandler = new QOAuthHttpServerReplyHandler(d->port, this);
}
const QUrl url = buildAuthenticateUrl();
Q_EMIT authorizationRequested(url);
}
......
......@@ -56,6 +56,7 @@ class OAUTH_EXPORT QOAuth2AuthorizationCodeFlow : public QAbstractOAuth2
Q_OBJECT
public:
explicit QOAuth2AuthorizationCodeFlow(QObject *parent = nullptr);
explicit QOAuth2AuthorizationCodeFlow(QNetworkAccessManager *manager,
quint16 port = 0,
QObject *parent = nullptr);
......
......@@ -53,7 +53,7 @@
QT_BEGIN_NAMESPACE
QOAuth2ImplicitGrantFlowPrivate::QOAuth2ImplicitGrantFlowPrivate(QNetworkAccessManager *manager)
: QAbstractOAuth2Private(manager)
: QAbstractOAuth2Private(manager, 0)
{}
QOAuth2ImplicitGrantFlowPrivate::~QOAuth2ImplicitGrantFlowPrivate()
......
......@@ -48,14 +48,12 @@ const QUrl liveThreadsUrl("https://oauth.reddit.com/live/XXXX/about.json");
RedditWrapper::RedditWrapper(QObject *parent)
: QObject(parent)
, oauth2(&manager)
{
init();
}
RedditWrapper::RedditWrapper(const QString &clientIdentifier, QObject *parent)
: QObject(parent)
, oauth2(&manager)
{
oauth2.setClientIdentifier(clientIdentifier);
init();
......@@ -118,6 +116,9 @@ void RedditWrapper::subscribeToLiveUpdates()
void RedditWrapper::init()
{
oauth2.setPort(1337);
oauth2.setAuthorizationUrl(QUrl("https://www.reddit.com/api/v1/authorize"));
oauth2.setAccessTokenUrl(QUrl("https://www.reddit.com/api/v1/access_token"));
oauth2.setScope("identity read");
connect(&oauth2, &QOAuth2AuthorizationCodeFlow::statusChanged, [=](
......@@ -125,19 +126,10 @@ void RedditWrapper::init()
if (status == QAbstractOAuth::Status::Granted)
emit authenticated();
});
}
RedditWrapper::RedditOAuth::RedditOAuth(QNetworkAccessManager *manager)
: QOAuth2AuthorizationCodeFlow(manager, 1337)
{
setAuthorizationUrl(QUrl("https://www.reddit.com/api/v1/authorize"));
setAccessTokenUrl(QUrl("https://www.reddit.com/api/v1/access_token"));
}
void RedditWrapper::RedditOAuth::grant()
{
QVariantMap parameters;
parameters.insert("duration", "permanent");
const QUrl url = buildAuthenticateUrl(parameters);
QDesktopServices::openUrl(url);
oauth2.setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap *parameters) {
if (stage == QAbstractOAuth::Stage::RequestingAuthorization)
parameters->insert("duration", "permanent");
});
connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizationRequested,
&QDesktopServices::openUrl);
}
......@@ -67,12 +67,7 @@ signals:
private:
void init();
QNetworkAccessManager manager;
struct RedditOAuth : QOAuth2AuthorizationCodeFlow
{
RedditOAuth(QNetworkAccessManager *manager);
virtual void grant() override;
} oauth2;
QOAuth2AuthorizationCodeFlow oauth2;
};
#endif // REDDITWRAPPER_H
......@@ -44,13 +44,13 @@
#include <QtNetwork>
Twitter::Twitter(QObject *parent)
: QOAuth1(&manager, parent)
: QOAuth1(parent)
{
init();
}
Twitter::Twitter(const QPair<QString, QString> &clientCredentials, QObject *parent)
: QOAuth1(&manager, parent)
: QOAuth1(parent)
{
init();
setClientCredentials(clientCredentials);
......@@ -60,7 +60,7 @@ Twitter::Twitter(const QPair<QString, QString> &clientCredentials, QObject *pare
Twitter::Twitter(const QString &screenName,
const QPair<QString, QString> &clientCredentials,
QObject *parent)
: QOAuth1(&manager, parent)
: QOAuth1(parent)
{
Twitter::screenName = screenName;
init();
......
......@@ -62,7 +62,6 @@ private:
void init();
QNetworkAccessManager manager;
QOAuthHttpServerReplyHandler *replyHandler;
QString screenName;
};
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