qabstractoauth.h 5.49 KB
Newer Older
Jesus Fernandez's avatar
Jesus Fernandez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtNetwork module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QABSTRACTOAUTH_H
#define QABSTRACTOAUTH_H

#ifndef QT_NO_HTTP

#include <QtCore/qobject.h>

Jesus Fernandez's avatar
Jesus Fernandez committed
47 48 49
#include <QtCore/qurl.h>
#include <QtCore/qstring.h>

Jesus Fernandez's avatar
Jesus Fernandez committed
50 51
#include <QtNetwork/qnetworkaccessmanager.h>

Jesus Fernandez's avatar
Jesus Fernandez committed
52 53 54 55 56 57 58
#include <functional>

#if defined(OAUTH_LIBRARY)
# define OAUTH_EXPORT Q_DECL_EXPORT
#else
# define OAUTH_EXPORT Q_DECL_IMPORT
#endif
Jesus Fernandez's avatar
Jesus Fernandez committed
59

Jesus Fernandez's avatar
Jesus Fernandez committed
60
QT_BEGIN_NAMESPACE
Jesus Fernandez's avatar
Jesus Fernandez committed
61 62

class QUrl;
Jesus Fernandez's avatar
Jesus Fernandez committed
63
class QString;
Jesus Fernandez's avatar
Jesus Fernandez committed
64 65 66 67 68 69
class QByteArray;
class QNetworkReply;
class QNetworkRequest;
class QAbstractOAuthReplyHandler;

class QAbstractOAuthPrivate;
Jesus Fernandez's avatar
Jesus Fernandez committed
70
class OAUTH_EXPORT QAbstractOAuth : public QObject
Jesus Fernandez's avatar
Jesus Fernandez committed
71 72 73 74 75
{
    Q_OBJECT

    Q_PROPERTY(Status status  READ status NOTIFY statusChanged)
    Q_PROPERTY(QVariantMap extraTokens READ extraTokens NOTIFY extraTokensChanged)
Jesus Fernandez's avatar
Jesus Fernandez committed
76 77 78 79
    Q_PROPERTY(QUrl authorizationUrl
               READ authorizationUrl
               WRITE setAuthorizationUrl
               NOTIFY authorizationUrlChanged)
Jesus Fernandez's avatar
Jesus Fernandez committed
80 81 82 83

public:
    enum class Status {
        NotAuthenticated,
Jesus Fernandez's avatar
Jesus Fernandez committed
84 85
        TemporaryTokenReceived,
        Granted
Jesus Fernandez's avatar
Jesus Fernandez committed
86 87
    };

Jesus Fernandez's avatar
Jesus Fernandez committed
88 89 90 91
    enum class Stage {
        RequestingTemporaryCredentials,
        RequestingAuthorization,
        RequestingAccessToken
Jesus Fernandez's avatar
Jesus Fernandez committed
92 93 94 95 96
    };

    enum class Error {
        NoError,
        NetworkError,
Jesus Fernandez's avatar
Jesus Fernandez committed
97 98
        ServerError,

Jesus Fernandez's avatar
Jesus Fernandez committed
99 100
        OAuthTokenNotFoundError,
        OAuthTokenSecretNotFoundError,
Jesus Fernandez's avatar
Jesus Fernandez committed
101
        OAuthCallbackNotVerified
Jesus Fernandez's avatar
Jesus Fernandez committed
102 103
    };

Jesus Fernandez's avatar
Jesus Fernandez committed
104 105 106 107
    typedef std::function<void(Stage, QVariantMap*)> ModifyParametersFunction;

    virtual ~QAbstractOAuth();

Jesus Fernandez's avatar
Jesus Fernandez committed
108 109 110 111 112 113
    virtual QString clientIdentifier() const = 0;
    virtual void setClientIdentifier(const QString &clientIdentifier) = 0;

    virtual QString token() const = 0;
    virtual void setToken(const QString &token) = 0;

114 115 116
    QNetworkAccessManager *networkAccessManager() const;
    void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager);

Jesus Fernandez's avatar
Jesus Fernandez committed
117 118
    Status status() const;

Jesus Fernandez's avatar
Jesus Fernandez committed
119 120 121
    // Resource Owner Authorization: https://tools.ietf.org/html/rfc5849#section-2.2
    QUrl authorizationUrl() const;
    void setAuthorizationUrl(const QUrl &url);
Jesus Fernandez's avatar
Jesus Fernandez committed
122 123 124 125 126 127

    QVariantMap extraTokens() const;

    QAbstractOAuthReplyHandler *replyHandler() const;
    void setReplyHandler(QAbstractOAuthReplyHandler *handler);

Jesus Fernandez's avatar
Jesus Fernandez committed
128 129 130 131 132 133 134 135
    Q_INVOKABLE virtual QNetworkReply *head(const QUrl &url,
                                            const QVariantMap &parameters = QVariantMap()) = 0;
    Q_INVOKABLE virtual QNetworkReply *get(const QUrl &url,
                                           const QVariantMap &parameters = QVariantMap()) = 0;
    Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url,
                                            const QVariantMap &parameters = QVariantMap()) = 0;
    Q_INVOKABLE virtual QNetworkReply *deleteResource(
            const QUrl &url, const QVariantMap &parameters = QVariantMap()) = 0;
Jesus Fernandez's avatar
Jesus Fernandez committed
136

Jesus Fernandez's avatar
Jesus Fernandez committed
137 138 139 140
    ModifyParametersFunction modifyParametersFunction() const;
    void setModifyParametersFunction(const ModifyParametersFunction &modifyParametersFunction);

public Q_SLOTS:
Jesus Fernandez's avatar
Jesus Fernandez committed
141 142 143
    virtual void grant() = 0;

Q_SIGNALS:
Jesus Fernandez's avatar
Jesus Fernandez committed
144 145
    void clientIdentifierChanged(const QString &clientIdentifier);
    void tokenChanged(const QString &token);
Jesus Fernandez's avatar
Jesus Fernandez committed
146
    void statusChanged(Status status);
Jesus Fernandez's avatar
Jesus Fernandez committed
147
    void authorizationUrlChanged(const QUrl &url);
Jesus Fernandez's avatar
Jesus Fernandez committed
148
    void extraTokensChanged(const QVariantMap &tokens);
Jesus Fernandez's avatar
Jesus Fernandez committed
149 150

    void requestFailed(const Error error);
Jesus Fernandez's avatar
Jesus Fernandez committed
151
    void authorizationRequested(const QUrl &url);
Jesus Fernandez's avatar
Jesus Fernandez committed
152
    void granted();
Jesus Fernandez's avatar
Jesus Fernandez committed
153
    void finished(QNetworkReply *reply);
Jesus Fernandez's avatar
Jesus Fernandez committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

protected:
    explicit QAbstractOAuth(QAbstractOAuthPrivate &, QObject *parent = nullptr);

    void setStatus(Status status);

    QString callback() const;

private:
    Q_DISABLE_COPY(QAbstractOAuth)
    Q_DECLARE_PRIVATE(QAbstractOAuth)
};

QT_END_NAMESPACE

#endif // QT_NO_HTTP

#endif // QABSTRACTOAUTH_H