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

> Actions の Machine to Machine Flow と、その一部として実行される credentials-exchange Action トリガーについて説明します。

# Machine to Machine トリガー

Machine to Machine トリガーは、[Client Credentials Flow](/ja/docs/get-started/authentication-and-authorization-flow/client-credentials-flow) を通じて <Tooltip tip="アクセストークン: API へのアクセスに使用される、不透明な文字列または JWT 形式の認可資格情報。" cta="用語集を見る" href="/ja/docs/glossary?term=Access+Token">アクセストークン</Tooltip> が発行される際に実行されます。

<Frame>
  <img src="https://mintcdn.com/translations/eVsQcTnbClN-oB7d/docs/images/cdy7uua7fh8z/1JPl54LFWCUh5StuglZS2o/41f89372526574c3b8cdac4d5ba38072/Machine_to_Machine_Flow.png?fit=max&auto=format&n=eVsQcTnbClN-oB7d&q=85&s=12356c2f5b637c5760d658116c2345bf" alt="Actions の Machine to Machine Flow と、その中のトリガーがいつ実行されるかを示す図。" width="851" height="215" data-path="docs/images/cdy7uua7fh8z/1JPl54LFWCUh5StuglZS2o/41f89372526574c3b8cdac4d5ba38072/Machine_to_Machine_Flow.png" />
</Frame>

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

<div id="triggers">
  ## トリガー
</div>

<div id="m2m-client-credentials">
  ### M2M / クライアントクレデンシャル
</div>

`credentials-exchange` トリガーは、アクセストークンが返却される前に実行される関数です。

<div id="references">
  #### 参考資料
</div>

* [Event object](/ja/docs/customize/actions/explore-triggers/machine-to-machine-trigger/credentials-exchange-event-object): クライアントクレデンシャル交換のリクエストに関するコンテキスト情報を提供します。
* [API object](/ja/docs/customize/actions/explore-triggers/machine-to-machine-trigger/credentials-exchange-api-object): フローの動作を変更するためのメソッドを提供します。

<div id="common-use-cases">
  ## 主なユースケース
</div>

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

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', "Access from North America is not allowed.");
  }
};
```

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

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 形式の名前空間付きカスタムクレームを使用することを強く推奨します。名前空間付きカスタムクレームと名前空間なしのカスタムクレームの詳細については、[カスタムクレームを作成する](/ja/docs/secure/tokens/json-web-tokens/create-custom-claims) を参照してください。
</Callout>
