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

# Token Vault を設定する

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

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

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

1. サポート対象のソーシャル接続またはエンタープライズ接続に対して、[Connected Accounts for Token Vault を設定](/docs/docs/ja-jp/secure/call-apis-on-users-behalf/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)

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

対話型フローで MFA チャレンジをトリガーする必要がある場合は、[テナント用に MFA を設定する](/docs/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/docs/ja-jp/secure/multi-factor-authentication/customize-mfa/customize-mfa-selection-universal-login)をお読みください。

## Connected Accounts for Token Vault を設定する

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

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

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

## アプリケーションを設定

Token Vault グラントタイプを使用するよう、Auth0 Dashboard (Management API: お客様が管理タスクを実行できるようにする製品です。) または Management API (Auth0 Dashboard: サービスを設定するための Auth0 の主要製品です。) を使用してアプリケーションを設定します。

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

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

> [tab] Auth0 Dashboard
>
> 1. **アプリケーション > アプリケーション** に移動します。
> 2. 設定するアプリケーションを選択します。
> 3. **Advanced Settings > Grant Types** で、**Token Vault** グラントタイプを選択します。
> 4. **変更を保存** を選択します。
>
> ![](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/token-vault/configure_token_vault_grant_type.png)

> [tab] Management API
>
> アプリケーションで Token Vault を有効にするには、[Update a Client](/docs/docs/ja-jp/api/management/v2/clients/patch-clients-by-id) エンドポイントに `PATCH` リクエストを送信し、クライアントの JSON オブジェクトに `urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` グラントタイプを追加します。
>
> > [tab] Auth0 CLI
> >
> > Auth0 CLI を使用していますか？まだの場合は、このコマンドを実行する前に[CLI セッションをセットアップして認証](/docs/docs/ja-jp/deploy-monitor/auth0-cli)してください。
> >
> > ```bash theme={null}
> > auth0 api patch "clients/{clientId}" \
> >   --data '{
> >     "grant_types": [
> >       "authorization_code",
> >       "refresh_token",
> >       "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token"
> >     ]
> >   }'
> > ```
>
> > [tab] cURL
> >
> > ```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"
> >     ]
> >   }'
> > ```

## トークン交換を設定する

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

### リフレッシュトークン交換を設定する

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

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

> [tab] Auth0 Dashboard
>
> リフレッシュトークン交換用にアプリケーションを設定するには:
>
> * **アプリケーション > アプリケーション** に移動します。
> * 設定するアプリケーションを選択します。
> * **Advanced Settings > Grant Types** で、**リフレッシュトークン**、**認可コード**、**Token Vault** の各グラントタイプを選択します。
> * **変更を保存** を選択します。

> [tab] Management API
>
> リフレッシュトークン交換用にアプリケーションを設定するには、[Update a Client](/docs/docs/ja-jp/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 オブジェクトに追加します。
>
> > [tab] Auth0 CLI
> >
> > ```bash theme={null}
> > auth0 api patch "clients/{clientId}" \
> >   --data '{
> >     "grant_types": [
> >       "authorization_code",
> >       "refresh_token",
> >       "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token"
> >     ]
> >   }'
> > ```
>
> > [tab] cURL
> >
> > ```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"
> >     ]
> >   }'
> > ```

### アクセストークン交換を設定する

[Token Vault を使用したアクセストークン交換](/docs/docs/ja-jp/secure/call-apis-on-users-behalf/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)する。

#### SPA を設定する

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

> [tab] Auth0 Dashboard
>
> `authorization_code` グラントタイプで SPA を設定するには:
>
> * **アプリケーション > アプリケーション** に移動します。
> * 設定するアプリケーションを選択します。
> * **Advanced Settings > Grant Types** で、**認可コード** グラントタイプを選択します。
> * **変更を保存** を選択します。

> [tab] Management API
>
> SPA を設定するには、[Update a Client](/docs/docs/ja-jp/api/management/v2/clients/patch-clients-by-id) エンドポイントに `PATCH` リクエストを送信し、クライアントの JSON オブジェクトに `authorization_code` グラントタイプを追加します:
>
> > [tab] Auth0 CLI
> >
> > ```bash theme={null}
> > auth0 api patch "clients/{clientId}" \
> >   --data '{
> >     "grant_types": [
> >       "authorization_code"
> >     ]
> >   }'
> > ```
>
> > [tab] cURL
> >
> > ```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"
> >     ]
> >   }'
> > ```

#### バックエンド API を作成する

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

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

> [tab] Management API
>
> Management API を使用してバックエンド API を作成するには、`/resource-servers` エンドポイントに `POST` リクエストを送信します。
>
> > [tab] Auth0 CLI
> >
> > ```bash theme={null}
> > auth0 api post "resource-servers" \
> >   --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] cURL
> >
> > ```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"
> >       }
> >     ]
> >   }'
> > ```

#### カスタム API クライアント を作成する

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

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

> [tab] Auth0 Dashboard
>
> Auth0 Dashboard で カスタム API クライアント を作成するには、次の手順に従います。
>
> * **アプリケーション > APIs** に移動し、バックエンド API を選択します。
> * **Add Application** を選択し、アプリケーション名を入力します。
> * **Add** をクリックします。アプリケーションが正常に作成されたら、**Configure Application** をクリックし、**Application Properties** までスクロールします。**アプリケーションの種類** は カスタム API クライアント です。
> * **Advanced Settings > Grant Types** で、カスタム API クライアント の **Token Vault** グラントタイプがすでに有効になっていることを確認します。
>
> ![](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/token-vault/create_custom_api_client.png)

> [tab] Management API
>
> 次のコードサンプルは、バックエンド API と同じ識別子を持つ カスタム API クライアント を作成し、Token Vault のグラントタイプを追加します。
>
> > [tab] Auth0 CLI
> >
> > ```bash theme={null}
> > auth0 api post "clients" \
> >   --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"
> >     ]
> >   }'
> > ```
>
> > [tab] cURL
> >
> > ```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` に設定します。 |

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