使用 Cognito UserPools 进行社交登录的 Boto3

Boto3 for Social Logins using Cognito UserPools

我有一个带有 signup/signin 选项的移动应用程序。移动应用程序调用 Rest API,API 使用 Python boto3 CognitoIdentityProvider 客户端在 AWS Cognito 用户池中创建用户。使用 email/password 登录工作正常。

对于社交登录,移动应用更新为 google 登录并获取 idToken、accessToken。如何使用 google 从后端 python 环境向 Cognito 用户池中的 signin/create 用户返回登录令牌?这可行吗?

对于 username/password 个选项,我使用 signup and admin_initiate_auth 方法。但不确定在使用 google/facebook 登录选项时使用什么来允许用户在 UserPool 中签名或创建用户。

本质上,在 Boto3 或其他 AWS 库中有一种方法可以使用 google/facebook 返回的 idToken>

在 UserPool 中创建用户

get_id 来自 boto3 CongnitoIdentity 服务的方法解决了这个问题。

使用 google 返回的 ID 令牌,调用 get_id 创建联合身份。

    client = boto3.client('cognito-identity',
                        aws_access_key_id=ACCESS_KEY,
                        aws_secret_access_key=ACCESS_SECRET_KEY)
    response = client.get_id(
        AccountId='YOUR AWS ACCOUNT ID',
        IdentityPoolId='us-east-1:xxxdexxx-xxdx-xxxx-ac13-xxxxf645dxxx',
        Logins={
            'accounts.google.com': 'google returned IdToken'
        },
    )

响应包括 Cognito IdentityId:

{
"ResponseMetadata": {
  "RetryAttempts": 0,
  "HTTPStatusCode": 200,
  "RequestId": "xxxfb049b-1f77-xxxx-a67c-xxxfb049b",
  "HTTPHeaders": {
    "date": "Sun, 04 Mar 2018 06:43:13 GMT",
    "x-amzn-requestid": "xxxfb049b-1f77-xxx-a67c-xxxfb049b",
    "content-length": "63",
    "content-type": "application/x-amz-json-1.1",
    "connection": "keep-alive"
  }
},
"IdentityId": "us-east-1:xxx-2xx1-1234-9xx2-xxxx"
}