python - 是否可以使用 aws cognito google oauth access_token 通过 boto3 获取临时凭证?

标签 python amazon-web-services boto3 amazon-cognito

我想通过 AWS Cognito(Google 身份提供商)授权对 API 的用户请求。目前,我可以从 aws 接收 JWT token (id_token 和 access_token)并授权请求,但我是使用 id_token 而不是使用 access_token 来完成的。但有些文章说使用 id_token 授权 API 请求是一种不好的做法(从前端在 header 中发送 id_token),我应该使用 access_token。是否可以使用 access_token 而不是 id_token 做同样的事情?

import boto3

id_token = get_token_from_headers(headers)

identity_client = boto3.client('cognito-identity')

id_response = identity_client.get_id(
     AccountId='account_id',
     IdentityPoolId='identity_pool_id',
     Logins={
         'cognito-idp.us-west-1.amazonaws.com/us-west-1_blabla: id_token'
     }
 )

response = identity_client.get_credentials_for_identity(
     IdentityId=id_response['IdentityId'],
     Logins={
         'cognito-idp.us-west-1.amazonaws.com/us-west-1_blabla': id_token
     })


access_key = response['Credentials']['AccessKeyId']
secret_key = response['Credentials']['SecretKey']
session_key = response['Credentials']['SessionToken']

看起来它能够从 assume_role_with_web_identity link to docs 获取带有 access_token 的临时凭证,但文档说此方法的 WebIdentityToken 参数仅在 oAuth 提供者是 Amazon 和 Facebook 的情况下接受 access_token

最佳答案

似乎通过 AWS Cognito 身份池检索临时凭证始终是通过身份 token :https://docs.aws.amazon.com/cognito/latest/developerguide/google.html

Successful authentication results in a response object that contains an id_token, which Amazon Cognito uses to authenticate the user and generate a unique identifier

我认为这不会破坏安全性,因为 AWS Cognito 身份池是专门为使用身份 token 而设置的,您可以根据返回的身份 token 配置将分配给您的临时 AWS 凭证的 IAM 角色您来自身份池:https://docs.aws.amazon.com/cognito/latest/developerguide/iam-roles.html , https://docs.aws.amazon.com/cognito/latest/developerguide/role-based-access-control.html

但是,您不应该将 Google id_token 发送到您获取 AWS 临时凭证的 API(我假设基于 AWS)。 相反,您应该在客户端上获取 AWS 临时凭证,然后使用 Signature V4 header 调用 AWS 资源。

或者,如果您的 API 可以承担 AWS 服务角色并且您不需要获取临时 AWS 凭证,则可以使用 AWS Cognito 用户池。他们还支持 Google 身份提供商 - https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-social-idp.html

对于 EC2 上托管的 API,您无法真正使用 Cognito 身份池生成的安全凭证,因为您无法通过 IAM 策略有效控制 EC2 HTTP(S) 端口访问。不过,您的 EC2 托管应用程序可以接受 Google id_token 并验证它 - https://developers.google.com/identity/sign-in/web/backend-auth#:~:text=After%20you%20receive%20the%20ID,to%20verify%20the%20token's%20signature 。没有理由使用身份池,并且通过 Google id_token 验证 API 访问不存在安全风险,因为该 token 是由 Google 签名的,您可以在 API 上进行验证。您还可以使用 Cognito 用户池:您从客户端上的用户池检索访问 token 并将其发送到 EC2 上托管的 API。然后,您验证托管应用程序上的访问 token - https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html

如果您的 EC2 是通过弹性负载均衡器访问的,您可以将用户身份验证转移到 ELB 级别。您可以使用 Cognito 用户池或 Google 身份提供商用户身份验证 - https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-authenticate-users.html

对于 Amazon API Gateway,您可以使用 Cognito 身份池:在客户端上生成的安全凭证将允许执行给定的 API 网关,并且您可以在 API 网关上启用 IAM 身份验证 - https://aws.amazon.com/premiumsupport/knowledge-center/iam-authentication-api-gateway/ 。您还可以使用 Cognito 用户池:您从客户端上的用户池检索访问 token 并将其发送到启用了 Cognito 身份验证的 API Gateway - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html

关于python - 是否可以使用 aws cognito google oauth access_token 通过 boto3 获取临时凭证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65570158/

相关文章:

apache - 亚马逊网络服务和 ubuntu 10.04 ec2 实例

python - 在 Python 中,为什么 itertools.cycle 需要额外的内存?

python - Subprocess.call 或 Subprocess.Popen 不能使用 PATH (Linux/Windows) 中的可执行文件

amazon-web-services - 有没有办法向AWS ELB中的所有实例发送http请求?

python - 如何使用 boto3 资源更新 EC2 实例状态

python - 如何使用 boto3 和 moto 测试方法

python - 当 DynamoDB 中不存在项目时,如何使用 Python (boto3) 强制 delete_item 返回错误?

python - 在 Python 中尝试并排除

python - 多个参数 psycopg2 where 子句 - 语法错误

node.js - 将图像附加到 S3 存储桶中以使用 SendGrid 发送