> ## 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 2.0 以降でのみ利用可能になります。

## スコープ属性とは

スコープ属性とは、あるスコープに関連づけられる key-value 形式の任意の属性です。
それぞれのスコープに対して複数の属性を関連づけ、認可サーバーにおける認可決定やそ
の他の処理に、それらの属性を活用できます。またいくつかの属性は Authlete があらか
じめ定義しており、システム設定に用いられます。\
設定方法

***

スコープの属性の設定は以下の手順で行います。

1. サービス管理者コンソールにログインし、対象のサービスの編集画面を開きます。
2. 「スコープを作成」ボタンを押下し、スコープの編集画面を開きます。
3. 最下部の属性ボタンから属性を追加します。
4. 属性の key と value の値を入力し、作成ボタンを押下します。各属性の key、value
   の値は文字列として扱われることにご注意ください。

<img src="https://mintcdn.com/authlete/WGQFc11Y4wUplGrc/img/kb/ja/oauth-and-openid-connect/scopes/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88_2019-09-30_17.06.27.png?fit=max&auto=format&n=WGQFc11Y4wUplGrc&q=85&s=30605f7d35a108e227c2d4e6e01bd145" alt="スクリーンショット_2019-09-30_17" width="922" height="265" data-path="img/kb/ja/oauth-and-openid-connect/scopes/スクリーンショット_2019-09-30_17.06.27.png" />

*コンソール上でスコープの属性を作成*

> 設定手順の動画:
> [Scope attributes.mov 5.63 MB](https://storage.googleapis.com/authlete-website/Scope_attributes.mov)

## 定義済みのスコープ属性

以下にリストアップされた属性は、Authlete では特別な意味を持つものとして定義され
ています。

| 属性のキー                   | 属性の値       | 説明                                                                                                                                                                                     |
| ----------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| access\_token.duration  | 任意の数値      | スコープ単位でアクセストークンの継続時間を管理したい場合に利用します。詳細は「[トークンの有効期間のスコープ単位での制御](/ja/configuration-reference/tokens-and-claims/token-duration-per-scope)」をご覧ください。                                         |
| refresh\_token.duration | 任意の数値      | スコープ単位でリフレッシュトークンの継続時間を管理したい場合に利用します。詳細は「[トークンの有効期間のスコープ単位での制御](/ja/configuration-reference/tokens-and-claims/token-duration-per-scope)」をご覧ください。                                       |
| fapi                    | r          | FAPI read-only API プロファイルを有効化したい場合に利用します。詳細は「[FAPI の機能を利用する](https://kb.authlete.com/ja/s/fapi/a/validation)」をご覧ください。                                                                  |
| fapi                    | rw         | FAPI read-and-write API プロファイルを有効化したい場合に利用します。詳細は「[FAPI の機能を利用する](https://kb.authlete.com/ja/s/fapi/a/validation)」をご覧ください。                                                             |
| regex                   | 任意の正規表現    | スコープ文字列の一部を可変にしたい場合に利用します。詳細は「[パラメーター化されたスコープ」の利用](https://kb.authlete.com/ja/s/oauth-and-openid-connect/a/parameterized-scopes)」をご覧ください。                                              |
| fapi2                   | sp         | FAPI 2.0 Security プロファイルを有効化したい場合に利用します。詳細は、「[FAPI 2.0 Security Profile における認可コードフロー](/v2/protocols-and-flows/compliance-profiles/authz-code-flow-in-fapi-2-security-profile)」をご覧ください。 |
| fapi2                   | ms-authreq | 認可リクエストに対して FAPI 2.0 Message Signing プロファイルを有効化したい場合に利用します。この属性と (fapi2, ms-authres) の両者を特定のスコープに紐づけることもできます。                                                                          |
| fapi2                   | ms-authres | 認可レスポンスに対して FAPI 2.0 Message Signing プロファイルを有効化したい場合に利用します。この属性と (fapi2, ms-authreq) の両者を特定のスコープに紐づけることもできます。                                                                          |

## 利用方法

スコープの属性はさまざまな場面で活用可能です。以下の Java による認可サーバーのサ
ンプルコードは、クライアントからの認可リクエストを Authlete の
[/auth/authorization API](/api-reference/authorization-endpoint/process-authorization-request) を
用いて解析し、そのリクエストに含まれるスコープに関連づけられている属性をもとに、
なんらかの処理を行う例です。

```
// Call Authlete /api/authorization API.
AuthorizationResponse res = callAuthorizationAPI();

// Get scopes contained in the original authorization request.
Scope[] scopes = res.getScopes();

if (scopes == null || scopes.length() == 0) {
    return;
}

// Check each scope's attributes.
for (Scope scp in scopes) {
    // Get the scope attributes of the scope.
    Pair[] attributes = scp.getAttributes();

    if (attributes == null || attributes.length() == 0) {
        continue;
    }

    // Check each attributes.
    for (Pair attr in attributes) {
        // The key of the attribute.
        String key = attr.getKey();

        // The value of the attirbute.
        String value = attr.getValue();

        // If the key is the target one.
        if ("targetkey".equals(key)) {
            // Do something with the value.
            doSomething(value);
        }
    }
}
```
