Amazon Cognito
Web・モバイルアプリに認証・認可機能を追加する AWS マネージドサービス
AWS認証
Cognito とは
Amazon Cognito は、Web・モバイルアプリに認証・認可機能を追加する AWS マネージドサービスである。ユーザープール (認証) とアイデンティティプール (認可) の 2 つのコンポーネントで構成される。
ユーザープール vs アイデンティティプール
| コンポーネント | 役割 | 出力 |
|---|---|---|
| ユーザープール | 認証 (サインアップ、ログイン) | JWT (ID トークン, アクセストークン) |
| アイデンティティプール | 認可 (AWS リソースへのアクセス) | 一時的な AWS 認証情報 |
SAM での定義
UserPool:
Type: AWS::Cognito::UserPool
Properties:
AutoVerifiedAttributes: [email]
Policies:
PasswordPolicy:
MinimumLength: 8
UserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref UserPool
ExplicitAuthFlows: [ALLOW_USER_SRP_AUTH, ALLOW_REFRESH_TOKEN_AUTH]
API Gateway との統合
MyApi:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: CognitoAuth
Authorizers:
CognitoAuth:
JwtConfiguration:
issuer: !Sub https://cognito-idp.${AWS::Region}.amazonaws.com/${UserPool}
audience: [!Ref UserPoolClient]
Cognito vs 自前認証 vs Auth0
| 観点 | Cognito | 自前実装 | Auth0 |
|---|---|---|---|
| コスト | 50,000 MAU 無料 | 開発コスト | 有料 |
| 管理負荷 | 低い | 高い | 低い |
| カスタマイズ | 中 | 高い | 中 |
| AWS 統合 | ✅ ネイティブ | 手動 | 手動 |
基礎から学ぶなら関連書籍が手がかりになる。