| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QABSTRACTOAUTH2_H |
| 5 | #define QABSTRACTOAUTH2_H |
| 6 | |
| 7 | #include <QtNetworkAuth/qoauthglobal.h> |
| 8 | |
| 9 | #ifndef QT_NO_HTTP |
| 10 | |
| 11 | #include <QtCore/qcontainerfwd.h> |
| 12 | #include <QtCore/qdatetime.h> |
| 13 | |
| 14 | #include <QtNetworkAuth/qabstractoauth.h> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | #if !QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 13) |
| 19 | # define QOAUTH2_NO_LEGACY_SCOPE |
| 20 | #endif |
| 21 | |
| 22 | class QSslConfiguration; |
| 23 | class QHttpMultiPart; |
| 24 | class QAbstractOAuth2Private; |
| 25 | class Q_OAUTH_EXPORT QAbstractOAuth2 : public QAbstractOAuth |
| 26 | { |
| 27 | Q_OBJECT |
| 28 | #ifndef QOAUTH2_NO_LEGACY_SCOPE |
| 29 | Q_PROPERTY(QString scope READ scope WRITE setScope NOTIFY scopeChanged) |
| 30 | #endif |
| 31 | Q_PROPERTY(QSet<QByteArray> grantedScopeTokens |
| 32 | READ grantedScopeTokens NOTIFY grantedScopeTokensChanged) |
| 33 | Q_PROPERTY(QSet<QByteArray> requestedScopeTokens |
| 34 | READ requestedScopeTokens |
| 35 | WRITE setRequestedScopeTokens |
| 36 | NOTIFY requestedScopeTokensChanged) |
| 37 | Q_PROPERTY(QString userAgent READ userAgent WRITE setUserAgent NOTIFY userAgentChanged) |
| 38 | Q_PROPERTY(QString clientIdentifierSharedKey |
| 39 | READ clientIdentifierSharedKey |
| 40 | WRITE setClientIdentifierSharedKey |
| 41 | NOTIFY clientIdentifierSharedKeyChanged) |
| 42 | Q_PROPERTY(QString state READ state WRITE setState NOTIFY stateChanged) |
| 43 | Q_PROPERTY(QDateTime expiration READ expirationAt NOTIFY expirationAtChanged) |
| 44 | Q_PROPERTY(QString refreshToken |
| 45 | READ refreshToken |
| 46 | WRITE setRefreshToken |
| 47 | NOTIFY refreshTokenChanged) |
| 48 | Q_PROPERTY(std::chrono::seconds refreshLeadTime |
| 49 | READ refreshLeadTime |
| 50 | WRITE setRefreshLeadTime |
| 51 | NOTIFY refreshLeadTimeChanged) |
| 52 | Q_PROPERTY(bool autoRefresh |
| 53 | READ autoRefresh |
| 54 | WRITE setAutoRefresh |
| 55 | NOTIFY autoRefreshChanged) |
| 56 | Q_PROPERTY(NonceMode nonceMode READ nonceMode WRITE setNonceMode NOTIFY nonceModeChanged) |
| 57 | Q_PROPERTY(QString nonce READ nonce WRITE setNonce NOTIFY nonceChanged) |
| 58 | Q_PROPERTY(QString idToken READ idToken NOTIFY idTokenChanged) |
| 59 | Q_PROPERTY(QUrl tokenUrl READ tokenUrl WRITE setTokenUrl NOTIFY tokenUrlChanged) |
| 60 | |
| 61 | using NetworkRequestModifierPrototype = void(*)(QNetworkRequest&, QAbstractOAuth::Stage); |
| 62 | template <typename Functor> |
| 63 | using ContextTypeForFunctor = typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType; |
| 64 | template <typename Functor> |
| 65 | using if_compatible_callback = std::enable_if_t< |
| 66 | QtPrivate::AreFunctionsCompatible<NetworkRequestModifierPrototype, Functor>::value, bool>; |
| 67 | |
| 68 | public: |
| 69 | enum class NonceMode : quint8 { |
| 70 | Automatic, |
| 71 | Enabled, |
| 72 | Disabled, |
| 73 | }; |
| 74 | Q_ENUM(NonceMode) |
| 75 | |
| 76 | explicit QAbstractOAuth2(QObject *parent = nullptr); |
| 77 | explicit QAbstractOAuth2(QNetworkAccessManager *manager, QObject *parent = nullptr); |
| 78 | ~QAbstractOAuth2(); |
| 79 | |
| 80 | Q_INVOKABLE virtual QUrl createAuthenticatedUrl(const QUrl &url, |
| 81 | const QVariantMap ¶meters = QVariantMap()); |
| 82 | |
| 83 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 84 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 85 | Q_INVOKABLE QNetworkReply *(const QUrl &url, |
| 86 | const QVariantMap ¶meters = QVariantMap()) override; |
| 87 | |
| 88 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 89 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 90 | Q_INVOKABLE QNetworkReply *get(const QUrl &url, |
| 91 | const QVariantMap ¶meters = QVariantMap()) override; |
| 92 | |
| 93 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 94 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 95 | Q_INVOKABLE QNetworkReply *post(const QUrl &url, |
| 96 | const QVariantMap ¶meters = QVariantMap()) override; |
| 97 | |
| 98 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 99 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 100 | Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url, const QByteArray &data); |
| 101 | |
| 102 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 103 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 104 | Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url, QHttpMultiPart *multiPart); |
| 105 | |
| 106 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 107 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 108 | Q_INVOKABLE QNetworkReply *put(const QUrl &url, |
| 109 | const QVariantMap ¶meters = QVariantMap()) override; |
| 110 | |
| 111 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 112 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 113 | Q_INVOKABLE virtual QNetworkReply *put(const QUrl &url, const QByteArray &data); |
| 114 | |
| 115 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 116 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 117 | Q_INVOKABLE virtual QNetworkReply *put(const QUrl &url, QHttpMultiPart *multiPart); |
| 118 | |
| 119 | QT_DEPRECATED_VERSION_X_6_11("Use QtNetwork classes instead." |
| 120 | "See https://doc.qt.io/qt-6/oauth-http-method-alternatives.html" ) |
| 121 | Q_INVOKABLE QNetworkReply *deleteResource(const QUrl &url, |
| 122 | const QVariantMap ¶meters = QVariantMap()) override; |
| 123 | #ifndef QOAUTH2_NO_LEGACY_SCOPE |
| 124 | QT_DEPRECATED_VERSION_X_6_13("Use requestedScopeTokens and grantedScopeTokens properties instead." ) |
| 125 | QString scope() const; |
| 126 | QT_DEPRECATED_VERSION_X_6_13("Use requestedScopeTokens and grantedScopeTokens properties instead." ) |
| 127 | void setScope(const QString &scope); |
| 128 | #endif |
| 129 | |
| 130 | QSet<QByteArray> grantedScopeTokens() const; |
| 131 | |
| 132 | QSet<QByteArray> requestedScopeTokens() const; |
| 133 | void setRequestedScopeTokens(const QSet<QByteArray> &tokens); |
| 134 | |
| 135 | QString userAgent() const; |
| 136 | void setUserAgent(const QString &userAgent); |
| 137 | |
| 138 | QString responseType() const; |
| 139 | |
| 140 | QString clientIdentifierSharedKey() const; |
| 141 | void setClientIdentifierSharedKey(const QString &clientIdentifierSharedKey); |
| 142 | |
| 143 | QString state() const; |
| 144 | void setState(const QString &state); |
| 145 | |
| 146 | QDateTime expirationAt() const; |
| 147 | |
| 148 | QString refreshToken() const; |
| 149 | void setRefreshToken(const QString &refreshToken); |
| 150 | |
| 151 | std::chrono::seconds refreshLeadTime() const; |
| 152 | void setRefreshLeadTime(std::chrono::seconds leadTime); |
| 153 | |
| 154 | bool autoRefresh() const; |
| 155 | void setAutoRefresh(bool enable); |
| 156 | |
| 157 | NonceMode nonceMode() const; |
| 158 | void setNonceMode(NonceMode mode); |
| 159 | |
| 160 | QString nonce() const; |
| 161 | void setNonce(const QString &nonce); |
| 162 | |
| 163 | QString idToken() const; |
| 164 | |
| 165 | QUrl tokenUrl() const; |
| 166 | void setTokenUrl(const QUrl &tokenUrl); |
| 167 | |
| 168 | #ifndef QT_NO_SSL |
| 169 | QSslConfiguration sslConfiguration() const; |
| 170 | void setSslConfiguration(const QSslConfiguration &configuration); |
| 171 | #endif |
| 172 | |
| 173 | void prepareRequest(QNetworkRequest *request, const QByteArray &verb, |
| 174 | const QByteArray &body = QByteArray()) override; |
| 175 | |
| 176 | template <typename Functor, if_compatible_callback<Functor> = true> |
| 177 | void setNetworkRequestModifier(const ContextTypeForFunctor<Functor> *context, |
| 178 | Functor &&callback) { |
| 179 | setNetworkRequestModifierImpl( |
| 180 | context, |
| 181 | slot: QtPrivate::makeCallableObject<NetworkRequestModifierPrototype>( |
| 182 | std::forward<Functor>(callback))); |
| 183 | } |
| 184 | void clearNetworkRequestModifier(); |
| 185 | |
| 186 | protected Q_SLOTS: |
| 187 | QT7_ONLY(virtual) void refreshTokensImplementation() QT7_ONLY(= 0); |
| 188 | |
| 189 | public Q_SLOTS: |
| 190 | void refreshTokens(); |
| 191 | |
| 192 | Q_SIGNALS: |
| 193 | #ifndef QOAUTH2_NO_LEGACY_SCOPE |
| 194 | QT_MOC_COMPAT |
| 195 | QT_DEPRECATED_VERSION_X_6_13("Use requestedScopeTokens and grantedScopeTokens properties instead." ) |
| 196 | void scopeChanged(const QString &scope); |
| 197 | #endif |
| 198 | void grantedScopeTokensChanged(const QSet<QByteArray> &tokens); |
| 199 | void requestedScopeTokensChanged(const QSet<QByteArray> &tokens); |
| 200 | void userAgentChanged(const QString &userAgent); |
| 201 | void responseTypeChanged(const QString &responseType); |
| 202 | void clientIdentifierSharedKeyChanged(const QString &clientIdentifierSharedKey); |
| 203 | void stateChanged(const QString &state); |
| 204 | void expirationAtChanged(const QDateTime &expiration); |
| 205 | void refreshTokenChanged(const QString &refreshToken); |
| 206 | void accessTokenAboutToExpire(); |
| 207 | void refreshLeadTimeChanged(std::chrono::seconds leadTime); |
| 208 | void autoRefreshChanged(bool enable); |
| 209 | void nonceModeChanged(NonceMode mode); |
| 210 | void nonceChanged(const QString &nonce); |
| 211 | void idTokenChanged(const QString &idToken); |
| 212 | void tokenUrlChanged(const QUrl &tokenUrl); |
| 213 | #ifndef QT_NO_SSL |
| 214 | void sslConfigurationChanged(const QSslConfiguration &configuration); |
| 215 | #endif |
| 216 | |
| 217 | #if QT_DEPRECATED_SINCE(6, 13) |
| 218 | QT_MOC_COMPAT |
| 219 | QT_DEPRECATED_VERSION_X_6_13("Use serverReportedErrorOccurred instead." ) |
| 220 | void error(const QString &error, const QString &errorDescription, const QUrl &uri); |
| 221 | #endif |
| 222 | void serverReportedErrorOccurred(const QString &error, const QString &errorDescription, |
| 223 | const QUrl &uri); |
| 224 | void authorizationCallbackReceived(const QVariantMap &data); |
| 225 | |
| 226 | protected: |
| 227 | explicit QAbstractOAuth2(QAbstractOAuth2Private &, QObject *parent = nullptr); |
| 228 | |
| 229 | void setResponseType(const QString &responseType); |
| 230 | |
| 231 | private: |
| 232 | void setNetworkRequestModifierImpl(const QObject* context, QtPrivate::QSlotObjectBase *slot); |
| 233 | Q_DECLARE_PRIVATE(QAbstractOAuth2) |
| 234 | }; |
| 235 | |
| 236 | QT_END_NAMESPACE |
| 237 | |
| 238 | #endif // QT_NO_HTTP |
| 239 | |
| 240 | #endif // QABSTRACTOAUTH2_H |
| 241 | |