【问题标题】:Using aws STS to get temporary credentials: Where is the web identity token?使用 aws STS 获取临时凭证:Web 身份令牌在哪里?
【发布时间】:2020-02-18 21:32:18
【问题描述】:

我的用户通过连接到 cognito 的微服务登录到我的应用程序(请求通过 API 网关代理)

他们得到一个会话令牌。

登录后,他们需要将一些文件放入 S3。

我想使用 STS 为他们提供临时凭据,但要调用 sts.AssumeRoleWithWebIdentity 我需要一个网络身份令牌。

如何获取带有会话令牌作为输入的 Web 身份令牌?

我编写了一个临时 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

【问题讨论】:

    标签: amazon-web-services go aws-sts


    【解决方案1】:

    先进行几项检查:

    • 让 cognito 身份验证用户以 iam 角色“伪装”,为此我们使用 trust relationships,您可以快速重用分配给您的 cognito 身份池的 iam 角色。
    • access to s3 bucket 授予该iam 角色策略

    完成后:

    再次运行cognitoidentity.getCredentialsForIdentity(...),它会先通过sts,因此您不必调用sts承担角色api。如果成功,响应应该有AccessKeyIdSecretKeySessionToken。这些是可以访问 s3 的过期 aws 凭据,并且会在一个小时后消失(除非设置)。将它们用作正常的会话身份验证。

    creds = new SessionAWSCredentials(AccessKeyId, SecretKey, SessionToken);
    s3Request = CreateAmazonS3Client(creds);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多