<application>libpq</application> implements support for the OAuth v2 Device Authorization client flow, documented in <ulink url="https://datatracker.ietf.org/doc/html/rfc8628">RFC 8628</ulink>, as an optional module. See the <link linkend="configure-option-with-libcurl"> installation documentation</link> for information on how to enable support for Device Authorization as a builtin flow. libpqは、RFC 8628に記載されているOAuth v2デバイス認証クライアントフローのサポートを、オプションのモジュールとして実装しています。 組み込みフローとしてデバイス認証のサポートを有効にする方法については、インストール文書を参照してください。
When support is enabled and the optional module installed, <application>libpq</application> will use the builtin flow by default if the server <link linkend="auth-oauth">requests a bearer token</link> during authentication. This flow can be utilized even if the system running the client application does not have a usable web browser, for example when running a client via <acronym>SSH</acronym>. サポートが有効化されておりオプションのモジュールがインストールされている場合、認証中にサーバがベアラトークンを要求すると、libpqはデフォルトで組み込みフローを使用します。 このフローは、たとえばSSH経由でクライアントを実行している場合など、クライアントアプリケーションを実行しているシステムに使用可能なWebブラウザがない場合でも利用できます。
The builtin flow will, by default, print a URL to visit and a user code to enter there: 組み込みフローでは、デフォルトでアクセス先のURLとそこで入力するユーザコードを表示します。
$ psql 'dbname=postgres oauth_issuer=https://example.com oauth_client_id=...' Visit https://example.com/device and enter the code: ABCD-EFGH
(This prompt may be <link linkend="libpq-oauth-authdata-prompt-oauth-device">customized</link>.) The user will then log into their OAuth provider, which will ask whether to allow libpq and the server to perform actions on their behalf. It is always a good idea to carefully review the URL and permissions displayed, to ensure they match expectations, before continuing. Permissions should not be given to untrusted third parties. (このプロンプトはカスタマイズされているかもしれません。) ユーザがOAuthプロバイダにログインすると、OAuthプロバイダはlibpqとサーバがユーザの代わりにアクションを実行することを許可するかどうかを尋ねます。 続行する前に、表示されたURLと権限を注意深く確認し、期待通りであることを確認することをお勧めします。 信頼できない第三者に許可を与えてはいけません。
Client applications may implement their own flows to customize interaction and integration with applications. See <xref linkend="libpq-oauth-authdata-hooks"/> for more information on how add a custom flow to <application>libpq</application>. クライアントアプリケーションは、独自のフローを実装してアプリケーションとの対話や統合をカスタマイズできます。 libpqに独自のフローを追加する方法の詳細については、32.20.1を参照してください。
For an OAuth client flow to be usable, the connection string must at minimum contain <xref linkend="libpq-connect-oauth-issuer"/> and <xref linkend="libpq-connect-oauth-client-id"/>. (These settings are determined by your organization's OAuth provider.) The builtin flow additionally requires the OAuth authorization server to publish a device authorization endpoint. OAuthクライアントフローを使用できるようにするには、接続文字列に少なくともoauth_issuerとoauth_client_idが含まれている必要があります。 (これらの設定は、利用者の組織のOAuthプロバイダによって決定されます。) さらに組み込みフローでは、OAuth認証サーバがデバイス認証エンドポイントを公開する必要があります。
The builtin Device Authorization flow is not currently supported on Windows. Custom client flows may still be implemented. 組み込みデバイス認証フローは、現在Windowsではサポートされていません。 カスタムクライアントフローは引き続き実装できます。
The behavior of the OAuth flow may be modified or replaced by a client using the following hook API: OAuthフローの動作は、次のフックAPIを使用するクライアントにより変更または置き換えることができます。
PQsetAuthDataHook #
Sets the <symbol>PGauthDataHook</symbol>, overriding
<application>libpq</application>'s handling of one or more aspects of
its OAuth client flow.
PGauthDataHookを設定し、libpqによるOAuthクライアントフローの1つ以上の処理を上書きします。
void PQsetAuthDataHook(PQauthDataHook_type hook);
If <replaceable>hook</replaceable> is <literal>NULL</literal>, the
default handler will be reinstalled. Otherwise, the application passes
a pointer to a callback function with the signature:
hookがNULLの場合、デフォルトのハンドラが再インストールされます。
それ以外の場合、アプリケーションは次のシグネチャを持つコールバック関数へのポインタを渡します。
int hook_fn(PGauthData type, PGconn *conn, void *data);
which <application>libpq</application> will call when an action is
required of the application. <replaceable>type</replaceable> describes
the request being made, <replaceable>conn</replaceable> is the
connection handle being authenticated, and <replaceable>data</replaceable>
points to request-specific metadata. The contents of this pointer are
determined by <replaceable>type</replaceable>; see
<xref linkend="libpq-oauth-authdata-hooks-types"/> for the supported
list.
これは、アプリケーションでアクションが必要なときにlibpqが呼び出すものです。
typeはリクエストの種類を示し、connは認証中の接続ハンドルを示し、dataはリクエスト固有のメタデータを指します。
このポインタの内容はtypeによって決まります。
サポートされている一覧については32.20.1.1を参照してください。
Hooks can be chained together to allow cooperative and/or fallback
behavior. In general, a hook implementation should examine the incoming
<replaceable>type</replaceable> (and, potentially, the request metadata
and/or the settings for the particular <replaceable>conn</replaceable>
in use) to decide whether or not to handle a specific piece of authdata.
If not, it should delegate to the previous hook in the chain
(retrievable via <function>PQgetAuthDataHook</function>).
フックをつなぎ合わせることで、連携動作や代替動作を実現できます。
一般的に、フックの実装では、入力のtype(および、場合によってはリクエストのメタデータや使用中のconnの設定)を調べて、特定の認証データを処理するかどうかを判断する必要があります。
処理しない場合は、チェーン内の前のフック(PQgetAuthDataHook経由で取得可能)に委任する必要があります。
Success is indicated by returning an integer greater than zero. Returning a negative integer signals an error condition and abandons the connection attempt. (A zero value is reserved for the default implementation.) 0より大きい整数を返すと、成功していることを示します。 負の整数を返すとエラー状態であることを示しており、接続試行が中止されます。 (デフォルトの実装では0の値は予約されています。)
PQgetAuthDataHook #
Retrieves the current value of <symbol>PGauthDataHook</symbol>.
現在のPGauthDataHookの値を取得します。
PQauthDataHook_type PQgetAuthDataHook(void);
At initialization time (before the first call to
<function>PQsetAuthDataHook</function>), this function will return
<symbol>PQdefaultAuthDataHook</symbol>.
初期化時(PQsetAuthDataHookを最初に呼び出す前)には、この関数はPQdefaultAuthDataHookを返します。
The following <symbol>PGauthData</symbol> types and their corresponding
<replaceable>data</replaceable> structures are defined:
次のPGauthData型とそれに対応するdata構造体が定義されています。
PQAUTHDATA_PROMPT_OAUTH_DEVICE
#
Replaces the default user prompt during the builtin device
authorization client flow. <replaceable>data</replaceable> points to
an instance of <symbol>PGpromptOAuthDevice</symbol>:
組み込みデバイス認証クライアントフロー中の、デフォルトのユーザプロンプトを置き換えます。
dataはPGpromptOAuthDeviceのインスタンスを指しています。
typedef struct _PGpromptOAuthDevice
{
const char *verification_uri; /* verification URI to visit */
const char *user_code; /* user code to enter */
const char *verification_uri_complete; /* optional combination of URI and
* code, or NULL */
int expires_in; /* seconds until user code expires */
const char *verification_uri; /* アクセスする検証URI */
const char *user_code; /* 入力するユーザコード */
const char *verification_uri_complete; /* URIとコードのオプションの組み合わせ、
* またはNULL */
int expires_in; /* ユーザコードの有効期限が切れるまでの秒数 */
} PGpromptOAuthDevice;
The OAuth Device Authorization flow which
<link linkend="configure-option-with-libcurl">can be included</link>
in <application>libpq</application>
requires the end user to visit a URL with a browser, then enter a code
which permits <application>libpq</application> to connect to the server
on their behalf. The default prompt simply prints the
<literal>verification_uri</literal> and <literal>user_code</literal>
on standard error. Replacement implementations may display this
information using any preferred method, for example with a GUI.
libpqに含めることができるOAuthデバイス認証フローでは、エンドユーザがブラウザでURLにアクセスし、libpqがユーザに代わってサーバに接続することを許可するコードを入力する必要があります。
デフォルトのプロンプトは、標準エラーにverification_uriとuser_codeを出力するだけです。
代替の実装では、この情報をGUIなど好みの方法を使用して表示できます。
This callback is only invoked during the builtin device authorization flow. If the application installs a <link linkend="libpq-oauth-authdata-oauth-bearer-token">custom OAuth flow</link>, or <application>libpq</application> was not built with support for the builtin flow, this authdata type will not be used. このコールバックは、組み込みデバイス認証フロー中にのみ呼び出されます。 アプリケーションがカスタムOAuthフローをインストールしている場合や、libpqが組み込みフローをサポートするようにビルドされていない場合、この認証データ型は使用されません。
If a non-NULL <structfield>verification_uri_complete</structfield> is
provided, it may optionally be used for non-textual verification (for
example, by displaying a QR code). The URL and user code should still
be displayed to the end user in this case, because the code will be
manually confirmed by the provider, and the URL lets users continue
even if they can't use the non-textual method. For more information,
see section 3.3.1 in
<ulink url="https://datatracker.ietf.org/doc/html/rfc8628#section-3.3.1">RFC 8628</ulink>.
非NULLのverification_uri_completeが提供されている場合は、テキスト以外の検証(QRコードの表示など)にも使用できます。
この場合でも、URLとユーザコードは引き続きエンドユーザに表示する必要があります。
これは、そのコードがプロバイダによって手動で確認されるため、またユーザがテキスト以外の方法を使用できない場合でもURLを使用して続行できるようにするためです。
詳細については、RFC 8628のセクション3.3.1を参照してください。
PQAUTHDATA_OAUTH_BEARER_TOKEN
#Adds a custom implementation of a flow, replacing the builtin flow if it is <link linkend="configure-option-with-libcurl">installed</link>. The hook should either directly return a Bearer token for the current user/issuer/scope combination, if one is available without blocking, or else set up an asynchronous callback to retrieve one. 独自実装したフローを追加し、組み込みフローがインストールされている場合はそれを置き換えます。 フックは、ブロッキングなしで使用可能な場合は現在のユーザ/発行者/スコープの組み合わせのベアラトークンを直接返すか、それ以外の場合は非同期コールバックを設定してベアラトークンを取得する必要があります。
<replaceable>data</replaceable> points to an instance
of <symbol>PGoauthBearerRequest</symbol>, which should be filled in
by the implementation:
dataはPGoauthBearerRequestのインスタンスを指しています。
このインスタンスは実装側で入力する必要があります。
typedef struct PGoauthBearerRequest
{
/* Hook inputs (constant across all calls) */
const char *openid_configuration; /* OIDC discovery URL */
const char *scope; /* required scope(s), or NULL */
/* フックの入力(全ての呼び出しで一定) */
const char *openid_configuration; /* OIDCディスカバリーURL */
const char *scope; /* 必要なスコープ、またはNULL */
/* Hook outputs */
/* フックの出力 */
/*
* Callback implementing a custom asynchronous OAuth flow. The signature is
* platform-dependent: PQ_SOCKTYPE is SOCKET on Windows, and int everywhere
* else.
* カスタム非同期OAuthフローを実装するコールバック。シグネチャはプラット
* フォーム依存です。WindowsではPQ_SOCKTYPEはSOCKETで、その他のすべての場所
* ではintです。
*/
PostgresPollingStatusType (*async) (PGconn *conn,
struct PGoauthBearerRequest *request,
PQ_SOCKTYPE *altsock);
/* Callback to clean up custom allocations. */
/* カスタム割り当てをクリーンアップするためのコールバック。 */
void (*cleanup) (PGconn *conn, struct PGoauthBearerRequest *request);
char *token; /* acquired Bearer token */
void *user; /* hook-defined allocated data */
char *token; /* 取得したベアラトークン */
void *user; /* フックで定義される割り当てデータ */
} PGoauthBearerRequest;
Two pieces of information are provided to the hook by
<application>libpq</application>:
<replaceable>openid_configuration</replaceable> contains the URL of an
OAuth discovery document describing the authorization server's
supported flows, and <replaceable>scope</replaceable> contains a
(possibly empty) space-separated list of OAuth scopes which are
required to access the server. Either or both may be
<literal>NULL</literal> to indicate that the information was not
discoverable. (In this case, implementations may be able to establish
the requirements using some other preconfigured knowledge, or they may
choose to fail.)
libpqでは、このフックで2つの情報を提供しています。
openid_configurationには、認証サーバがサポートするフローを説明するOAuthディスカバリー文書のURLが含まれ、scopeには、サーバへのアクセスに必要なOAuthスコープの一覧(空の場合もあります)がスペースで区切られて含まれています。
どちらか一方または両方がNULLの場合は、情報が検出できなかったことを示します。
(この場合、実装側で事前に設定された他の知識を使用して要件を確立できる場合もあれば、失敗を選択する場合もあります。)
The final output of the hook is <replaceable>token</replaceable>, which
must point to a valid Bearer token for use on the connection. (This
token should be issued by the
<xref linkend="libpq-connect-oauth-issuer"/> and hold the requested
scopes, or the connection will be rejected by the server's validator
module.) The allocated token string must remain valid until
<application>libpq</application> is finished connecting; the hook
should set a <replaceable>cleanup</replaceable> callback which will be
called when <application>libpq</application> no longer requires it.
フックの最終出力はtokenであり、これは接続で使用できる有効なベアラトークンを指している必要があります。
(このトークンはoauth_issuerによって発行され、要求されたスコープを保持する必要があります。
そうしないと、サーバの検証モジュールによって接続が拒否されます。)
割り当てられたトークン文字列は、libpqが接続を終了するまで有効なままでなければなりません。
フックは、libpqが接続を必要としなくなったときに呼び出されるcleanupコールバックを設定する必要があります。
If an implementation cannot immediately produce a
<replaceable>token</replaceable> during the initial call to the hook,
it should set the <replaceable>async</replaceable> callback to handle
nonblocking communication with the authorization server.
実装上フックの最初の呼び出し中にすぐにtokenを生成できない場合は、認証サーバとの非ブロッキング通信を処理するように非同期コールバックを設定する必要があります。
[16]
This will be called to begin the flow immediately upon return from the
hook. When the callback cannot make further progress without blocking,
it should return either <symbol>PGRES_POLLING_READING</symbol> or
<symbol>PGRES_POLLING_WRITING</symbol> after setting
<literal>*altsock</literal> to the file descriptor that will be marked
ready to read/write when progress can be made again. (This descriptor
is then provided to the top-level polling loop via
<function>PQsocket()</function>.) Return <symbol>PGRES_POLLING_OK</symbol>
after setting <replaceable>token</replaceable> when the flow is
complete, or <symbol>PGRES_POLLING_FAILED</symbol> to indicate failure.
これは、フックから戻るとすぐにフローを開始するために呼び出されます。
コールバックがブロッキングなしでこれ以上先に進めない場合は、*altsockに、処理が再開できるようになったときに読み取り/書き込みの準備完了とマークされるファイルディスクリプタを設定した後、PGRES_POLLING_READINGまたはPGRES_POLLING_WRITINGのいずれかを返す必要があります。
(その後、このディスクリプタはPQsocket()を介して最上位のポーリングループに提供されます。)
フローが完了したらtokenを設定した後にPGRES_POLLING_OKを返し、失敗した場合はPGRES_POLLING_FAILEDを返します。
Implementations may wish to store additional data for bookkeeping
across calls to the <replaceable>async</replaceable> and
<replaceable>cleanup</replaceable> callbacks. The
<replaceable>user</replaceable> pointer is provided for this purpose;
<application>libpq</application> will not touch its contents and the
application may use it at its convenience. (Remember to free any
allocations during token cleanup.)
実装によっては、asyncコールバックとcleanupコールバックの呼び出しをまたぐ情報に対し、追加のデータを格納したい場合があります。
userポインタはこの目的のために提供されています。
libpqはその内容に触れることはなく、アプリケーションは都合の良いときにこの追加のデータを使用できます。
(トークンのクリーンアップ中に割り当てを解放することを忘れないでください。)
A "dangerous debugging mode" may be enabled by setting the environment
variable <envar>PGOAUTHDEBUG=UNSAFE</envar>. This functionality is provided
for ease of local development and testing only. It does several things that
you will not want a production system to do:
環境変数PGOAUTHDEBUG=UNSAFEを設定することで、「危険なデバッグモード」を有効にできます。
この機能は、ローカルでの開発とテストを容易にするためにのみ提供されています。
この機能は、本番システムでは実行したくないいくつかのことを実行します。
permits the use of unencrypted HTTP during the OAuth provider exchange OAuthプロバイダの交換中に暗号化されていないHTTPを使用することを許可します
allows the system's trusted CA list to be completely replaced using the
<envar>PGOAUTHCAFILE</envar> environment variable
PGOAUTHCAFILE環境変数を使用して、システムの信頼するCAリストを完全に置き換えることができます
prints HTTP traffic (containing several critical secrets) to standard error during the OAuth flow OAuthフロー中にHTTPトラフィック(いくつかの重要なシークレットを含む)を標準エラーに出力します
permits the use of zero-second retry intervals, which can cause the client to busy-loop and pointlessly consume CPU 0秒の再試行間隔の使用を許可します。 これにより、クライアントはビジーループ状態になり、CPUを無意味に消費する可能性があります
Do not share the output of the OAuth flow traffic with third parties. It contains secrets that can be used to attack your clients and servers. OAuthフロートラフィックの出力を第三者と共有しないでください。 クライアントやサーバを攻撃するために使用できるシークレットが含まれています。
[16]
Performing blocking operations during the
<symbol>PQAUTHDATA_OAUTH_BEARER_TOKEN</symbol> hook callback will
interfere with nonblocking connection APIs such as
<function>PQconnectPoll</function> and prevent concurrent connections
from making progress. Applications which only ever use the
synchronous connection primitives, such as
<function>PQconnectdb</function>, may synchronously retrieve a token
during the hook instead of implementing the
<replaceable>async</replaceable> callback, but they will necessarily
be limited to one connection at a time.
PQAUTHDATA_OAUTH_BEARER_TOKENフックコールバック中にブロッキング操作を実行すると、PQconnectPollなどの非ブロッキングな接続APIが妨害され、同時接続の進行が妨げられます。
PQconnectdbのように同期接続プリミティブのみを使用するアプリケーションでは、asyncコールバックを実装する代わりにフック中にトークンを同期的に取得できますが、一度に1つの接続に制限される必要があります。