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

# 資格情報交換

> Machine to Machine フローの一環として Access Token が発行される際に実行される Credentials Exchange Actions について説明します。これらを使用して交換を拒否したり、アクセストークン にカスタムクレームを追加したりできます。

`credentials-exchange` トリガーは、[クライアント認証情報フロー](/docs/ja-jp/get-started/authentication-and-authorization-flow/client-credentials-flow) を介して <Tooltip tip="Access Token: API へのアクセスに使用する、不透明な文字列または JWT 形式の Authorization 資格情報。" cta="用語集を表示" href="/docs/ja-jp/glossary?term=Access+Token">Access Token</Tooltip> が発行される際に、アクセストークン が返される前に実行されます。

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/docs-staging/docs/images/cdy7uua7fh8z/1JPl54LFWCUh5StuglZS2o/41f89372526574c3b8cdac4d5ba38072/Machine_to_Machine_Flow.png" alt="Actions の Machine to Machine フローと、その中のトリガーが実行されるタイミングを示す図。" />
</Frame>

このフローの Actions はブロッキング (同期) で、トリガーの処理の一部として実行されます。Action が完了するまで、Auth0 パイプラインの残りの処理は実行されません。

<h2 id="references">
  リファレンス
</h2>

* [イベントオブジェクト](/docs/ja-jp/customize/actions/reference/credentials-exchange/credentials-exchange-event-object): クライアント資格情報交換リクエストに関するコンテキスト情報を提供します。
* [API object](/docs/ja-jp/customize/actions/reference/credentials-exchange/credentials-exchange-api-object): フローの動作を変更するためのメソッドを提供します。

<h2 id="common-use-cases">
  一般的なユースケース
</h2>

<h3 id="access-control">
  アクセス制御
</h3>

credentials-exchange Action を使用すると、カスタムロジックに基づいて アクセストークン の発行を拒否できます。

```javascript lines theme={null}
/**
 * @param {Event} event - クライアントクレデンシャルグラントのリクエストに関する詳細。
 * @param {CredentialsExchangeAPI} api - クライアントクレデンシャルグラントの動作を変更するメソッドを提供するインターフェース。
 */
exports.onExecuteCredentialsExchange = async (event, api) => {
  if (event.request.geoip.continentCode === "NA") {
    api.access.deny('invalid_request', "北米からのアクセスは許可されていません。");
  }
};
```

<h3 id="add-custom-claims-to-the-access-token">
  アクセストークンにカスタムクレームを追加する
</h3>

credentials-exchange Action を使用すると、アクセストークンにカスタムクレームを追加できます。

```javascript lines theme={null}
/**
 * @param {Event} event - クライアントクレデンシャルグラントリクエストの詳細。
 * @param {CredentialsExchangeAPI} api - クライアントクレデンシャルグラントの動作を変更するためのメソッドを提供するインターフェース。
 */
exports.onExecuteCredentialsExchange = async (event, api) => {
  api.accessToken.setCustomClaim("https://my-api.exampleco.com/request-ip", event.request.ip);
};
```

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  URI形式の名前空間付きカスタムクレームを使用することを強くお勧めします。名前空間付きおよび非名前空間のカスタムクレームについて詳しくは、[Create Custom Claims](/docs/ja-jp/secure/tokens/json-web-tokens/create-custom-claims)を参照してください。
</Callout>
