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

# トークンの有効期間のスコープ単位での制御

> アクセス (リフレッシュ) トークンの有効期間をスコープ単位で制御する方法について解説。

## はじめに

本ドキュメントでは、アクセス (リフレッシュ) トークンの有効期間をスコープ単位で制御する方法について解説します。

> 本機能は Authlete 2.0 以降でのみ利用可能になります。

## スコープ単位でのトークンの有効期間設定

Authlete 1.1 では、アクセス (リフレッシュ) トークンの有効期間はサービス (認可サーバー) 単位でのみ設定可能な項目でしたが、Authlete 2.0 以降では、**スコープ単位でもトークンの有効期間の設定**が可能となりました。

これにより認可サーバーは「write\*\* **スコープは重要度の高い権限なので、write** \*\*スコープがリクエストされた場合に払い出されるトークンの有効期間は短くしたい。」といったユースケースにも対応できるようになります。

## トークンの有効期間の計算ロジック

別記事「[トークンの有効期間の計算ロジック](/ja/configuration-reference/tokens-and-claims/how-authlete-determines-token-duration)」をご覧ください。

## 設定

本機能を利用するためには、サービス管理画面上でスコープの属性を設定する必要があります。詳細は「[スコープの属性機能](/ja/configuration-reference/tokens-and-claims/scope-attributes)」をご覧ください。

<img src="https://mintcdn.com/authlete/WGQFc11Y4wUplGrc/img/kb/ja/oauth-and-openid-connect/tokens/token-duration-per-scope_1.png?fit=max&auto=format&n=WGQFc11Y4wUplGrc&q=85&s=27215fb92680fb3a24358c999f062637" alt="token-duration-per-scope_1" width="947" height="497" data-path="img/kb/ja/oauth-and-openid-connect/tokens/token-duration-per-scope_1.png" />

## 実行例

ここでは、ある 1 つのサービスに 2 つのスコープが設定されている環境において、アクセストークンの有効期間が以下の通り設定されているものとします。

| エンティティ       | アクセストークンの有効期間（秒） |
| ------------ | ---------------- |
| サービス         | 86,400           |
| *read* スコープ  | 3,600            |
| *write* スコープ | 600              |

この状況において、クライアントから送信された、スコープの組み合わせが異なる「インプリシットグラントフローの認可リクエスト」に対し、Authlete の [/auth/authorization/issue API](/api-reference/authorization-endpoint/issue-authorization-response) は以下のレスポンスを返却します。

### 1. どのスコープもリクエストされなかった場合

```
{
    "type": "authorizationIssueResponse",
    "accessTokenDuration": 86400,
    "responseContent": "https://client.example.org/cb/example.com
        #access_token=xbNhif-bsWOPyRasrEFUFurBSQUHnarjv6sMz8cSDjg
        &token_type=Bearer
        &expires_in=86400
        &scope=",
    ...
}
```

\=> サービスに設定されたアクセストークン有効期間秒数が利用されていることがわかる。

## 2. read スコープがリクエストされた場合

```
{
    "type": "authorizationIssueResponse",
    "accessTokenDuration": 3600,
    "responseContent": "https://client.example.org/cb/example.com
                        #access_token=8ihMgxhMf-HYBy-O2rYVlMHEQD7WcvFGUhaXfP3YZHs
                        &token_type=Bearer
                        &expires_in=3600
                        &scope=read",
    ...
}
```

\=> *read* スコープに設定されたアクセストークン有効期間秒数が利用されていることがわかる。

## 3. write スコープがリクエストされた場合

```
{
    "type": "authorizationIssueResponse",
    "accessTokenDuration": 600,
    "responseContent": "https://client.example.org/cb/example.com#access_token=lZ4rjCLlwDvgO2wgOaXhDhNGMhpUE_yGi3pyTPcHFyU
    &token_type=Bearer
    &expires_in=600
    &scope=write",
    ...
}
```

\=> *write* スコープに設定されたアクセストークン有効期間秒数が利用されていることがわかる。

## 4. read スコープと write スコープの両者がリクエストされた場合

```
{
    "type": "authorizationIssueResponse",
    "**accessTokenDuration":600,**    "responseContent": "https://client.example.org/cb/example.com     #access_token=3zQNzTiX5MUxO1Gy0ZFfD7mhn3U1Cg3Q15rhjNob6uc
    &token_type=Bearer
    &**expires\_in=600**    &**scope=read+write,**"    ...
}
```

\=> *write* スコープに設定されたアクセストークン有効期間秒数が利用されていることがわかる。
