LoginSignup
5
5

More than 5 years have passed since last update.

AWS API Gateway, Lambda, DynamoDB, Cognito を使ってGoogle+でログインし、ユーザーごとにアクセス権を与える

Posted at

Cognitoを使ってGoogle+でログイン

手順:
http://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/google.html
Google Console:
https://console.developers.google.com
- プロジェクト作成
- Google+ APIを有効化
- 認証情報を作成し、OAuthクライアントIDを取得
- AWS ConsoleでID Poolを作成

AWS CLIを使ってRoleを作る。
http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Cognito.Credentials.html
Googleログインボタンを挿入。
http://rmdi.hatenablog.com/entry/2015/12/22/142059

CognitoでAPI Gatewayをアクセス制限

以下のURLの手順に従う。
http://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html
1. 作ったID PoolとAPIを結合。
2. ヘッダーにIDトークンを入れて、アクセス制限のあるAPIにアクセス。

jsでCognitoIDを取得し、XMLHttpRequestでAPIにアクセス。
http://edtheron.me/projects/store-messages-aws-dynamodb-lambda-api-gateway-cognito

Fine-Grained Access ControlでDynamoDBのアクセス制限

UserIDをハッシュに設定し、IAMポリシーのConditionに${cognito-identity.amazonaws.com:sub}をdynamodb:LeadingKeysとして設定し作成。
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/specifying-conditions.html

GoogleIDを更新

アクセス制限のAPI先でDynamoDBアクセス拒否時に、IDを更新。jsで実装。
- gapi.auth2.getAuthInstance().signIn()でGoogleIDを更新。
- AWS.config.credentials.refreshでAWS認証情報を更新。コールバックにPromiseを使い、解決した時にAWS.config.credentials.identityIdを返す。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5