> ## Documentation Index
> Fetch the complete documentation index at: https://translations.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Token Vault を設定して、リフレッシュトークン交換およびアクセストークン交換フローを含め、ユーザーに代わってサードパーティ API トークンを安全に保存および交換します。

# Token Vault を設定する

ユーザーが[サポート対象の外部プロバイダー](/docs/ja-jp/secure/tokens/token-vault#supported-external-providers)で認証し、接続を承認すると、アプリケーションは Token Vault にアクセスして、Auth0トークンを外部プロバイダーのアクセストークンと交換できます。

Token Vault を設定するには、次の設定が必要です。

1. サポート対象のソーシャル接続またはエンタープライズ接続に対して、[Connected Accounts for Token Vault を設定](/docs/ja-jp/secure/tokens/token-vault/configure-token-vault#configure-connected-accounts-for-token-vault)します。
2. Token Vault のグラントタイプを使用するように[アプリケーションを設定](#configure-application)します。
3. アプリケーションのトークン交換を設定します。
   * [リフレッシュトークン交換](#configure-refresh-token-exchange)
   * [アクセストークン交換](#configure-access-token-exchange)

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  以前に Auth0 Dashboard で MFA ポリシーを **Always** に設定していた場合は、Token Vault からアクセストークンを取得するために **Never** に設定する必要があります。そうしないと、エラーが発生します。さまざまな MFA ポリシーの詳細については、[Auth0 Dashboard で MFA を有効にする](/docs/ja-jp/secure/multi-factor-authentication/enable-mfa#enable-mfa-in-the-auth0-dashboard)をお読みください。

  対話型フローで MFA チャレンジをトリガーする必要がある場合は、[テナント用に MFA を設定する](/docs/ja-jp/secure/multi-factor-authentication/customize-mfa/customize-mfa-selection-universal-login#prepare-your-tenant)際に **Customize MFA Factors using Actions** を有効にしてください。すると、`event.transaction.protocol` プロパティに基づいて Action を使用して MFA チャレンジをトリガーできます。詳しくは、[Universal Login の MFA 選択をカスタマイズする](/docs/ja-jp/secure/multi-factor-authentication/customize-mfa/customize-mfa-selection-universal-login)をお読みください。
</Callout>

<div id="configure-connected-accounts-for-token-vault">
  ## Connected Accounts for Token Vault を設定する
</div>

Connected Accounts for Token Vault では、複数の外部アカウントにリンクされた一元的な Auth0 ユーザープロファイルを管理します。その後、アプリケーションは Token Vault に保存された資格情報を取得し、ユーザーに代わって外部 API を利用できます。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  アプリケーションで [Organizations](/docs/ja-jp/manage-users/organizations) を使用している場合は、Connected Accounts フローを開始する前に接続を設定し、対象の organization でユーザーを認証してください。接続済みアカウントと保存されたトークンは、引き続きユーザーの Auth0 プロファイルに紐付けられたままです。
</Callout>

Connected Accounts は、サポート対象のソーシャル接続およびエンタープライズ接続に対して設定できます。詳しくは、[Configure Connected Accounts](/docs/ja-jp/secure/tokens/token-vault/connected-accounts-for-token-vault#configure-connected-accounts) をご覧ください。

<div id="configure-application">
  ## アプリケーションを設定
</div>

<TokenVault>グラントタイプを使用するよう、<Tooltip tip="Management API: お客様が管理タスクを実行できるようにする製品です。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=Auth0+Dashboard">Auth0 Dashboard</Tooltip> または <Tooltip tip="Auth0 Dashboard: サービスを設定するための Auth0 の主要製品です。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=Management+API">Management API</Tooltip> を使用してアプリケーションを設定します。</TokenVault>

Token Vault グラントタイプを使用できるのは、特定の種類のクライアントのみです。

1. クライアントはファーストパーティ クライアントである必要があります。つまり、`is_first_party` プロパティが `true` である必要があります。
2. クライアントは、有効な認証方式を持つ confidential client である必要があります。つまり、`token_endpoint_auth_method` プロパティを `none` に設定してはいけません。
3. クライアントは OIDC 準拠である必要があります。つまり、`oidc_conformant` が `true` である必要があります。

<Tabs>
  <Tab title="Auth0 Dashboard">
    1) **アプリケーション > アプリケーション** に移動します。
    2) 設定するアプリケーションを選択します。
    3) **Advanced Settings > Grant Types** で、**Token Vault** グラントタイプを選択します。
    4) **変更を保存** を選択します。

    <Frame>
      <img src="https://mintcdn.com/translations/S4csL9vq6QUX5-Rr/docs/images/token-vault/configure_token_vault_grant_type.png?fit=max&auto=format&n=S4csL9vq6QUX5-Rr&q=85&s=2a416e1e8355934962d10b1c35d37d7e" alt="" width="1368" height="680" data-path="docs/images/token-vault/configure_token_vault_grant_type.png" />
    </Frame>
  </Tab>

  <Tab title="Management API">
    アプリケーションで Token Vault を有効にするには、[Update a Client](https://auth0.com/docs/api/management/v2/clients/patch-clients-by-id) エンドポイントに `PATCH` リクエストを送信し、クライアントの JSON オブジェクトに `urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` グラントタイプを追加します。

    ```bash lines theme={null}
    curl --location --request PATCH 'https://{yourDomain}/api/v2/clients/{clientId}' \
      --header 'Content-Type: application/json' \
      --header 'Authorization: Bearer <YOUR_MANAGEMENT_API_ACCESS_TOKEN>' \
      --data '{
        "grant_types": [
          "authorization_code",
          "refresh_token",
          "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token"
        ]
      }'
    ```
  </Tab>
</Tabs>

<div id="configure-token-exchange">
  ## トークン交換を設定する
</div>

外部プロバイダーのAPIを呼び出すには、アプリケーションで有効なAuth0トークンをToken Vault内の外部プロバイダーのアクセストークンと交換する必要があります。交換に使用するAuth0トークンの種類は、クライアントの種類とユースケースによって異なります。詳しくは、[サポートされているトークン交換](/docs/ja-jp/secure/tokens/token-vault#supported-token-exchanges)をご覧ください。

<div id="configure-refresh-token-exchange">
  ### リフレッシュトークン交換を設定する
</div>

[Token Vault を使用したリフレッシュトークン交換](/docs/ja-jp/secure/tokens/token-vault/refresh-token-exchange-with-token-vault)を使用するには、アプリケーションで次のグラントタイプを設定する必要があります。

* 認可コード: アプリケーションが初回のユーザーログインを実行できるようにします。このとき、アプリケーションは一時的な認可コードを Auth0 アクセストークン、リフレッシュトークン、ID トークンと交換します。
* リフレッシュトークン: ユーザーに再度ログインを求めることなく、長期間有効な Auth0 リフレッシュトークンを使用して新しい Auth0 アクセストークンをリクエストできるようにします。
* Token Vault: アプリケーションが、Auth0 リフレッシュトークンを Token Vault に保存されている外部プロバイダーのアクセストークンと交換できるようにします。

<Tabs>
  <Tab title="Auth0 Dashboard">
    リフレッシュトークン交換用にアプリケーションを設定するには:

    * **アプリケーション > アプリケーション** に移動します。
    * 設定するアプリケーションを選択します。
    * **Advanced Settings > Grant Types** で、**Refresh Token**、**認可コード**、**Token Vault** の各グラントタイプを選択します。
    * **変更を保存** を選択します。
  </Tab>

  <Tab title="Management API">
    リフレッシュトークン交換用にアプリケーションを設定するには、[Update a Client](https://auth0.com/docs/api/management/v2/clients/patch-clients-by-id)エンドポイントに `PATCH` リクエストを送信し、`refresh_token`、`authorization_code,`、`urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` の各グラントタイプをクライアントの JSON オブジェクトに追加します。

    ```bash lines theme={null}
    curl --location --request PATCH 'https://{yourDomain}/api/v2/clients/{clientId}' \
      --header 'Content-Type: application/json' \
      --header 'Authorization: Bearer <YOUR_MANAGEMENT_API_ACCESS_TOKEN>' \
      --data '{
        "grant_types": [
          "authorization_code",
          "refresh_token",
          "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token"
        ]
      }'
    ```
  </Tab>
</Tabs>

<div id="configure-access-token-exchange">
  ### アクセストークン交換を設定する
</div>

[Token Vault を使用したアクセストークン交換](/docs/ja-jp/secure/tokens/token-vault/access-token-exchange-with-token-vault)を利用するには、次の設定が必要です。

* `authorization_code` グラントタイプで [SPA を設定](#configure-your-spa)する。
* SPA が audience として指定して Auth0 アクセストークンをリクエストできるように、[バックエンド API を作成](#create-backend-api)する。
* Token Vault グラントタイプを有効にし、バックエンド API にリンクした [カスタム API クライアントを作成](#create-custom-api-client)する。

<div id="configure-your-spa">
  #### SPA を設定する
</div>

`authorization_code` グラントタイプで SPA を設定します。これにより、SPA は Auth0 Authorization Server から、バックエンド API に対してスコープが設定された Auth0 アクセストークンをリクエストできるようになります。

<Tabs>
  <Tab title="Auth0 Dashboard">
    `authorization_code` グラントタイプで SPA を設定するには:

    * **アプリケーション > アプリケーション** に移動します。
    * 設定するアプリケーションを選択します。
    * **Advanced Settings > Grant Types** で、**認可コード** グラントタイプを選択します。
    * **変更を保存** を選択します。
  </Tab>

  <Tab title="Management API">
    SPA を設定するには、[Update a Client](https://auth0.com/docs/api/management/v2/clients/patch-clients-by-id) エンドポイントに `PATCH` リクエストを送信し、クライアントの JSON オブジェクトに `authorization_code` グラントタイプを追加します:

    ```bash lines theme={null}
    curl --request PATCH 'https://{yourDomain}/api/v2/clients/{clientId}' \
      --header 'Content-Type: application/json' \
      --header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
      --data '{
        "grant_types": [
          "authorization_code"
        ]
      }'
    ```
  </Tab>
</Tabs>

<div id="create-backend-api">
  #### バックエンド API を作成する
</div>

Auth0 Authorization Server とアクセストークン交換を行うために、一意の識別子と必要なスコープを持つバックエンド API を作成します。

<Tabs>
  <Tab title="Auth0 Dashboard">
    Auth0 Dashboard でバックエンド API を作成するには、次の手順に従います。

    * **アプリケーション > APIs** に移動し、**Create API** をクリックします。
    * API を作成するには、[API を登録する](/docs/ja-jp/get-started/auth0-overview/set-up-apis) の手順に従ってください。**注:** API の識別子は、一度設定すると変更できません。
    * **Create** をクリックします。
    * API を作成したら、その API にスコープを追加する必要があります。**Permissions** タブに移動し、**Add a Permission** でスコープを追加します。
  </Tab>

  <Tab title="Management API">
    Management API を使用してバックエンド API を作成するには、`/resource-servers` エンドポイントに `POST` リクエストを送信します。

    ```bash lines  theme={null}
    curl --request POST 'https://{yourDomain}/api/v2/resource-servers' \
      --header 'Content-Type: application/json' \
      --header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
      --data '{
        "name": "My API Resource Server",
        "identifier": "https://my-api.example.com",
        "scopes": [
          {
            "value": "read:calendar",
            "description": "Read calendar events"
          },
          {
            "value": "write:calendar",
            "description": "Write calendar events"
          }
        ]
      }'
    ```
  </Tab>
</Tabs>

<div id="create-custom-api-client">
  #### カスタム API クライアント を作成する
</div>

アクセストークン交換 では、バックエンド API にリンクされた カスタム API クライアント を作成する必要があります。SPA は、Auth0 Authorization Server への認可リクエストで audience として指定することで、バックエンド API への アクセストークン をリクエストできるようになります。カスタム API クライアント は バックエンド API と同じ識別子を持ち、Token Vault のグラントタイプが有効になっています。

バックエンド API が アクセストークン交換 を実行する際は、カスタム API クライアント の資格情報を Auth0 Authorization Server に渡して自身を認証し、Auth0 Dashboard に登録されているものと同一のエンティティであることを証明します。

<Tabs>
  <Tab title="Auth0 Dashboard">
    Auth0 Dashboard で カスタム API クライアント を作成するには、次の手順に従います。

    * **アプリケーション > APIs** に移動し、バックエンド API を選択します。
    * **Add Application** を選択し、アプリケーション名を入力します。
    * **Add** をクリックします。アプリケーションが正常に作成されたら、**Configure Application** をクリックし、**Application Properties** までスクロールします。**アプリケーションの種類** は カスタム API クライアント です。
    * **Advanced Settings > Grant Types** で、カスタム API クライアント の **Token Vault** グラントタイプがすでに有効になっていることを確認します。

    <Frame>
      <img src="https://mintcdn.com/translations/S4csL9vq6QUX5-Rr/docs/images/token-vault/create_custom_api_client.png?fit=max&auto=format&n=S4csL9vq6QUX5-Rr&q=85&s=cb602bcdbbb8809b88c45f91be3ee502" alt="" width="1392" height="870" data-path="docs/images/token-vault/create_custom_api_client.png" />
    </Frame>
  </Tab>

  <Tab title="Management API">
    次のコードサンプルは、バックエンド API と同じ識別子を持つ カスタム API クライアント を作成し、Token Vault のグラントタイプを追加します。

    ```bash lines theme={null}
    curl --request POST 'https://{yourDomain}/api/v2/clients' \
      --header 'Content-Type: application/json' \
      --header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
      --data '{
        "name": "Custom API Client",
        "app_type": "resource_server",
        "resource_server_identifier": "https://my-api.example.com",
        "grant_types": [
          "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token"
        ]
      }'
    ```

    | パラメータ                        | 説明                                                                                                                                              |
    | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
    | `name`                       | カスタム API クライアント の名前。                                                                                                                            |
    | `app_type`                   | カスタム API クライアント のアプリケーションの種類。クライアントを resource server として登録するには、`resource_server` に設定します。                                                        |
    | `resource_server_identifier` | カスタム API クライアント の一意の識別子。バックエンド API の audience、つまり `https://my-api.example.com.` に設定します。                                                         |
    | `grant_types`                | カスタム API クライアント で有効にするグラントタイプ。Token Vault のグラントタイプ `urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` に設定します。 |
  </Tab>
</Tabs>

カスタム API クライアント の作成に成功すると、ユーザーはログイン後に SPA ではなくそのクライアントにリダイレクトされます。
