> ## 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.

> カスタムトークンプロファイルにActionを関連付けて、カスタムトークン交換を設定する方法を説明します。

# カスタムトークン交換を設定する

export const ReleaseStageNotice = ({feature, stage, plans, contact, terms}) => {
  const stageTextMap = {
    "beta": "Beta",
    "ea": "早期アクセス"
  };
  const stageText = stageTextMap[stage] || "製品リリース段階";
  const prsLink = "/docs/troubleshoot/product-lifecycle/product-release-stages";
  const linkify = (text, url) => {
    return <a href={url} target="_blank" rel="noreferrer" class="link">{text}</a>;
  };
  const includeDetails = (plans, contact, terms) => {
    const hasDetails = terms || plans || contact;
    if (!hasDetails) return null;
    return <span data-as="p">
            {plans && <>この機能は{linkify(`${plans}プラン`, "https://auth0.com/pricing")}でご利用いただけます。 </>}
            {contact && "参加をご希望の場合は、" + contact + "までお問い合わせください。 "}
            {terms && <>この機能を使用することにより、Oktaの該当する無料トライアル規約および{linkify("Master Subscription Agreement", "https://www.okta.com/legal")}に同意したものとみなされます。</>}
        </span>;
  };
  return <Warning>
            <span data-as="p">
                <strong>{feature}機能は現在、{linkify(stageText, prsLink)}です。</strong>
            </span>

            {includeDetails(plans, contact, terms)}
        </Warning>;
};

<ReleaseStageNotice feature="カスタムトークン交換 (CTE)" stage="ea" plans="B2C Professional, B2B Professional, and Enterprise" terms="true" />

アプリケーションのカスタムトークン交換を設定するには、次の操作が必要です。

* [前提条件を満たす](#prerequisites)
* [アプリケーションでカスタムトークン交換を有効にする](#enable-custom-token-exchange-for-your-application)
* カスタムトークン交換プロファイルを[設定](#configure-custom-token-exchange-profile)し、[管理](#manage-custom-token-exchange-profile)する

<div id="prerequisites">
  ## 前提条件
</div>

カスタムトークン交換を設定する前に、アプリケーションが以下の前提条件を満たしていることを確認してください。

* ファーストパーティ クライアントである
* OIDC 準拠である

アプリケーションを設定するには、Auth0 Dashboard で **[アプリケーション > 詳細設定 > OAuth](https://auth0.com/docs/get-started/applications/application-settings#oauth)** に移動してください。

<div id="enable-custom-token-exchange-for-your-application">
  ## アプリケーションでカスタムトークン交換を有効にする
</div>

カスタムトークン交換を有効にするには、[新しいアプリケーションを作成](/docs/ja-jp/get-started/auth0-overview/create-applications)するか、Auth0 Dashboard または Management API を使用して既存のアプリケーションを更新します。カスタムトークン交換で使用するために、複数のアプリケーションを作成できます。

新しいアプリケーションを作成する場合:

1. デフォルトでは、カスタムトークン交換は無効になっています。カスタムトークン交換を有効にするには、Management API を使用して [Create a Client](https://auth0.com/docs/api/management/v2/clients/post-clients) に `POST` リクエストを送信するか、[Update a Client](https://auth0.com/docs/api/management/v2/clients/patch-clients-by-id) に `PATCH` リクエストを送信します。`token_exchange` 配下の `allow_any_profile_of_type` 属性を `["custom_authentication"]` に設定します:

```json lines theme={null}
{
  "token_exchange": {
    "allow_any_profile_of_type": ["custom_authentication"]
  }
}
```

2. アプリケーションで、カスタムトークン交換に使用する接続を有効にします。
3. ご使用のアプリケーションが[ファーストパーティ](/docs/ja-jp/get-started/applications/first-party-and-third-party-applications)として設定され、**[Auth0 Dashboard > アプリケーション > 詳細設定 > OAuth](/docs/ja-jp/get-started/applications/application-settings#oauth)** で OIDC 準拠として構成されていることを確認します。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  import mode が `ON` のカスタム DB は、`setUserById()` 操作でのみサポート対象です。
</Callout>

アプリケーションを作成したら、後で `/oauth/token` エンドポイントを呼び出すときに使用する `client_id` と `client_secret` を控えておきます。

<div id="configure-custom-token-exchange-profile">
  ## カスタムトークン交換プロファイルを設定する
</div>

各カスタムトークン交換プロファイルでは、`subject_token_type` と Action が1対1でマッピングされ、Action には特定のユースケースに対応するコードロジックが含まれます。

特定の `subject_token_type` 値を指定して `/oauth/token` エンドポイントに送信されたカスタムトークン交換リクエストは、対応するカスタムトークンプロファイルにマッピングされ、関連付けられた Action にルーティングされて処理されます。

カスタムトークン交換プロファイルを作成するには、次の手順を行います。

1. [プロファイル用のActionを作成する](#create-action-for-the-profile)
2. [カスタムトークン交換プロファイルを作成する](#create-custom-token-exchange-profile)
3. [カスタムトークン交換プロファイルを管理する](#manage-custom-token-exchange-profile)

<div id="create-action-for-the-profile">
  ### プロファイル用のActionを作成する
</div>

Custom Token Exchange の Event オブジェクトと API オブジェクトを使用して、次の処理を行う Action を作成します。

* `subject_token_type` に基づいて `subject_token` をデコードし、検証します。これにより、トランザクションにおけるユーザー情報を取得できます。
* トランザクションに適用する必要がある認可ポリシーを適用します。

トランザクションを続行できることを確認したら、ユーザーを設定します。すると Auth0 は、このユーザーに対してユーザー認証の一環として、アクセストークン、ID トークン、リフレッシュトークンを発行します。

Custom Token Exchange の Action の例については、[Example Use Cases and Code Samples](/docs/ja-jp/authenticate/custom-token-exchange/cte-example-use-cases) を参照してください。

Action を作成したら、Auth0 Dashboard で追加してデプロイします。

1. **Actions > Library** に移動します。
2. **Create Action > Build from Scratch** を選択します。
3. **Create Action** ダイアログで名前を入力し、ドロップダウンから **カスタムトークン交換** トリガーを選択します。

<Frame>
  <img src="https://mintcdn.com/translations/eVsQcTnbClN-oB7d/docs/images/cdy7uua7fh8z/22vz9dsCFj5Ruot7U0HIVx/1c3dc4b562334dab9d6ac415028ea76e/Screenshot_2025-02-05_at_8.48.34_AM.png?fit=max&auto=format&n=eVsQcTnbClN-oB7d&q=85&s=f6f64e6e8208a914cf5f7d73e4a47ada" alt="" width="400" height="440" data-path="docs/images/cdy7uua7fh8z/22vz9dsCFj5Ruot7U0HIVx/1c3dc4b562334dab9d6ac415028ea76e/Screenshot_2025-02-05_at_8.48.34_AM.png" />
</Frame>

4. **Create** を選択します。
5. Action を **Deploy** します。

<Frame>
  <img src="https://mintcdn.com/translations/MV7tE-x71x8RWRES/docs/images/cdy7uua7fh8z/56NZA69Gmzha167xfgRD0W/302e59276815d2e2644ab2da3b9b5f1f/Screenshot_2025-02-03_at_10.29.17_AM.png?fit=max&auto=format&n=MV7tE-x71x8RWRES&q=85&s=7b29ab5bb0bb3ce0075837f48592ab6b" alt="" width="1244" height="574" data-path="docs/images/cdy7uua7fh8z/56NZA69Gmzha167xfgRD0W/302e59276815d2e2644ab2da3b9b5f1f/Screenshot_2025-02-03_at_10.29.17_AM.png" />
</Frame>

Action をデプロイしたら、Auth0 によって割り当てられた Action ID をコピーします。Action にはまだカスタムロジックを追加する必要があります。まず、カスタムトークン交換プロファイル を作成するために Action ID を取得します。

6. Auth0 Dashboard で Action ID を取得するには、ブラウザーウィンドウの URL を確認します。次の画像に示すように、Action ID は URL の最後の部分です。

<Frame>
  <img src="https://mintcdn.com/translations/eVsQcTnbClN-oB7d/docs/images/cdy7uua7fh8z/1Xx4UbgZR0FIuLC1KVvhKG/bde4469d770c2ff8d37c19895a0c8e66/Screenshot_2025-02-03_at_10.31.18_AM.png?fit=max&auto=format&n=eVsQcTnbClN-oB7d&q=85&s=da53b4a079e0f2338b54db38647b3c24" alt="" width="1244" height="558" data-path="docs/images/cdy7uua7fh8z/1Xx4UbgZR0FIuLC1KVvhKG/bde4469d770c2ff8d37c19895a0c8e66/Screenshot_2025-02-03_at_10.31.18_AM.png" />
</Frame>

Management API から Action ID を取得することもできます。まず、API を利用するための Management API トークンを取得します。次に、`/actions` エンドポイントに対して次の `GET` リクエストを実行します。

```bash lines theme={null}
curl --location 'https://{yourDomain}/api/v2/actions/actions?actionName={yourActionName}' \
--header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
```

レスポンス本文の `actions[0].id.` に Action ID が含まれているはずです。カスタムトークン交換プロファイル を作成するには、Action ID が必要です。

<div id="create-custom-token-exchange-profile">
  ### カスタムトークン交換プロファイルを作成
</div>

カスタムトークン交換プロファイルを作成するには、Management API を使用して、次のパラメータを指定した `POST` リクエストを `/token-exchange-profiles` エンドポイントに送信します。

```bash lines theme={null}
curl --location 'https://{yourDomain}/api/v2/token-exchange-profiles' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
--data '{
    "name": "<YOUR_PROFILE_NAME>",
    "subject_token_type": "<YOUR_UNIQUE_PROFILE_TOKEN_TYPE_URI>",
    "action_id": "<YOUR_ACTION_ID>",
    "type": "custom_authentication"
}'
```

| パラメータ                | 説明                                                                                                                                                                                                                                                                             |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `subject_token_type` | `https://` または `urn` で始まる、一意のプロファイルトークンタイプ URI<br /><br />次の名前空間は予約されているため、使用できません。<br /><br /><ul><li>`http://auth0.com`</li><li>`https://auth0.com`</li><li>`http://okta.com`</li><li>`https://okta.com`</li><li>`urn:ietf`</li><li>`urn:auth0`</li><li>`urn:okta`</li></ul> |
| `action_id`          | カスタムトークンプロファイル に関連付けられた Action の Action ID。                                                                                                                                                                                                                                    |
| `type`               | `custom_authentication` に設定する必要があります。                                                                                                                                                                                                                                          |

カスタムトークン交換プロファイル の作成に成功すると、次のようなレスポンスが返されます。

```json lines theme={null}
{
  "id":"tep_9xqewuejpa2RTltf",
  "name":"<YOUR_PROFILE_NAME>",
  "type":"custom_authentication",
  "subject_token_type":"<YOUR_UNIQUE_PROFILE_TOKEN_TYPE_URI>",
  "action_id":"<YOUR_ACTION_ID>",
  "created_at":"2025-01-30T13:19:00.616Z",
  "updated_at":"2025-01-30T13:19:00.616Z"
}
```

<div id="manage-custom-token-exchange-profile">
  ### カスタムトークン交換プロファイルを管理する
</div>

カスタムトークン交換プロファイルを管理するには、Management API を使用して `/token-exchange-profiles` エンドポイントにリクエストを送信します。

すべてのカスタムトークン交換プロファイルを取得するには、`/token-exchange-profiles` エンドポイントに対して次の `GET` リクエストを送信します。プロファイルが複数ある場合、`/token-exchange-profiles` エンドポイントではチェックポイントベースのページネーションを利用できます。

```bash lines theme={null}
curl --location 'https://{yourDomain}/api/v2/token-exchange-profiles' \
--header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
```

既存のプロファイルの名前または `subject_token_type` を更新するには、`/token-exchange-profiles` エンドポイントに対して次の `PATCH` リクエストを実行します。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  Action を作成すると、Action ID は変更できません。
</Callout>

```bash lines theme={null}
curl --location --request PATCH 'https://{yourDomain}/api/v2/token-exchange-profiles/{yourProfileId}' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
--data '{
    "name": "external-idp-migration",
    "subject_token_type": "urn:partner0:external-idp-migration"
}'
```

カスタムトークン交換プロファイル を削除するには、`/token-exchange-profiles` エンドポイントに次の `DELETE` リクエストを送信します。

```bash lines theme={null}
curl --location --request DELETE 'https://{yourDomain}/api/v2/token-exchange-profiles/{yourProfileId}' \
--header 'Authorization: Bearer <YOUR_MANAGEMENT_API_TOKEN>' \
--data ''
```
