# AWS Cognito の設定

{% hint style="warning" %}
このガイドでは、ドキュメント用の保護されたサインイン画面の設定方法を順を追って説明します。このガイドに進む前に、まず次の手順を済ませておいてください。 [認証済みアクセスを有効にする](/docs/documentation/ja-gitbook-documentation/saitoakusesu/authenticated-access/enabling-authenticated-access.md).
{% endhint %}

AWS Cognito を使用して認証済みアクセスで GitBook サイトをセットアップする手順は、次のとおりです:

{% stepper %}
{% step %}
**新しい AWS Cognito アプリケーションを作成する**

AWS ダッシュボードから AWS Cognito アプリケーションを作成します。
{% endstep %}

{% step %}
**AWS Cognito の統合をインストールして設定する**

AWS Cognito の統合をインストールし、必要な設定を追加します。
{% endstep %}

{% step %}
**アダプティブコンテンツ用に AWS Cognito を設定する（任意）**

GitBook のアダプティブコンテンツで動作するように AWS Cognito を設定します。
{% endstep %}
{% endstepper %}

### 新しい AWS Cognito アプリケーションを作成する

Cognito で目的の User Pool に移動し、\[App integration] をクリックします。Cognito ドメインを控えておいてください。統合を設定する際に必要になります。

一番下までスクロールして \[Create app client] をクリックします。アプリの種類は \[Confidential client] を選択します。下へスクロールして Hosted UI settings を開きます。Allowed Callback URLs に、スペースで統合をインストールしたときに GitBook から取得した Callback URL を入力します。

さらに下へスクロールして \[OAuth 2.0 grant types] まで進み、\[Authorization code grant] が選択されていることを確認します。

\[OpenID connect scopes] では、OpenID が選択されていることを確認します。

下へスクロールして \[Create app client] をクリックします。

作成した app client をクリックし、Client ID と Client Secret を控えておきます。

### AWS Cognito の統合をインストールして設定する

GitBook アプリ内の integrations に移動し、カテゴリーとして authenticated access を選択して、AWS Cognito の統合をインストールします。

<figure><img src="/files/ff8966a6b15bdf933048875d874c1a17a20c16c6" alt="A GitBook screenshot showing the AWS Cognito integration install screen"><figcaption></figcaption></figure>

サイトにインストールしたら、configuration に移動し、Save ボタンのすぐ上にある Callback URL を控えておきます。Cognito のセットアップに必要になります。

統合をインストールしたスペースの Cognito 統合の設定画面を開きます。

次の画像のようになるはずです:

<figure><img src="/files/d1f8557ff5d943886556931547ef3198c2d69c8c" alt="A GitBook screenshot showing the AWS Cognito configuration screen"><figcaption></figcaption></figure>

Client ID、Cognito Domain、Client Secret には、Cognito から取得した値を貼り付けます。

Save をクリックします。

次に、GitBook でインテグレーションのモーダルを閉じ、Manage site ボタンをクリックします。次に **Audience**に移動し、 **認証付きアクセス**を選択し、バックエンドとして Cognito を選びます。次に、 **オーディエンスを更新**をクリックします。サイトの画面に移動し、 **公開**をクリックします。\
\
これでサイトは、Auth0 アプリケーションによって制御される認証済みアクセスの背後で公開されました。試すには、Visit をクリックします。Cognito でのサインインを求められます。これにより、サイトが Auth0 を使用した認証済みアクセスの背後で公開されていることが確認できます。

### アダプティブコンテンツ用に AWS Cognito を設定する（任意）

GitBook で認証済みアクセスとともに Adaptive Content を活用するには、Amazon Cognito の user pool を設定して、ID トークンにカスタムクレームを含める必要があります。

これは通常、 [Cognito Lambda トリガー](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html)—特に *Pre Token Generation* Lambda—を作成して行います。これにより、カスタムクレームを上書きまたは追加する JSON ペイロードを返します。これらのクレームには、ユーザーの役割、サブスクリプション階層、またはコンテンツに関連するその他のメタデータを含めることができます。

以下は、その例です:

```javascript
export const handler = async (event, context) => {
  // event request からユーザー属性を取得
  const userAttributes = event.request.userAttributes;

  // event response に追加クレームを追加
  event.response = {
    "claimsAndScopeOverrideDetails": {
      "idTokenGeneration": {},
      "accessTokenGeneration": {
        "claimsToAddOrOverride": {
          "products": ['api', 'sites', 'askAI'],
          "isBetaUser": true,
          "isAlphaUser": true,
        }
      }
    }
  };
  // Amazon Cognito に戻す
  context.done(null, event);
};
```

<figure><img src="/files/c868cc1d6c8fc7cd8ffce8fdb7f5842d638eacc6" alt=""><figcaption></figcaption></figure>

追加すると、これらのキーと値のペアは認証トークンに含まれて GitBook に渡され、認証済みユーザーのプロフィールに基づいてサイトが動的にコンテンツを調整できるようになります。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.com/docs/documentation/ja-gitbook-documentation/saitoakusesu/authenticated-access/setting-up-aws-cognito.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
