1. 【AWS Black Belt Online Seminar】
Amazon Cognito
Amazon Web Services Japan K.K.
Solutions Architect Akihiro Tsukada
2017.05.17
Add user sign-up, sign-in, and data synchronization to your apps
15. Dataset1 {
key1: val1,
key2: val2...
}
SAML
Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
Amazon
アカウントA
Identity Pool 1
IAM
role
AWS STS
Temporary
Credential
Temporary
Credential
Token
Token Validation
Identity A
Cognito Sync
Logins
User
Pool 2
Dataset1
{key1: val1…}
Dataset1
{key1: val1…}
Local Storage
所有・操作
Temporary
Credential
Cognito
Federated Identities
Amazon
SNS
Amazon
Kinesis
AWS
Lambda
※この他、独自認証
サーバも利用可
完成図
15
16. Dataset1 {
key1: val1,
key2: val2...
}
SAML
Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
Amazon
アカウントA
Identity Pool 1
IAM
role
AWS STS
Temporary
Credential
Temporary
Credential
Token
Token Validation
Identity A
Cognito Sync
Logins
User
Pool 2
Dataset1
{key1: val1…}
Dataset1
{key1: val1…}
Local Storage
所有・操作
Temporary
Credential
Cognito
Federated Identities
Amazon
SNS
Amazon
Kinesis
AWS
Lambda
※この他、独自認証
サーバも利用可
完成図Cognitoを利用したアプリのシミュレーション
想定する要件
1. ユーザは複数プラットフォーム(モバイルアプリとWebブラウザ)からサービスを利用
2. ユーザはCognito Your User PoolsとAmazonアカウントでサービスを利用
3. ゲストユーザ(未認証)アクセスはナシ
4. Cognito Your User Poolsの特定グループに属するユーザは一般ユーザと別権限を持つ
シミュレーションの大まかな流れ
1. User PoolとIdentity Poolの作成
2. 複数デバイスからUser PoolとAmazonにサインアップ、サインイン
3. サインインして得たTokenをCognito Federated Identityに渡して検証
4. Tokenに問題なければCognitoはTemporary Credentialsをデバイスに返却
5. デバイスはCredentialsを使って直接AWSリソースにアクセス
6. モバイルでCognito Syncに同期したデータはWebブラウザのローカルにも反映される
16
18. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
Group n
User
Pool 2
所有・操作
IAM
role
このアプリで使うUser PoolとGroupを作成
GroupにIAM Roleを紐付け
SAML
18
19. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
Logins
Cognito
Federated Identities
Identity Poolを作成し、IAM RoleとIdPを
Identity Poolに紐付け
SAML
19
21. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
Logins
Cognito
Federated Identities
SAML
21
22. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
Logins
Cognito
Federated Identities
各デバイスから各アカウントにサインアップ & イン
SAML
Amazon
アカウントA
22
23. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
Logins
Cognito
Federated Identities
Token
各IdPから返されたTokenをFederated Identityに渡す
SAML
Amazon
アカウントA
23
24. Token Validation
Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
Logins
Cognito
Federated Identities
Token
Federated Identityは該当IdPにアクセスしTokenを検証
SAML
Amazon
アカウントA
24
25. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
Logins
Token
Token Validation
Identity A
Cognito
Federated Identities
検証成功すれば、Identity Pool内にIdentityを作成し
LoginsとしてIdPおよびTokenをIdentityに紐付け
SAML
Amazon
アカウントA
25
26. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
AWS STS
Logins
Token
Token Validation
Identity A
Temporary
Credential
Cognito
Federated Identities
Federated IdentityはAWS IAM(STS)から
Roleに基づいたTemporary Credentialsを取得
SAML
Amazon
アカウントA
26
27. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
AWS STS
Logins
Token
Token Validation
Identity A
Temporary
Credential
Temporary
Credential
Cognito
Federated Identities
Federated Identityはクライアントデバイスに
Temporary Credentialsを渡す
SAML
Amazon
アカウントA
27
28. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
AWS STS
Logins
Token
Token Validation
Identity A
Temporary
Credential
Temporary
CredentialTemporary
Credential
Cognito
Federated Identities
デバイスはCredentialsを使ってAWSリソースにアクセス
SAML
Amazon
アカウントA
28
30. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
AWS STS
Logins
Token
Token Validation
Identity A
Temporary
Credential
Temporary
CredentialTemporary
Credential
Cognito
Federated Identities
SAML
Amazon
アカウントA
30
31. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
AWS STS
Logins
Token
Token Validation
Identity A
Temporary
Credential
Temporary
CredentialTemporary
Credential
Dataset1 {
key1: val1,
key2: val2...
}
Dataset1
{key1: val1…}
Local Storage
Cognito Sync
Cognito
Federated Identities
モバイルデバイスでローカルストレージにデータを
保存し同期処理実行
SAML
Amazon
アカウントA
31
32. Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
User
Pool 2
所有・操作
Identity Pool 1
IAM
role
AWS STS
Logins
Token
Token Validation
Identity A
Temporary
Credential
Temporary
CredentialTemporary
Credential
Dataset1 {
key1: val1,
key2: val2...
}
Dataset1
{key1: val1…}
Local Storage
Cognito Sync
Dataset1
{key1: val1…}
Cognito
Federated Identities
Webブラウザで同期処理を実行するとモバイルデバイスで
の変更がWebブラウザのローカルに反映される
SAML
Amazon
アカウントA
32
33. Dataset1 {
key1: val1,
key2: val2...
}
Cognito Your User Pools
対応Identity Provider ユーザとデバイス
User
Pool 1
User Pool n
…
User Pools
アカウントA
Group n
Identity Pool 1
IAM
role
AWS STS
Temporary
Credential
Temporary
Credential
Token
Token Validation
Identity A
Logins
User
Pool 2
Dataset1
{key1: val1…}
Dataset1
{key1: val1…}
Local Storage
所有・操作
Temporary
Credential
Cognito
Federated Identities
Amazon
SNS
Amazon
Kinesis
AWS
Lambda
Cognito Sync
同期処理実行時、Cognito Syncは設定された
フック処理(SNS, Kinesis, Lambda)があれば起動する
SAML
Amazon
アカウントA
33
36. Cognito Identity
36
Your User Pools
モバイルやWebアプリに、簡単かつ
セキュアにサインアップとサインインの機能
を追加可能
数億ユーザのスケールをサポートするフル
マネージドサービス
Federated Identities
Facebookなどのソーシャルアカウントや
SAMLプロバイダといったサードパーティの
アイデンティティプロバイダでサインイン
AWSリソースへのアクセス権の管理
Facebook
Corporate
ID
Etc.
Sign in with
SAML
Sign in
Username
Password
Submit
Or
Start as a
guest
44. Your User Poolsの導入 - インポートパターン
csvファイルをアップロードすることによる
User Poolへのユーザのインポート
パスワードはインポート不可能のため、初回
サインインの際、ユーザは新規パスワードを
作成
インポートされる各ユーザはemailアドレス
もしくは電話番号を保有する必要がある
正しいファイル形式はコマンドで確認可能
44 http://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/cognito-user-pools-using-import-tool.html
$ aws cognito-idp get-csv-header --user-pool-id "USER_POOL_ID"
{"CSVHeader":["name","given_name","family_name","middle_name","nickname",
"preferred_username","profile","picture","website","email","email_verifie
d","gender","birthdate","zoneinfo","locale","phone_number","phone_number_
verified","address","updated_at","cognito:mfa_enabled","cognito:username"
],
"UserPoolId": "ap-northeast-1_FplQqjsOc"}
45. Your User Poolsの導入 - 既存システムをフックするパターン
45
1. Your User Poolsでのログイ
ンを試行
2. UserNotFoundExceptionで
あれば、ユーザ名とパスワー
ドを保持しながら既存システ
ムでのログインを試行
3. 既存システムでのログインが
成功したらUser Poolにユー
ザを作る
Amazon
Cognito
既存ログイン
サーバ
①username,
password
②username,
password
で引当
③User
NotFound
④username,
password
⑤ログイン成功
⑥username,
passwordk
でサインアップ
46. Your User Poolsの実装イメージ – JavaScript in the Browserの場合
46 https://github.com/aws/amazon-cognito-identity-js/
<script src="/path/to/aws-cognito-sdk.min.js"></script>
<script src="/path/to/amazon-cognito-identity.min.js"></script>
<!-- optional: only if you use other AWS services -->
<script src="/path/to/aws-sdk-2.6.10.js"></script>
SDKのロード
サインアップ、検証コード確認、サインイン
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser({
Username: username, Pool: createUserPoolObj()});
cognitoUser.confirmRegistration(verificationCode, forceAliasCreation,(err, result)=>{});
let poolData = {UserPoolId : '...', ClientId : '...'};
let userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
userPool.signUp(username, password, attributes, validationData, function(err, result){});
let authenticationData = {Email: email,Password: password};
let authenticationDetails = new
AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {onSuccess: (result)=>{}});
47. Amazon API Gatewayとのインテグレーション
カスタムオーソライザー ネイティブサポート
ユーザプール内に存在するユーザをもとにした
認証のためのIDトークンを受け取るように
API Gatewayを構成
OAuthやSAMLといったベアラートークン認証
の仕組みを用いてAPIへのアクセスをコント
ロール。Lambdaファンクションを用いて許可す
るアクセスポリシーを生成して返す
1 2
47
55. Your User PoolsのUser Groupで権限を分ける-1
55
User Poolでグループを作成する際にIAM Roleを設定
https://aws.amazon.com/jp/blogs/aws/new-amazon-cognito-groups-and-fine-grained-role-based-access-control-2/
56. Your User PoolsのUser Groupで権限を分ける-2
56
Identity PoolにIDプロバイダとして
User Poolを紐付ける際に
“Choose role from token” を選択
https://aws.amazon.com/jp/blogs/aws/new-amazon-cognito-groups-and-fine-grained-role-based-access-control-2/
57. Your User Poolsのユーザ属性で権限を分ける
57 https://aws.amazon.com/jp/blogs/aws/new-amazon-cognito-groups-and-fine-grained-role-based-access-control-2/
62. Cognito Identityがカバーするユースケース
Business to Consumer
IoT
Business to Employee
SAML
Federation
Enterprise
Directory
app
app
Partner A
Partner B
app
Business to Business
AWS IoT
API Gateway with Lambda
Allow
Custom
Authorizer
Access Control for AWS Resources
AWS IAM
62
Deny
64. Cognito Sync
64
User Data
Storage and
Sync
…Any Platform
iOS/Android/FireOS/Webブラウザ
ユーザのデータ、設定や情報を保存
アプリとデバイスのデータをクラウドに保存でき、
未認証Identityで保存したデータは認証後にマージ
クロスデバイス/クロス OS な同期
数行のコードでデバイスやプラットフォームをま
たがってユーザのデータや設定を同期
オフライン動作
データは常にローカルのストレージにまず保存さ
れるので電波が不安定もしくは不通であっても
シームレスに動作
k/v
data
Identity pool
バックエンド不要
スケーラブルで信頼性の高いデータ同期基盤
をシンプルなクライアント SDK を組み込む
だけで利用可能
65. Cognito Federated Identityと
Cognito Sync のデータモデル
Identity Pool: アプリユーザのPool。アプ
リ間で共有する場合もある
Identity: 個々のユーザ。IDプロバイダ間
をまたいでユニーク。ゲストユーザも可
Dataset: ユーザごとのデータのグループ
Record: Key/Valueのユーザデータ
AWS
Account
Dataset
Identity
Identity
Identity
Dataset
Dataset
Identity
Pool
1:60
1:n
1:20
Dataset
Dataset
Record
1:1024
You
Your App
Your App Users
User Data
Container
User Data
65