1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QABSTRACTOAUTH_H
5#define QABSTRACTOAUTH_H
6
7#ifndef QT_NO_HTTP
8
9#include <QtNetworkAuth/qoauthglobal.h>
10
11#include <QtCore/qurl.h>
12#include <QtCore/qobject.h>
13#include <QtCore/qstring.h>
14#include <QtCore/qvariant.h>
15#include <QtCore/qmap.h>
16
17#include <functional>
18
19QT_BEGIN_NAMESPACE
20
21class QString;
22class QByteArray;
23class QNetworkReply;
24class QNetworkRequest;
25class QNetworkAccessManager;
26class QAbstractOAuthReplyHandler;
27
28class QAbstractOAuthPrivate;
29class Q_OAUTH_EXPORT QAbstractOAuth : public QObject
30{
31 Q_OBJECT
32
33 Q_ENUMS(Status)
34 Q_ENUMS(Stage)
35 Q_ENUMS(Error)
36 Q_PROPERTY(QString clientIdentifier
37 READ clientIdentifier
38 WRITE setClientIdentifier
39 NOTIFY clientIdentifierChanged)
40 Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged)
41 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
42 Q_PROPERTY(QVariantMap extraTokens READ extraTokens NOTIFY extraTokensChanged)
43 Q_PROPERTY(QUrl authorizationUrl
44 READ authorizationUrl
45 WRITE setAuthorizationUrl
46 NOTIFY authorizationUrlChanged)
47 Q_PROPERTY(QAbstractOAuth::ContentType contentType
48 READ contentType
49 WRITE setContentType
50 NOTIFY contentTypeChanged)
51
52public:
53 enum class Status {
54 NotAuthenticated,
55 TemporaryCredentialsReceived,
56 Granted,
57 RefreshingToken
58 };
59
60 enum class Stage {
61 RequestingTemporaryCredentials,
62 RequestingAuthorization,
63 RequestingAccessToken,
64 RefreshingAccessToken
65 };
66
67 enum class Error {
68 NoError,
69 NetworkError,
70 ServerError,
71
72 OAuthTokenNotFoundError,
73 OAuthTokenSecretNotFoundError,
74 OAuthCallbackNotVerified
75 };
76
77 enum class ContentType {
78 WwwFormUrlEncoded,
79 Json
80 };
81
82 typedef std::function<void(Stage, QMultiMap<QString, QVariant>*)> ModifyParametersFunction;
83
84 virtual ~QAbstractOAuth();
85
86 QString clientIdentifier() const;
87 void setClientIdentifier(const QString &clientIdentifier);
88
89 QString token() const;
90 void setToken(const QString &token);
91
92 QNetworkAccessManager *networkAccessManager() const;
93 void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager);
94
95 Status status() const;
96
97 QUrl authorizationUrl() const;
98 void setAuthorizationUrl(const QUrl &url);
99
100 QVariantMap extraTokens() const;
101
102 QAbstractOAuthReplyHandler *replyHandler() const;
103 void setReplyHandler(QAbstractOAuthReplyHandler *handler);
104
105 Q_INVOKABLE virtual QNetworkReply *head(const QUrl &url,
106 const QVariantMap &parameters = QVariantMap()) = 0;
107 Q_INVOKABLE virtual QNetworkReply *get(const QUrl &url,
108 const QVariantMap &parameters = QVariantMap()) = 0;
109 Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url,
110 const QVariantMap &parameters = QVariantMap()) = 0;
111 Q_INVOKABLE virtual QNetworkReply *put(const QUrl &url,
112 const QVariantMap &parameters = QVariantMap()) = 0;
113 Q_INVOKABLE virtual QNetworkReply *deleteResource(
114 const QUrl &url, const QVariantMap &parameters = QVariantMap()) = 0;
115
116 virtual void prepareRequest(QNetworkRequest *request, const QByteArray &verb,
117 const QByteArray &body = QByteArray()) = 0;
118
119 ModifyParametersFunction modifyParametersFunction() const;
120 void setModifyParametersFunction(const ModifyParametersFunction &modifyParametersFunction);
121
122 ContentType contentType() const;
123 void setContentType(ContentType contentType);
124
125public Q_SLOTS:
126 virtual void grant() = 0;
127
128Q_SIGNALS:
129 void clientIdentifierChanged(const QString &clientIdentifier);
130 void tokenChanged(const QString &token);
131 void statusChanged(Status status);
132 void authorizationUrlChanged(const QUrl &url);
133 void extraTokensChanged(const QVariantMap &tokens);
134 void contentTypeChanged(ContentType contentType);
135
136 void requestFailed(const Error error);
137 void authorizeWithBrowser(const QUrl &url);
138 void granted();
139 void finished(QNetworkReply *reply);
140 void replyDataReceived(const QByteArray &data);
141
142protected:
143 explicit QAbstractOAuth(QAbstractOAuthPrivate &, QObject *parent = nullptr);
144
145 void setStatus(Status status);
146
147 QString callback() const;
148
149 virtual void resourceOwnerAuthorization(const QUrl &url, const QMultiMap<QString, QVariant> &parameters);
150 static QByteArray generateRandomString(quint8 length);
151
152private:
153 Q_DISABLE_COPY(QAbstractOAuth)
154 Q_DECLARE_PRIVATE(QAbstractOAuth)
155};
156
157QT_END_NAMESPACE
158
159#endif // QT_NO_HTTP
160
161#endif // QABSTRACTOAUTH_H
162

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