// Copyright (C) 2024 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include #include #include #include #ifdef QT_WEBENGINEWIDGETS_LIB #include #include #endif #include #include #ifdef QT_QML_LIB #include #endif #include #include #include #include #include using namespace Qt::StringLiterals; //! [webengine-qml-control] class HttpExample : public QObject { Q_OBJECT #ifdef QT_QML_LIB QML_NAMED_ELEMENT(OAuth2) #endif public: Q_INVOKABLE void authorize(); signals: void authorizationCompleted(bool success); void authorizeWithBrowser(const QUrl &url); //! [webengine-qml-control] public: HttpExample(); void setupSystemBrowser(); void setupSystemBrowserLocalHostHttps(); void setupWebEngineWidgets(); void setupDeviceFlow(); void readOIDCConfiguration(const QUrl &url); void readJSONWebKeySet(const QUrl &url); void readUserInfo(const QUrl &url) const; bool verifyIDToken() const; private: //! [httpserver-variables] QOAuth2AuthorizationCodeFlow m_oauth; QOAuthHttpServerReplyHandler *m_handler = nullptr; //! [httpserver-variables] QNetworkRequestFactory m_api; QRestAccessManager *m_network = nullptr; #ifdef QT_WEBENGINEWIDGETS_LIB QWebEngineView *webView = nullptr; QMainWindow mainWindow; #endif std::optional m_jwks; //! [deviceflow-variables] QOAuth2DeviceAuthorizationFlow m_deviceFlow; //! [deviceflow-variables] //! [oidc-id-token-struct] struct IDToken { QJsonObject header; QJsonObject payload; QByteArray signature; }; //! [oidc-id-token-struct] std::optional m_oidcConfig; //! [oidc-id-token-parser-declaration] std::optional parseIDToken(const QString &token) const; //! [oidc-id-token-parser-declaration] }; class UriSchemeExample : public QObject { Q_OBJECT public: UriSchemeExample(); void setupSystemBrowserCustom(); void setupWebEngineWidgetsCustom(); void setupWebEngineWidgetsHttps(); private: //! [uri-variables] QOAuth2AuthorizationCodeFlow m_oauth; QOAuthUriSchemeReplyHandler m_handler; //! [uri-variables] QNetworkRequestFactory m_api; #ifdef QT_WEBENGINEWIDGETS_LIB //! [webengine-widget-variables] QWebEngineView *webView = nullptr; QMainWindow mainWindow; //! [webengine-widget-variables] #endif };