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

# RFC 8693 Token Exchange

> RFC 8693 OAuth 2.0 Token Exchange 仕様と Authlete 2.x の実装の説明です。

<Note>
  このページは **Authlete 2.x** 向けのドキュメントです。3.0 の内容は[RFC 8693 OAuth 2.0 Token Exchange（3.0）](/ja/protocols-and-flows/advanced-flows/oauth-2-0-token-exchange-rfc-8693)をご覧ください。
</Note>

# はじめに

[RFC 8693 OAuth 2.0 Token Exchange](https://www.rfc-editor.org/rfc/rfc8693.html) (以下 "Token Exchange") は、トークンエンドポイントに既存のトークンを提示して新しいトークンを取得する方法を定義している技術仕様です。

当仕様はとても柔軟ですが、安全なトークン交換に必要となる詳細事項については定義されていません。そのため、同仕様を認可サーバーに実装する際には、未定義部分に関する詳細仕様化が必要です。

本記事では、Token Exchange 仕様を用いる場合の詳細仕様化の要点と、Authlete 2.x における同仕様のサポートについて説明します。

# 仕様

次の図はトークン交換フローを示しています。

<img src="https://mintcdn.com/authlete/oZd1VUaYFwHO2MBx/protocols-and-flows/advanced-flows/token_exchange_ja.png?fit=max&auto=format&n=oZd1VUaYFwHO2MBx&q=85&s=1290a0c11ff8c5f58a38771bc0c3a2c5" alt="" width="3153" height="1805" data-path="protocols-and-flows/advanced-flows/token_exchange_ja.png" />

## トークンの種別

トークン交換フローにおいて入力として提示するトークンには、**サブジェクトトークン (Subject Token)** と **アクタートークン (Actor Token)** の 2 種類があります。

* **サブジェクトトークン**: 必須。リクエストの当事者のアイデンティティを表す。
* **アクタートークン**: 任意。代行者のアイデンティティを表す。

### トークンタイプ

仕様は次のトークンタイプ識別子を定義しています。

| トークンタイプ        | トークンタイプ識別子                                         |
| :------------- | :------------------------------------------------- |
| JWT            | `urn:ietf:params:oauth:token-type:jwt`             |
| アクセストークン       | `urn:ietf:params:oauth:token-type:access_token`    |
| リフレッシュトークン     | `urn:ietf:params:oauth:token-type:refresh_token`   |
| ID トークン        | `urn:ietf:params:oauth:token-type:id_token`        |
| SAML 1.1 / 2.0 | `urn:ietf:params:oauth:token-type:saml1` / `saml2` |

## トークン交換リクエスト

* **グラントタイプ**: `urn:ietf:params:oauth:grant-type:token-exchange` を `grant_type` に指定。
* **主なパラメーター**: `subject_token`, `subject_token_type`（必須）、`actor_token`, `actor_token_type`（任意）、`resource`, `audience`, `scope`, `requested_token_type`（任意）。

## トークン交換レスポンス

* `access_token`, `issued_token_type`, `token_type` が必須。`expires_in`, `scope`, `refresh_token` は任意。

# Authlete 2.x における対応

Token Exchange は Authlete 2.3 以降でサポートされます。

* トークンリクエストを [`/auth/token`](https://docs.authlete.com/en/shared/latest#post-/api/auth/token) API に送信すると、`grant_type` が `urn:ietf:params:oauth:grant-type:token-exchange` でバリデーションをパスした場合、`action` として `TOKEN_EXCHANGE` が返却されます。
* トークン交換レスポンスの生成には [`/auth/token/create`](https://docs.authlete.com/en/shared/latest#post-/api/auth/token/create) API を用い、`grantType` に `TOKEN_EXCHANGE` を指定します。

## トークンのバリデーション

Authlete は `subject_token` / `actor_token` のトークンタイプに応じてバリデーションを行います（JWT・アクセストークン・リフレッシュトークン・ID トークン・SAML など）。詳細は [Authlete ウェブサイトの Token Exchange ページ（2.x）](https://www.authlete.com/ja/developers/v2/token_exchange/) を参照してください。

## 関連する設定項目（2.x）

* **サービス設定**: 特定可能なクライアントのみ、コンフィデンシャルクライアントのみ、許可されたクライアントのみ、暗号化/無署名 JWT の拒否など。
* **クライアント拡張**: `tokenExchangePermitted` でトークン交換を明示的に許可。

# 利用例

トークン交換の実装例は [authlete-java-jaxrs](https://github.com/authlete/authlete-java-jaxrs/) の `TokenRequestHandler.java` および [java-oauth-server](https://github.com/authlete/java-oauth-server) の `TokenExchanger.java` を参照してください。
