我的用户通过连接到cognito的微服务登录到我的应用程序(请求通过API网关代理)
他们获得了会话令牌。
登录后,他们需要将一些文件放入S3。
我想使用STS为他们提供临时凭据,但要致电sts.AssumeRoleWithWebIdentity
,我需要一个Web身份令牌。
如何获取带有会话令牌的Web身份令牌?
我写了一个临时的lambda(节点),该Lambda在使用用户名和密码登录后返回STS凭据:
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
const cognitoidentity = new AWS.CognitoIdentity();
cognitoidentityserviceprovider.initiateAuth(...) //AuthFlow: 'USER_PASSWORD_AUTH'
cognitoidentity.getId(...)
cognitoidentity.getCredentialsForIdentity(...)
在登录和上传文件之间可能需要一段时间,我不希望用户每次都提交用户名/密码。也没有AuthFlow接受会话令牌。
我猜想API网关可能返回有用的东西,但是我在文档中没有找到任何东西: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference
答案 0 :(得分:1)
先检查一下
完成后:
再次运行cognitoidentity.getCredentialsForIdentity(...)
,它将首先通过sts,因此您不必调用sts假定角色api。如果成功,则响应应包含AccessKeyId
,SecretKey
和SessionToken
。这些是可以访问s3的到期AWS凭证,一小时后将消失(除非设置)。将它们用作常规会话身份验证。
creds = new SessionAWSCredentials(AccessKeyId, SecretKey, SessionToken);
s3Request = CreateAmazonS3Client(creds);