> For the complete documentation index, see [llms.txt](https://hayashier.gitbook.io/article/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hayashier.gitbook.io/article/load-balancer/alb-authentication-get-started.md).

# ALB 認証 導入

## ALB 認証

## OIDC

Google 認証で Gmail のメールアドレスによるログイン

1. Google APIsへアクセス <https://console.developers.google.com/apis/credentials>
   * "認証情報を作成" から "OAuthクライアントID" を選択、承認済みのリダイレクト URI は、https\://＜domain name＞/oauth2/idpresponse
   * 作成されるクライアント ID とクライアントのシークレットはコピーして控えておく。
2. ALB のリスナールール編集
   * ALB のリスナールール編集で、IF パスが / , THEN 認証を OIDC として、以下の curl コマンドの実行結果に対応させて値を入力。(発行者はissuer,認証エンドポイントはauthorization\_endpoint,トークンエンドポイントはtoken\_endpoint,ユーザー情報エンドポイントはuserinfo\_endpoint)
   * クライアント IDとクライアントのシークレットはGoogle APIsで認証情報作成時に控えておいた値

```
$ curl https://accounts.google.com/.well-known/openid-configuration
{
 "issuer": "https://accounts.google.com",
 "authorization_endpoint": "https://accounts.google.com/o/oauth2/v2/auth",
 "token_endpoint": "https://www.googleapis.com/oauth2/v4/token",
 "userinfo_endpoint": "https://www.googleapis.com/oauth2/v3/userinfo",
 "revocation_endpoint": "https://oauth2.googleapis.com/revoke",
 :
```

上記仕組みのフローの詳細は、こちらの URL より参照 <https://www.exampleloadbalancer.com/auth\\_detail.html>

## Cognito

### ユーザープール

* 名前\
  プール名を "MyUserPool" のように入力
* 属性\
  "エンドユーザーをどのようにサインインさせますか?"で"E メールアドレスおよび電話番号"を選択\
  "どの標準属性が必要ですか?"で"email"を必須に選択。
* ポリシー\
  以下のように入力して、「次のステップ」を選択\
  "パスワードの強度はどれくらいを要求しますか?"で、最小長を6\
  "ユーザーに自己サインアップを許可しますか?"を"ユーザーに自己サインアップを許可する"\
  "管理者が作成したユーザーアカウントが使用されない期間がどれくらい続くと、有効期限が切れますか?"を有効期限 (日数)を7
* MFAそして確認\
  "多要素認証 (MFA) を有効にしますか?": オフ\
  "E メールまたは電話番号の検証を要求しますか?": Eメール\
  "Amazon Cognito に対して SMS メッセージの送信を許可するロールを提供する必要があります。": "MyUserPool-SMS-Role"で"ロールの作成"
* アプリクライアント\
  "アプリクライアント名": MyAppClient\
  "トークンの有効期限を更新 (日)": 30\
  "クライアントシークレットを生成"を選択\
  "アプリクライアントの作成"を選択。
* サイドメニューの"アプリクライアントの設定"を選択。\
  有効な IP プロバイダ: Cognito User Pool\
  コールバック URL: https\://＜domain name＞/oauth2/idpresponse\
  "許可されている OAuth フロー": Authorization code grant\
  "許可されている OAuth スコープ": openid
* サイドメニューの"ドメイン名"を選択\
  "ドメインのプレフィックス": <https://my-hayashier-domain.auth.us-west-2.amazoncognito.com\\>
  "変更の保存"

### フェデレーテッドアイデンティティ

* ステップ 1: ID プールを作成する\
  以下のように入力し、"プールを作成" を選択\
  ID プール名: My ID Pool\
  認証プロバイダー: Cognito
* ユーザープール ID: ＜region-id＞\_XXXXXXXXX (ユーザープールで MyUserPool を選択した状態で、サイドメニューから"全般設定"を選択し、プールIDを確認)
* アプリクライアント ID: xxxxxxxxxxxxxxxxxxxxxxxxxx (ユーザープールで MyUserPool を選択した状態で、サイドメニューから"アプリクライアント"を選択し、アプリクライアントID)

"Your Cognito identities require access to your resources "に対し、"許可"を選択

### ALB のリスナールール編集

ALB のリスナールール編集で、IF パスが / , THEN 認証を Amazon Cognito として、Cognito ユーザープール(Cognitoのユーザープールの画面で、サイドメニューの全般設定からプール ID の項目)。アプリクライアント(Cognitoのユーザープールの画面で、サイドメニューのアプリクライアントの画面の ID の項目)の値を入力。

実際のテストは、Congito のユーザープールでユーザーを作成しておいてテスト。

## 参考

<https://docs.aws.amazon.com/ja\\_jp/elasticloadbalancing/latest/application/listener-authenticate-users.html\\>
<https://aws.amazon.com/blogs/aws/built-in-authentication-in-alb/\\>
<https://dev.classmethod.jp/cloud/add-google-authenticate-your-webapp-on-alb/\\>
<https://dev.classmethod.jp/cloud/alb-cognito-user-pool/>
