> ## 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 の設定方法を説明します。

# Token Vault を設定する

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

Token Vault を設定するには、次の手順を実行します。

1. サポートされているソーシャル接続またはエンタープライズ接続に対して、[Token Vault 用の Connected Accounts を設定](/ja/docs/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 を有効にする](/ja/docs/secure/multi-factor-authentication/enable-mfa#enable-mfa-in-the-auth0-dashboard)を参照してください。

  インタラクティブなフローで MFA チャレンジをトリガーする必要がある場合は、[テナントに MFA を設定する](/ja/docs/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 選択をカスタマイズする](/ja/docs/secure/multi-factor-authentication/customize-mfa/customize-mfa-selection-universal-login)を参照してください。
</Callout>

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

Token Vault の Connected Accounts では、複数の外部アカウントにリンクされた統合 Auth0 ユーザープロファイルを管理します。その後、アプリケーションは Token Vault に保存された認証情報を取得し、ユーザーに代わって外部 API を操作できます。

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

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

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

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

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

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

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

    <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トークンの種類は、クライアントの種類とユースケースによって異なります。詳細については、[サポートされているトークン交換](/ja/docs/secure/tokens/token-vault#supported-token-exchanges)を参照してください。

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

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

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

<Tabs>
  <Tab title="Auth0 Dashboard">
    リフレッシュトークン交換用にアプリケーションを設定するには、次の手順に従います。

    * **Applications > Applications** に移動します。
    * 設定するアプリケーションを選択します。
    * **Advanced Settings > Grant Types** で、**Refresh Token**、**Authorization Code**、**Token Vault** のグラントタイプを選択します。
    * **Save Changes** を選択します。
  </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 を使用したアクセストークン交換](/ja/docs/secure/tokens/token-vault/access-token-exchange-with-token-vault)を使用するには、次の設定が必要です。

* `authorization_code` グラントタイプを使用して、[SPA を設定](#configure-your-spa)します。
* SPA がオーディエンスとして指定して Auth0 のアクセストークンをリクエストできるように、[バックエンド API を作成](#create-backend-api)します。
* Token Vault グラントタイプを有効にし、バックエンド API に関連付けた [Custom API Client を作成](#create-custom-api-client)します。

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

`authorization_code` グラントタイプを使用するように SPA を設定します。これにより、SPA はバックエンド API 用のスコープが設定された Auth0 アクセストークンを Auth0 認可サーバーにリクエストできるようになります。

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

    * **Applications > Applications** に移動します。
    * 設定するアプリケーションを選択します。
    * **Advanced Settings > Grant Types** で、**Authorization Code** グラントタイプを選択します。
    * **Save Changes** を選択します。
  </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>

一意の識別子と必要なスコープを持つバックエンド API を作成します。この API は、Auth0 認可サーバーとアクセストークン交換を行います。

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

    * **Applications > APIs** に移動し、**Create API** をクリックします。
    * API を作成するには、[Register APIs](/ja/docs/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">
  #### Custom API Client を作成する
</div>

アクセストークン交換を行うには、バックエンド API に紐付けられた Custom API Client を作成する必要があります。SPA は、Auth0 認可サーバーへの認可リクエストでバックエンド API をオーディエンスとして指定することで、そのバックエンド API 用のアクセストークンをリクエストできるようになります。Custom API Client はバックエンド API と同じ識別子を持ち、Token Vault のグラントタイプが有効になっています。

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

<Tabs>
  <Tab title="Auth0 Dashboard">
    Auth0 Dashboard で Custom API Client を作成するには、次の手順を実行します。

    * **Applications > APIs** に移動し、バックエンド API を選択します。
    * **Add Application** を選択し、アプリケーション名を入力します。
    * **Add** をクリックします。アプリケーションの作成が完了したら、**Configure Application** をクリックして **Application Properties** までスクロールします。**Application Type** は Custom API Client です。
    * **Advanced Settings > Grant Types** で、Custom API Client の **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 と同じ識別子を持つ Custom API Client を作成し、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"
        ]
      }'
    ```

    | Parameter                    | Description                                                                                                                                          |
    | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
    | `name`                       | Custom API Client の名前。                                                                                                                               |
    | `app_type`                   | Custom API Client のアプリケーションタイプ。クライアントをリソースサーバーとして登録するには、`resource_server` に設定します。                                                                    |
    | `resource_server_identifier` | Custom API Client の一意の識別子。バックエンド API のオーディエンス、つまり `https://my-api.example.com.` に設定します。                                                              |
    | `grant_types`                | Custom API Client で有効にするグラントタイプ。Token Vault のグラントタイプである `urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` に設定します。 |
  </Tab>
</Tabs>

Custom API Client の作成が完了すると、ログイン後、ユーザーは SPA ではなくそのクライアントにリダイレクトされます。
