【发布时间】:2021-04-10 16:30:14
【问题描述】:
我想通过 AWS Cognito(Google 身份提供商)授权用户对 API 的请求。目前我可以从 aws 接收 JWT 令牌(id_token 和 access_token)并授权请求,但我是使用 id_token 而不是 access_token 来完成的。但是有些文章说使用 id_token 来授权 API 请求是一种不好的做法(从前端的 headers 中发送 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
【问题讨论】:
标签: python amazon-web-services boto3 amazon-cognito