QOAuthHttpServerReplyHandler Class
ローカル HTTP サーバを設定することで、ループバック・リダイレクトを処理します。詳細...
ヘッダ | #include <QOAuthHttpServerReplyHandler> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS NetworkAuth) target_link_libraries(mytarget PRIVATE Qt6::NetworkAuth) |
qmake: | QT += networkauth |
を継承する: | QOAuthOobReplyHandler |
パブリック関数
QOAuthHttpServerReplyHandler(QObject *parent = nullptr) | |
QOAuthHttpServerReplyHandler(quint16 port, QObject *parent = nullptr) | |
QOAuthHttpServerReplyHandler(const QHostAddress &address, quint16 port, QObject *parent = nullptr) | |
virtual | ~QOAuthHttpServerReplyHandler() |
QString | callbackPath() const |
QString | callbackText() const |
void | close() |
bool | isListening() const |
bool | listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) |
quint16 | port() const |
void | setCallbackPath(const QString &path) |
void | setCallbackText(const QString &text) |
詳細な説明
このクラスは、ループバックリダイレクトを使用するOAuth 2.0認証処理用のリプライハンドラです。
リダイレクト URIは、認可サーバーがユーザーエージェント (通常はシステムブラウザ) をリダイレクトする場所です。ループバックリダイレクト URI はスキームとしてhttp
を使用し、ホストとしてlocalhostまたは IP アドレスリテラル (IPv4 and IPv6 を参照)を使用します。
QOAuthHttpServerReplyHandlerはlocalhostサーバーを設定する。認可サーバーがブラウザをこのlocalhostアドレスにリダイレクトすると、リプライハンドラーはリダイレクトURIのクエリーパラメータを解析し、a signal で認可完了のシグナルを送る。
他のリダイレクトURIスキームを扱うには、QOAuthUriSchemeReplyHandler を参照のこと。
次のコードは使い方を示している。まず、必要な変数:
QOAuth2AuthorizationCodeFlow m_oauth; QOAuthHttpServerReplyHandler *m_handler = nullptr;
続いてOAuthのセットアップ(簡潔のためエラー処理は省略):
m_oauth.setAuthorizationUrl(QUrl("https://some.authorization.service/v3/authorize"_L1)); m_oauth.setAccessTokenUrl(QUrl("https://some.authorization.service/v3/access_token"_L1)); m_oauth.setClientIdentifier("a_client_id"_L1); m_oauth.setScope("read"_L1); m_handler = new QOAuthHttpServerReplyHandler(1234, this); connect(&m_oauth, &QAbstractOAuth::authorizeWithBrowser, this, &QDesktopServices::openUrl); connect(&m_oauth, &QAbstractOAuth::granted, this, [this]() { // Here we use QNetworkRequestFactory to store the access token m_api.setBearerToken(m_oauth.token().toLatin1()); m_handler->close(); });
最後に、URIスキームのreply-handlerをセットアップする:
m_oauth.setReplyHandler(m_handler); // Initiate the authorization if (m_handler->isListening()) { m_oauth.grant(); }
IPv4とIPv6
ハンドラーが任意のアドレスのハンドラー(AnyIPv4, AnyIPv6, or Any)の場合、使用されるコールバックはhttp://localhost:{port}/{path}
の形式である。ハンドラーは最初にIPv4のループバックアドレスをリッスンし、次にIPv6をリッスンしようとする。localhost
が使用されるのは、IPv4とIPv6の両方のインターフェースで正しく解決されるからである。
ループバックアドレス(LocalHost or LocalHostIPv6)に対しては、IPリテラル(127.0.0.1
と::1
)が使用される。
例えば、IPv4アドレスの場合、http://192.168.0.123:{port}/{path}のように。
メンバー関数ドキュメント
[explicit]
QOAuthHttpServerReplyHandler::QOAuthHttpServerReplyHandler(QObject *parent = nullptr)
parent を親オブジェクトとして QOAuthHttpServerReplyHandler オブジェクトを構築する。ポート0
およびアドレスLocalHost を指定してlisten() をコールします。
listen()も参照 。
[explicit]
QOAuthHttpServerReplyHandler::QOAuthHttpServerReplyHandler(quint16 port, QObject *parent = nullptr)
parent を親オブジェクトとして QOAuthHttpServerReplyHandler オブジェクトを構築する。port とアドレスLocalHost を使用してlisten() を呼び出します。
listen()も参照してください 。
[explicit]
QOAuthHttpServerReplyHandler::QOAuthHttpServerReplyHandler(const QHostAddress &address, quint16 port, QObject *parent = nullptr)
parent を親オブジェクトとして QOAuthHttpServerReplyHandler オブジェクトを構築する。address およびport を使用してlisten() をコールします。
listen()も参照 。
[virtual noexcept]
QOAuthHttpServerReplyHandler::~QOAuthHttpServerReplyHandler()
QOAuthHttpServerReplyHandler オブジェクトを破棄する。接続/リダイレクションのリッスンを停止します。
close()も参照 。
QString QOAuthHttpServerReplyHandler::callbackPath() const
callback() /OAuth2 redirect_uri パラメータの path コンポーネントとして使用するパスを返します。
setCallbackPath()も参照ください 。
QString QOAuthHttpServerReplyHandler::callbackText() const
認証ステージの最後に、リダイレクトに応答するテキストを返します。
このテキストはシンプルな HTML ページでラップされ、 リダイレクトを行ったブラウザ/ユーザーエージェントによってユーザーに表示されます。
デフォルトのテキストは
Callback received. Feel free to close this page.
setCallbackText()も参照 。
void QOAuthHttpServerReplyHandler::close()
このハンドラに、接続やリダイレクションのリッスンを停止するように指示する。
listen()も参照ください 。
bool QOAuthHttpServerReplyHandler::isListening() const
このハンドラが現在リッスン中であればtrue
を返し、そうでなければfalse
を返す。
bool QOAuthHttpServerReplyHandler::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
このハンドラに、address とport の着信コネクション/リダイレクションをリッスンするよう指示する。リスニングに成功した場合はtrue
を返し、失敗した場合はfalse
を返す。
アクティブリスニングが必要なのは、最初の認可フェーズを実行するときだけである。通常はQOAuth2AuthorizationCodeFlow::grant() のコールによって開始される。
認証に成功したら、リスナーをクローズすることを推奨する。リスニングは、requesting access tokens やリフレッシュには必要ない。
この関数がNull をaddress として呼び出された場合、ハンドラはLocalHost のリスニングを試み、失敗した場合はLocalHostIPv6 のリスニングを試みる。
IPv4 and IPv6 も参照のこと。
close()、isListening()、QTcpServer::listen()も 参照のこと。
quint16 QOAuthHttpServerReplyHandler::port() const
このハンドラがリッスンしているポートを返す。
listen() およびisListening()も参照 。
void QOAuthHttpServerReplyHandler::setCallbackPath(const QString &path)
callback() のパス・コンポーネントとして使用するpath を設定する。
callbackPath()も参照 。
void QOAuthHttpServerReplyHandler::setCallbackText(const QString &text)
認証ステージの最後にリダイレクトに応答する際に使用するtext を設定する。
callbackText()も参照のこと 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.