1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Network Auth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#ifndef QABSTRACTOAUTH_H
31#define QABSTRACTOAUTH_H
32
33#ifndef QT_NO_HTTP
34
35#include <QtNetworkAuth/qoauthglobal.h>
36
37#include <QtCore/qurl.h>
38#include <QtCore/qobject.h>
39#include <QtCore/qstring.h>
40#include <QtCore/qvariant.h>
41
42#include <functional>
43
44QT_BEGIN_NAMESPACE
45
46class QString;
47class QByteArray;
48class QNetworkReply;
49class QNetworkRequest;
50class QNetworkAccessManager;
51class QAbstractOAuthReplyHandler;
52
53class QAbstractOAuthPrivate;
54class Q_OAUTH_EXPORT QAbstractOAuth : public QObject
55{
56 Q_OBJECT
57
58 Q_ENUMS(Status)
59 Q_ENUMS(Stage)
60 Q_ENUMS(Error)
61 Q_PROPERTY(QString clientIdentifier
62 READ clientIdentifier
63 WRITE setClientIdentifier
64 NOTIFY clientIdentifierChanged)
65 Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged)
66 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
67 Q_PROPERTY(QVariantMap extraTokens READ extraTokens NOTIFY extraTokensChanged)
68 Q_PROPERTY(QUrl authorizationUrl
69 READ authorizationUrl
70 WRITE setAuthorizationUrl
71 NOTIFY authorizationUrlChanged)
72 Q_PROPERTY(QAbstractOAuth::ContentType contentType
73 READ contentType
74 WRITE setContentType
75 NOTIFY contentTypeChanged)
76
77public:
78 enum class Status {
79 NotAuthenticated,
80 TemporaryCredentialsReceived,
81 Granted,
82 RefreshingToken
83 };
84
85 enum class Stage {
86 RequestingTemporaryCredentials,
87 RequestingAuthorization,
88 RequestingAccessToken,
89 RefreshingAccessToken
90 };
91
92 enum class Error {
93 NoError,
94 NetworkError,
95 ServerError,
96
97 OAuthTokenNotFoundError,
98 OAuthTokenSecretNotFoundError,
99 OAuthCallbackNotVerified
100 };
101
102 enum class ContentType {
103 WwwFormUrlEncoded,
104 Json
105 };
106
107 typedef std::function<void(Stage, QVariantMap*)> ModifyParametersFunction;
108
109 virtual ~QAbstractOAuth();
110
111 QString clientIdentifier() const;
112 void setClientIdentifier(const QString &clientIdentifier);
113
114 QString token() const;
115 void setToken(const QString &token);
116
117 QNetworkAccessManager *networkAccessManager() const;
118 void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager);
119
120 Status status() const;
121
122 QUrl authorizationUrl() const;
123 void setAuthorizationUrl(const QUrl &url);
124
125 QVariantMap extraTokens() const;
126
127 QAbstractOAuthReplyHandler *replyHandler() const;
128 void setReplyHandler(QAbstractOAuthReplyHandler *handler);
129
130 Q_INVOKABLE virtual QNetworkReply *head(const QUrl &url,
131 const QVariantMap &parameters = QVariantMap()) = 0;
132 Q_INVOKABLE virtual QNetworkReply *get(const QUrl &url,
133 const QVariantMap &parameters = QVariantMap()) = 0;
134 Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url,
135 const QVariantMap &parameters = QVariantMap()) = 0;
136 Q_INVOKABLE virtual QNetworkReply *put(const QUrl &url,
137 const QVariantMap &parameters = QVariantMap()) = 0;
138 Q_INVOKABLE virtual QNetworkReply *deleteResource(
139 const QUrl &url, const QVariantMap &parameters = QVariantMap()) = 0;
140
141 // ### Qt 6: Make this method pure virtual and remove the private implementation
142 void prepareRequest(QNetworkRequest *request,
143 const QByteArray &verb,
144 const QByteArray &body = QByteArray());
145
146 ModifyParametersFunction modifyParametersFunction() const;
147 void setModifyParametersFunction(const ModifyParametersFunction &modifyParametersFunction);
148
149 ContentType contentType() const;
150 void setContentType(ContentType contentType);
151
152public Q_SLOTS:
153 virtual void grant() = 0;
154
155Q_SIGNALS:
156 void clientIdentifierChanged(const QString &clientIdentifier);
157 void tokenChanged(const QString &token);
158 void statusChanged(Status status);
159 void authorizationUrlChanged(const QUrl &url);
160 void extraTokensChanged(const QVariantMap &tokens);
161 void contentTypeChanged(ContentType contentType);
162
163 void requestFailed(const Error error);
164 void authorizeWithBrowser(const QUrl &url);
165 void granted();
166 void finished(QNetworkReply *reply);
167 void replyDataReceived(const QByteArray &data);
168
169protected:
170 explicit QAbstractOAuth(QAbstractOAuthPrivate &, QObject *parent = nullptr);
171
172 void setStatus(Status status);
173
174 QString callback() const;
175
176 virtual void resourceOwnerAuthorization(const QUrl &url, const QVariantMap &parameters);
177 static QByteArray generateRandomString(quint8 length);
178
179private:
180 Q_DISABLE_COPY(QAbstractOAuth)
181 Q_DECLARE_PRIVATE(QAbstractOAuth)
182};
183
184QT_END_NAMESPACE
185
186#endif // QT_NO_HTTP
187
188#endif // QABSTRACTOAUTH_H
189

source code of qtnetworkauth/src/oauth/qabstractoauth.h